From 65b044e61886f8537270df95b7bfefd637b5f5aa Mon Sep 17 00:00:00 2001 From: Albert S Date: Sat, 8 May 2021 20:51:05 +0200 Subject: [PATCH] Begin SpecialCommand handling Introduce SpecialCommand handling. Currently, we block until the command finishes. SettingsProvider provides a few example, but needs implementation to read this from config. --- qsrun.pro | 1 + settingsprovider.cpp | 25 +++++++++++++++++++++++++ settingsprovider.h | 2 ++ window.cpp | 40 ++++++++++++++++++++++++++++++++++------ window.h | 4 ++++ 5 files changed, 66 insertions(+), 6 deletions(-) diff --git a/qsrun.pro b/qsrun.pro index 9c18929..5609d65 100644 --- a/qsrun.pro +++ b/qsrun.pro @@ -12,6 +12,7 @@ HEADERS += calculationengine.h \ entrypushbutton.h \ settingsprovider.h \ singleinstanceserver.h \ + specialcommandconfig.h \ textoutputlabel.h \ window.h SOURCES += calculationengine.cpp \ diff --git a/settingsprovider.cpp b/settingsprovider.cpp index 1089d0c..38a57bb 100644 --- a/settingsprovider.cpp +++ b/settingsprovider.cpp @@ -38,3 +38,28 @@ QString SettingsProvider::socketPath() const { return settings->value("singleInstanceSocket", "/tmp/qsrun").toString(); } + +QVector SettingsProvider::specialCommands() const +{ + QVector result; + + SpecialCommandConfig uname; + uname.command = "uname"; + uname.reqArgCount = 0; + uname.immediateProcessing = true; + result.append(uname); + + + SpecialCommandConfig date; + date.command = "date"; + date.reqArgCount = 0; + date.immediateProcessing = true; + result.append(date); + + SpecialCommandConfig echo; + echo.command = "echo"; + echo.reqArgCount = 0; + echo.immediateProcessing = true; + result.append(echo); + return result; +} diff --git a/settingsprovider.h b/settingsprovider.h index 99ad897..195890b 100644 --- a/settingsprovider.h +++ b/settingsprovider.h @@ -3,6 +3,7 @@ #include #include +#include "specialcommandconfig.h" class SettingsProvider { @@ -17,6 +18,7 @@ class SettingsProvider virtual bool singleInstanceMode() const; QString getTerminalCommand() const; QString socketPath() const; + QVector specialCommands() const; }; #endif // SETTINGSPROVIDER_H diff --git a/window.cpp b/window.cpp index 198ae79..4833457 100644 --- a/window.cpp +++ b/window.cpp @@ -30,6 +30,7 @@ #include "entryprovider.h" #include "window.h" + Window::Window(EntryProvider &entryProvider, SettingsProvider &configProvider) { this->entryProvider = &entryProvider; @@ -53,6 +54,7 @@ void Window::initFromConfig() { this->userEntryButtons = generateEntryButtons(entryProvider->getUserEntries()); this->systemEntryButtons = generateEntryButtons(entryProvider->getSystemEntries()); + this->specialCommands = settingsProvider->specialCommands(); } catch(const ConfigFormatException &e) { @@ -318,14 +320,18 @@ void Window::lineEditTextChanged(QString text) addPATHSuggestion(text); if(this->grid->count() == 0) { - QStringList arguments = text.split(" "); + QStringList arguments = QProcess::splitCommand(text); + QString command = arguments[0]; + auto specialCommandConfig = getSpecialCommandConfig(command); + if(specialCommandConfig) + { + executeSpecialCommand(specialCommandConfig.value(), arguments); + return; + } EntryConfig e; e.name = "Execute: " + text; - if(arguments.length() > 1) - { - e.arguments = arguments.mid(1); - } - e.command = arguments[0]; + e.command = command; + e.arguments = arguments; e.iconPath = "utilities-terminal"; e.type = EntryType::DYNAMIC; @@ -407,6 +413,28 @@ int Window::rankConfig(const EntryConfig &config, QString filter) const return -1; } +std::optional Window::getSpecialCommandConfig(QString cmd) const +{ + SpecialCommandConfig result; + for(const SpecialCommandConfig &config : this->specialCommands) + { + if(config.command == cmd) + { + return config; + } + } + return { }; +} + +void Window::executeSpecialCommand(const SpecialCommandConfig &config, QStringList arguments) +{ + QProcess process; + process.start(config.command, arguments.mid(1)); + process.waitForFinished(); + QString result = process.readAllStandardOutput(); + showGrowingOutputText(result); +} + void Window::filterGridFor(QString filter) { if(filter.length() > 0) diff --git a/window.h b/window.h index 4a2ca28..58a64be 100644 --- a/window.h +++ b/window.h @@ -36,6 +36,7 @@ #include "entrypushbutton.h" #include "calculationengine.h" #include "settingsprovider.h" +#include "specialcommandconfig.h" #include "textoutputlabel.h" class RankedButton @@ -60,6 +61,7 @@ class Window : public QWidget QVector userEntryButtons; QVector systemEntryButtons; QVector buttonsInGrid; + QVector specialCommands; TextoutputLabel calculationResultLabel; QString currentCalculationResult; QString queuedFileSearch; @@ -86,6 +88,8 @@ class Window : public QWidget void closeWindow(); std::pair getNextFreeCell(); int rankConfig(const EntryConfig &config, QString filter) const; + std::optional getSpecialCommandConfig(QString cmd) const; + void executeSpecialCommand(const SpecialCommandConfig &config, QStringList arguments); void showGrowingOutputText(QString text); private slots: void lineEditReturnPressed();