Skip to content

Commit

Permalink
[RELEASE v0.1.1] Release Spirit v0.1.1 (bugfix)
Browse files Browse the repository at this point in the history
  • Loading branch information
antony-jr committed May 18, 2021
1 parent f112275 commit a3213b8
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 81 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ list(APPEND source include/termcolor.hpp
include/windowinfo.hpp
include/helpers_p.hpp
include/spirit.hpp
include/bashrcwriter.hpp
src/bashrcwriter.cc
src/spirit.cc
src/windowinfo_p.cc
src/windowinfo.cc
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ See the demo at https://www.youtube.com/watch?v=KC_MwRyo0js
# Quick Start

```
$ wget "https://github.com/antony-jr/spirit/releases/download/v0.1.0/spirit"
$ wget "https://github.com/antony-jr/spirit/releases/download/v0.1.1/spirit"
$ chmod +x spirit
$ ./spirit init
$ # Now only the focused terminal will have the overlay.
Expand Down Expand Up @@ -50,7 +50,7 @@ Somewhat considered as stable. These builds are usually older than the developme


```
$ wget "https://github.com/antony-jr/spirit/releases/download/v0.1.0/spirit"
$ wget "https://github.com/antony-jr/spirit/releases/download/v0.1.1/spirit"
$ chmod +x spirit
$ ./spirit # Use it now, or move it to /usr/bin/ to install
```
Expand Down
17 changes: 17 additions & 0 deletions include/bashrcwriter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef BASHRC_WRITER_HPP_INCLUDED
#define BASHRC_WRITER_HPP_INCLUDED
#include <QString>

class BashRCWriter {
public:
BashRCWriter();
~BashRCWriter();

void setProgram(const QString&);
bool write();
bool unwrite();
private:
QString m_Program;
};

#endif // BASHRC_WRITER_HPP_INCLUDED
5 changes: 5 additions & 0 deletions release_notes/v0.1.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Spirit v0.1.1

* Fixed .bashrc writer to be more robust.
* Avoid invalid code written to .bashrc
* Fixed applicationFilePath() for **AppImage**.
6 changes: 4 additions & 2 deletions spirit.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ HEADERS += \
$$PWD/include/windowinfo_p.hpp \
$$PWD/include/windowinfo.hpp \
$$PWD/include/helpers_p.hpp \
$$PWD/include/spirit.hpp
$$PWD/include/spirit.hpp \
$$PWD/include/bashrcwriter.hpp

SOURCES += \
$$PWD/src/spirit.cc \
$$PWD/src/windowinfo_p.cc \
$$PWD/src/windowinfo.cc \
$$PWD/src/helpers_p.cc \
$$PWD/src/main.cc
$$PWD/src/bashrcwriter.cc \
$$PWD/src/main.cc

RESOURCES = resources.qrc
101 changes: 101 additions & 0 deletions src/bashrcwriter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QStringList>

#include "bashrcwriter.hpp"

static bool writeContents(const QString &bashrc, const QStringList &contents) {
QFile file(bashrc);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
return false;
}
file.seek(0);
QTextStream out(&file);

for(auto line : contents) {
out << line;
}
file.close();
return true;
}

BashRCWriter::BashRCWriter() { }
BashRCWriter::~BashRCWriter() { }

void BashRCWriter::setProgram(const QString &value) {
m_Program = value;
}

bool BashRCWriter::write() {
if(m_Program.isEmpty()) {
return false;
}
QString comment = QString::fromUtf8("### spirit trap commands\n");
QString trap_line = QString::fromUtf8("trap \"%1 %2\" %3\n");
const QString bashrc = QDir::homePath() + QDir::separator() + ".bashrc";

QFile file(bashrc);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
return false;
}

file.seek(0);

QStringList contents;
bool appended = false;
while(!file.atEnd()) {
auto line = file.readLine();
if(line.contains("### spirit trap commands")) {
contents << comment;
contents << trap_line.arg(m_Program).arg("error").arg("ERR");
contents << trap_line.arg(m_Program).arg("nonerror").arg("DEBUG");

/// Go past two lines
line = file.readLine();
line = file.readLine();

appended = true;
}else {
contents << line;
}
}

if(!appended) {
contents << comment;
contents << trap_line.arg(m_Program).arg("error").arg("ERR");
contents << trap_line.arg(m_Program).arg("nonerror").arg("DEBUG");
}
file.close();

return writeContents(bashrc, contents);
}

bool BashRCWriter::unwrite() {
if(m_Program.isEmpty()) {
return false;
}

const QString bashrc = QDir::homePath() + QDir::separator() + ".bashrc";
QFile file(bashrc);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
return false;
}

file.seek(0);

QStringList contents;
while(!file.atEnd()) {
auto line = file.readLine();
if(line.contains("### spirit trap commands")) {
/// Go past two lines
line = file.readLine();
line = file.readLine();
}else {
contents << line;
}
}
file.close();

return writeContents(bashrc, contents);
}
99 changes: 22 additions & 77 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include "termcolor.hpp"
#include "windowinfo.hpp"
#include "spirit.hpp"
#include "bashrcwriter.hpp"

static void info() {
std::cout << termcolor::bold << "Spirit"
<< termcolor::reset
<< " "
<< termcolor::underline
<< "v0.1.0"
<< "v0.1.1"
<< termcolor::reset
<< ","
<< " Overlay gif over any XWindow."
Expand Down Expand Up @@ -162,6 +163,17 @@ int main(int ac, char **av) {
return 0;
}

QString programPath;
auto appimage = std::getenv("APPIMAGE");
if(appimage) {
programPath = QString::fromUtf8(appimage);
}else {
programPath = QCoreApplication::applicationFilePath();
}

BashRCWriter bashrc_writer;
bashrc_writer.setProgram(programPath);

const QString subcommand = args.at(0).toLower();
const QString socketName = QString::fromUtf8("com.github.antony-jr.spirit");
QLocalSocket socket;
Expand Down Expand Up @@ -230,20 +242,13 @@ int main(int ac, char **av) {
return -1;
}

QString program;
auto appimage = std::getenv("APPIMAGE");
if(appimage) {
program = QString::fromUtf8(appimage);
}else {
program = QCoreApplication::applicationFilePath();
}
arguments.removeFirst();
arguments.replaceInStrings(
QString::fromUtf8("init"),
QString::fromUtf8("server"),
Qt::CaseInsensitive);

bool r = QProcess::startDetached(program, arguments);
bool r = QProcess::startDetached(programPath, arguments);
if(!r) {
std::cout << termcolor::bold
<< termcolor::red
Expand All @@ -254,47 +259,17 @@ int main(int ac, char **av) {
return -1;
}

//// Write trap commands into bashrc.
const QString bashrc = QDir::homePath() + QDir::separator() + ".bashrc";
{
QFile file(bashrc);
if(!file.open(QIODevice::ReadWrite)) {
std::cout << "Warning: cannot update .bashrc."
<< "\n";
}else {
file.seek(0);
bool avoid_comment = false;
while(true) {
QByteArray line;
line = file.readLine(2096);
if(line.size() == 0) {
break;
}

if(line.contains("### spirit trap commands")) {
avoid_comment = true;
break;
}
}

QString comment = QString::fromUtf8("\n### spirit trap commands\n");
QString trap_line = QString::fromUtf8("trap \"%1 %2\" %3\n");

if(!avoid_comment) {
file.write(comment.toUtf8());
}
file.write(trap_line.arg(program).arg("error").arg("ERR").toUtf8());
file.write(trap_line.arg(program).arg("nonerror").arg("DEBUG").toUtf8());

std::cout << termcolor::bold
if(bashrc_writer.write()) {
std::cout << termcolor::bold
<< "~/.bashrc updated. Please restart your terminal for full effect."
<< termcolor::reset
<< "\n";
}

file.close();
}else {
std::cout << "Warning: cannot update .bashrc."
<< "\n";
}


std::cout << termcolor::bold
<< termcolor::yellow
<< "Spirit Initialized Successfully."
Expand Down Expand Up @@ -400,7 +375,7 @@ int main(int ac, char **av) {
bool errored = false;

QObject::connect(&server, &QLocalServer::newConnection,
[&server, &app, &s, &error_file, &errored, &info, &filename, &error]() {
[&server, &app, &s, &error_file, &errored, &info, &filename, &error, &bashrc_writer]() {
auto socket = server.nextPendingConnection();
if(!socket || !socket->isValid()) {
return;
Expand All @@ -412,37 +387,7 @@ int main(int ac, char **av) {
socket->disconnectFromServer();

if(dataSent.contains("quit")) {
//// Remove trap commands from bashrc.
const QString bashrc = QDir::homePath() + QDir::separator() + ".bashrc";
{
QFile file(bashrc);
if(file.open(QIODevice::ReadWrite)) {
file.seek(0);
bool write_empty = false;
while(true) {
QByteArray line;
line = file.readLine(2096);
if(line.size() == 0) {
break;
}

if(line.contains("### spirit trap commands")) {
write_empty = true;
break;
}
}

QString c = QString::fromUtf8("### ");
if(write_empty) {
file.write(c.toUtf8());

auto line = file.readLine(1024);

file.write(c.toUtf8());
}
}
file.close();
}
bashrc_writer.unwrite();
QTimer::singleShot(1000, &app, &QApplication::quit);
}else if(dataSent.contains("check")) {
auto args = dataSent.split(" ");
Expand Down

0 comments on commit a3213b8

Please sign in to comment.