]> gitweb.hamatoma.de Git - reqt/commitdiff
recommand: command line, tail
authorhama <hama@siduction.net>
Sun, 14 Feb 2016 07:57:27 +0000 (08:57 +0100)
committerhama <hama@siduction.net>
Sun, 14 Feb 2016 07:57:27 +0000 (08:57 +0100)
appl/recommand/CommandProcessor.cpp
appl/recommand/CommandProcessor.hpp
appl/recommand/mainwindow.cpp
appl/recommand/mainwindow.hpp
appl/recommand/mainwindow.ui
appl/recommand/recommand.hpp
appl/recommand/recommand.pro
base/ReProgramArgs.cpp
base/ReProgramArgs.hpp

index 9731c715f1a0937b9b0a7e71cb61826dc8529f50..f702b9f550010060245a7a17a2ec14afaded8d20 100644 (file)
@@ -54,6 +54,76 @@ void CommandProcessor::out(const QString& message)
        m_mainWindow->say(LOG_INFO, message);
 }
 
+/**
+ * @brief CommandProcessor::tail
+ * @param args
+ */
+void CommandProcessor::tail(const QStringList& args)
+{
+       ReProgramArgs args2("Prints the last N lines of the files",
+                                          "tail -n 20 abc.txt xyz.txt");
+       args2.addInt("count", "number of lines to print", 'n', "lines", 10);
+       args2.addBool("quiet", "the filenames will be suppressed", 'q', "quiet", false);
+       args2.init(args, true);
+       if (args2.argCount() < 1)
+               args2.help("no files given");
+       else if (args2.argCount() == 1){
+               execTail(args2.arg(0), false, args2.getInt("count"));
+       } else {
+               for (int ix = 0; ix < args2.argCount(); ix++){
+                       execTail(args2.arg(ix), ! args2.getBool("quiet"), args2.getInt("count"));
+               }
+       }
+}
+
+/**
+ * Issues the last N lines of a file.
+ *
+ * @param file         the name of the file
+ * @param withName     <code>true</code>: the filename is issued in front of the content
+ * @param count                the number of lines to issue
+ */
+void CommandProcessor::execTail(const QString& file, bool withName, int count){
+       QByteArray file2 = I18N::s2b(file);
+       FILE* fp = fopen(file2, "rb");
+       if (fp == NULL){
+               printf("cannot open %s: %d", file2.constData(), errno);
+       } else {
+               if (withName)
+                       printf("%s:\n", file2.constData());
+               QByteArray content;
+               int bufferSize = max(0x10000, count * 100) / 2;
+               int count2 = 0;
+               do {
+                       bufferSize *= 2;
+                       content.resize(bufferSize);
+                       size_t pos = fseek(fp, -bufferSize, SEEK_END);
+                       int pos2 = 0;
+                       if ( (pos2 = fread(content.data(), 1, bufferSize, fp)) <= 0){
+                               break;
+                       } else {
+                               content.resize(pos2);
+                               const char* ptr = content.constData() + pos2 - 1;
+                               const char* start = content.constData();
+                               if (ptr > start && *ptr == '\n'){
+                                       ptr--;
+                               }
+                               int count2 = 0;
+                               while(ptr > start && count2 < count){
+                                       if (*ptr-- == '\n')
+                                               count2--;
+                                       if (ptr == content.constData())
+                                               break;
+                               }
+                               if (count2 == count){
+                                       fputs(*ptr == '\n' ? ptr + 1 : ptr, fp);
+                               }
+                       }
+               } while(count2 != count);
+               fclose(fp);
+       }
+}
+
 /**
  * Executes one statement.
  *
index 09c626ac31caf8d58f5319252331c263f04bc000..5c32dd8cd4563835d37e91629476119f44fc4925 100644 (file)
@@ -22,11 +22,13 @@ public:
 public:
        QString calcNOfM(int n, int m, const QString& separator = " ");
        void euroJackpot(const QStringList& args);
+       void execTail(const QString& file, bool withName, int count);
        void help(const QString& errorMessage, const QStringList& args);
        void interpret(const QString& text);
        void lotto(const QStringList& args);
        void nOfM(const QStringList& args);
        void out(const QString& message);
+       void tail(const QStringList& args);
        void time(const QStringList& args);
        void statement(int lineNo, const QString& text);
 protected:
index e8645e46e38b78bc2e6b9e41de6f77d5778922cf..ec4b6e21d89977b61869eb87ecb1eb33aff01db5 100644 (file)
@@ -32,9 +32,11 @@ MainWindow::MainWindow(const QString& homeDir, QWidget *parent) :
        connect(ui->pushButtonSave, SIGNAL(clicked()), this, SLOT(onSave()));
        connect(ui->pushButtonInterpolate, SIGNAL(clicked()), this, SLOT(onInterpolate()));
        connect(ui->pushButtonExpand, SIGNAL(clicked()), this, SLOT(onExpand()));
+       connect(ui->lineEditCommand, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
+       connect(ui->listWidgetHistory, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
+                       this, SLOT(onHistoryDoubleClicked(QListWidgetItem*)));
        onLoad();
 }
-
 /**
  * Destructor.
  */
@@ -84,6 +86,15 @@ void MainWindow::onExpand()
        }
 }
 
+/**
+ * Handles a double click in the command history.
+ *
+ * @param item the current item
+ */
+void MainWindow::onHistoryDoubleClicked(QListWidgetItem* item){
+       ui->lineEditCommand->setText(item->text());
+}
+
 /**
  * Set GUI elements from the queue when the GUI timer is triggered.
  */
@@ -152,6 +163,15 @@ void MainWindow::onInterpolate()
        }
 }
 
+/**
+ * Handles the RETURN button in the command line.
+ */
+void MainWindow::onReturnPressed(){
+       m_processor->interpret(ui->lineEditCommand->text());
+       toHistory(ui->lineEditCommand->text());
+       ui->lineEditCommand->clear();
+}
+
 /**
  * Executes the current commands.
  */
@@ -176,8 +196,10 @@ void MainWindow::onRun()
                pos2++;
        ui->plainTextEditResult->clear();
        QString script = text.mid(pos1, pos2 - pos1 + 1);
-       if (script.length() > 10 || script.trimmed().length() > 0)
+       if (script.length() > 10 || script.trimmed().length() > 0){
+               toHistory(script);
                m_processor->interpret(script);
+       }
 }
 
 /**
@@ -202,6 +224,19 @@ void MainWindow::onSave()
                                                         ui->plainTextEditPad->toPlainText().toUtf8().constData());
 }
 
+/**
+ * Inserts commands into the command history.
+ *
+ * @param commands     the commands to insert
+ */
+void MainWindow::toHistory(const QString& commands)
+{
+       QStringList list = commands.split('\n');
+       for (int ix = 0; ix < list.count(); ix++){
+               ui->listWidgetHistory->addItem(list.at(ix));
+       }
+}
+
 /**
  * Writes a message.
  *
index a75d985b6d4b906d46649a777f05ef7d1836ea5f..60e8884166e984d7f2d88de8edf89c729784cacd 100644 (file)
@@ -33,10 +33,14 @@ public slots:
        void onClearEdit();
        void onClearList();
        void onExpand();
+       void onHistoryDoubleClicked(QListWidgetItem* item);
        void onInterpolate();
        void onLoad();
+       void onReturnPressed();
        void onRun();
        void onSave();
+protected:
+       void toHistory(const QString& text);
 public:
        virtual bool say(ReLoggerLevel level, const QString& message);
 
index ffd4d4098649e61b6d5c40320bc7a69f8ad9811b..11038c7bfc4a1eae78f922775f4d4e4b177592f5 100644 (file)
     <height>624</height>
    </rect>
   </property>
+  <property name="minimumSize">
+   <size>
+    <width>125</width>
+    <height>0</height>
+   </size>
+  </property>
   <property name="windowTitle">
    <string>MainWindow</string>
   </property>
   <widget class="QWidget" name="centralWidget">
-   <layout class="QVBoxLayout" name="verticalLayout_5">
+   <layout class="QVBoxLayout" name="verticalLayout_9">
     <item>
      <widget class="QSplitter" name="splitter">
       <property name="orientation">
        <enum>Qt::Vertical</enum>
       </property>
-      <widget class="QWidget" name="layoutWidget">
-       <layout class="QVBoxLayout" name="verticalLayout">
-        <item>
-         <layout class="QHBoxLayout" name="horizontalLayout">
-          <item>
-           <widget class="QLabel" name="label">
-            <property name="text">
-             <string>Write command, try &quot;help;&quot;</string>
-            </property>
-           </widget>
-          </item>
-          <item>
-           <spacer name="horizontalSpacer">
-            <property name="orientation">
-             <enum>Qt::Horizontal</enum>
-            </property>
-            <property name="sizeHint" stdset="0">
-             <size>
-              <width>40</width>
-              <height>20</height>
-             </size>
-            </property>
-           </spacer>
-          </item>
-          <item>
-           <widget class="QPushButton" name="pushButtonRun">
-            <property name="minimumSize">
-             <size>
-              <width>125</width>
-              <height>0</height>
-             </size>
-            </property>
-            <property name="text">
-             <string>Run</string>
-            </property>
-           </widget>
-          </item>
-          <item>
-           <widget class="QPushButton" name="pushButtonSave">
-            <property name="minimumSize">
-             <size>
-              <width>125</width>
-              <height>0</height>
-             </size>
-            </property>
-            <property name="text">
-             <string>Save</string>
-            </property>
-           </widget>
-          </item>
-         </layout>
-        </item>
-        <item>
-         <widget class="QPlainTextEdit" name="plainTextEditPad"/>
-        </item>
-       </layout>
-      </widget>
-      <widget class="QTabWidget" name="tabWidgetResult">
+      <widget class="QTabWidget" name="tabWidgetInput">
        <property name="currentIndex">
-        <number>2</number>
+        <number>0</number>
        </property>
-       <widget class="QWidget" name="tabList">
+       <property name="documentMode">
+        <bool>false</bool>
+       </property>
+       <property name="movable">
+        <bool>false</bool>
+       </property>
+       <widget class="QWidget" name="tabCommandLine">
         <attribute name="title">
-         <string>Result as list</string>
+         <string>Command line</string>
         </attribute>
-        <layout class="QVBoxLayout" name="verticalLayout_3">
+        <layout class="QVBoxLayout" name="verticalLayout_8">
          <item>
-          <layout class="QVBoxLayout" name="verticalLayout_2">
+          <widget class="QLabel" name="label_6">
+           <property name="text">
+            <string>Type command and &lt;ENTER&gt;</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QLineEdit" name="lineEditCommand"/>
+         </item>
+         <item>
+          <layout class="QHBoxLayout" name="horizontalLayout_6">
            <item>
-            <layout class="QHBoxLayout" name="horizontalLayout_3">
-             <item>
-              <spacer name="horizontalSpacer_2">
-               <property name="orientation">
-                <enum>Qt::Horizontal</enum>
-               </property>
-               <property name="sizeHint" stdset="0">
-                <size>
-                 <width>40</width>
-                 <height>20</height>
-                </size>
-               </property>
-              </spacer>
-             </item>
-             <item>
-              <widget class="QPushButton" name="pushButtonClearList">
-               <property name="text">
-                <string>Clear</string>
-               </property>
-              </widget>
-             </item>
-            </layout>
+            <widget class="QLabel" name="label_7">
+             <property name="minimumSize">
+              <size>
+               <width>125</width>
+               <height>0</height>
+              </size>
+             </property>
+             <property name="text">
+              <string>History:</string>
+             </property>
+            </widget>
            </item>
            <item>
-            <widget class="QListWidget" name="listWidgetResult">
-             <property name="font">
-              <font>
-               <family>Monospace</family>
-              </font>
+            <spacer name="horizontalSpacer_6">
+             <property name="orientation">
+              <enum>Qt::Horizontal</enum>
+             </property>
+             <property name="sizeHint" stdset="0">
+              <size>
+               <width>40</width>
+               <height>20</height>
+              </size>
+             </property>
+            </spacer>
+           </item>
+           <item>
+            <widget class="QPushButton" name="pushButtonClearHistory">
+             <property name="minimumSize">
+              <size>
+               <width>125</width>
+               <height>0</height>
+              </size>
+             </property>
+             <property name="text">
+              <string>Clear</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
+         <item>
+          <widget class="QListWidget" name="listWidgetHistory"/>
+         </item>
         </layout>
        </widget>
-       <widget class="QWidget" name="tabEdit">
+       <widget class="QWidget" name="tab">
         <attribute name="title">
-         <string>Result as edit field</string>
+         <string>Command pad</string>
         </attribute>
-        <layout class="QVBoxLayout" name="verticalLayout_4">
+        <layout class="QVBoxLayout" name="verticalLayout">
          <item>
-          <layout class="QHBoxLayout" name="horizontalLayout_4">
+          <layout class="QHBoxLayout" name="horizontalLayout">
            <item>
-            <widget class="QPushButton" name="pushButton">
-             <property name="minimumSize">
-              <size>
-               <width>125</width>
-               <height>0</height>
-              </size>
-             </property>
+            <widget class="QLabel" name="label">
              <property name="text">
-              <string>From List</string>
+              <string>Write command, try &quot;help;&quot;</string>
              </property>
             </widget>
            </item>
            <item>
-            <spacer name="horizontalSpacer_3">
+            <spacer name="horizontalSpacer">
              <property name="orientation">
               <enum>Qt::Horizontal</enum>
              </property>
             </spacer>
            </item>
            <item>
-            <widget class="QPushButton" name="pushButtonClearEdit">
+            <widget class="QPushButton" name="pushButtonRun">
              <property name="minimumSize">
               <size>
                <width>125</width>
               </size>
              </property>
              <property name="text">
-              <string>Clear</string>
+              <string>Run</string>
+             </property>
+            </widget>
+           </item>
+           <item>
+            <widget class="QPushButton" name="pushButtonSave">
+             <property name="minimumSize">
+              <size>
+               <width>125</width>
+               <height>0</height>
+              </size>
+             </property>
+             <property name="text">
+              <string>Save</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
          <item>
-          <widget class="QPlainTextEdit" name="plainTextEditResult">
-           <property name="font">
-            <font>
-             <family>FreeMono</family>
-            </font>
-           </property>
-          </widget>
+          <widget class="QPlainTextEdit" name="plainTextEditPad"/>
          </item>
         </layout>
        </widget>
-       <widget class="QWidget" name="tabBuffers">
+       <widget class="QWidget" name="tab_3">
         <attribute name="title">
          <string>Buffers</string>
         </attribute>
-        <layout class="QVBoxLayout" name="verticalLayout_8">
+        <layout class="QVBoxLayout" name="verticalLayout_5">
          <item>
           <widget class="QSplitter" name="splitter_2">
            <property name="orientation">
              </item>
             </layout>
            </widget>
-           <widget class="QWidget" name="layoutWidget">
+           <widget class="QWidget" name="layoutWidget_2">
             <layout class="QVBoxLayout" name="verticalLayout_6">
              <item>
               <layout class="QHBoxLayout" name="horizontalLayout_2">
         </layout>
        </widget>
       </widget>
+      <widget class="QTabWidget" name="tabWidgetResult">
+       <property name="currentIndex">
+        <number>0</number>
+       </property>
+       <widget class="QWidget" name="tabList">
+        <attribute name="title">
+         <string>Result as list</string>
+        </attribute>
+        <layout class="QVBoxLayout" name="verticalLayout_3">
+         <item>
+          <layout class="QVBoxLayout" name="verticalLayout_2">
+           <item>
+            <layout class="QHBoxLayout" name="horizontalLayout_3">
+             <item>
+              <widget class="QLabel" name="label_8">
+               <property name="minimumSize">
+                <size>
+                 <width>125</width>
+                 <height>0</height>
+                </size>
+               </property>
+               <property name="text">
+                <string>Suche:</string>
+               </property>
+              </widget>
+             </item>
+             <item>
+              <widget class="QComboBox" name="comboBox">
+               <property name="minimumSize">
+                <size>
+                 <width>125</width>
+                 <height>0</height>
+                </size>
+               </property>
+               <property name="editable">
+                <bool>true</bool>
+               </property>
+              </widget>
+             </item>
+             <item>
+              <widget class="QPushButton" name="pushButtonPrevious">
+               <property name="maximumSize">
+                <size>
+                 <width>25</width>
+                 <height>16777215</height>
+                </size>
+               </property>
+               <property name="text">
+                <string>&lt;</string>
+               </property>
+              </widget>
+             </item>
+             <item>
+              <widget class="QPushButton" name="pushButton_2">
+               <property name="maximumSize">
+                <size>
+                 <width>25</width>
+                 <height>16777215</height>
+                </size>
+               </property>
+               <property name="text">
+                <string>&gt;</string>
+               </property>
+              </widget>
+             </item>
+             <item>
+              <spacer name="horizontalSpacer_2">
+               <property name="orientation">
+                <enum>Qt::Horizontal</enum>
+               </property>
+               <property name="sizeHint" stdset="0">
+                <size>
+                 <width>40</width>
+                 <height>20</height>
+                </size>
+               </property>
+              </spacer>
+             </item>
+             <item>
+              <widget class="QPushButton" name="pushButtonClearList">
+               <property name="minimumSize">
+                <size>
+                 <width>125</width>
+                 <height>0</height>
+                </size>
+               </property>
+               <property name="text">
+                <string>Clear</string>
+               </property>
+              </widget>
+             </item>
+            </layout>
+           </item>
+           <item>
+            <widget class="QListWidget" name="listWidgetResult">
+             <property name="font">
+              <font>
+               <family>Monospace</family>
+              </font>
+             </property>
+            </widget>
+           </item>
+          </layout>
+         </item>
+        </layout>
+       </widget>
+       <widget class="QWidget" name="tabEdit">
+        <attribute name="title">
+         <string>Result as edit field</string>
+        </attribute>
+        <layout class="QVBoxLayout" name="verticalLayout_4">
+         <item>
+          <layout class="QHBoxLayout" name="horizontalLayout_4">
+           <item>
+            <widget class="QPushButton" name="pushButton">
+             <property name="minimumSize">
+              <size>
+               <width>125</width>
+               <height>0</height>
+              </size>
+             </property>
+             <property name="text">
+              <string>From List</string>
+             </property>
+            </widget>
+           </item>
+           <item>
+            <spacer name="horizontalSpacer_3">
+             <property name="orientation">
+              <enum>Qt::Horizontal</enum>
+             </property>
+             <property name="sizeHint" stdset="0">
+              <size>
+               <width>40</width>
+               <height>20</height>
+              </size>
+             </property>
+            </spacer>
+           </item>
+           <item>
+            <widget class="QPushButton" name="pushButtonClearEdit">
+             <property name="minimumSize">
+              <size>
+               <width>125</width>
+               <height>0</height>
+              </size>
+             </property>
+             <property name="text">
+              <string>Clear</string>
+             </property>
+            </widget>
+           </item>
+          </layout>
+         </item>
+         <item>
+          <widget class="QPlainTextEdit" name="plainTextEditResult">
+           <property name="font">
+            <font>
+             <family>FreeMono</family>
+            </font>
+           </property>
+          </widget>
+         </item>
+        </layout>
+       </widget>
+      </widget>
      </widget>
     </item>
    </layout>
index 328cb8af2d474c406b8f8aff9f00cf0170a1a405..f82d458375aeb85b6aea13b3d4515419edecf5fb 100644 (file)
@@ -11,6 +11,7 @@
 
 #ifndef RECOMMAND_HPP
 #define RECOMMAND_HPP
+#include <QListWidgetItem>
 #include "base/rebase.hpp"
 #include "gui/regui.hpp"
 #include "CommandProcessor.hpp"
index 21da3bb967af7e0737a9c6ac855b9c01adbe1b0c..8efbe4f8c05d997ec0bc0d7579ac89b1a47db8e6 100644 (file)
@@ -22,6 +22,7 @@ SOURCES += main.cpp\
         ../../base/ReLogger.cpp \
         ../../base/ReRandomizer.cpp \
         ../../base/ReStringUtils.cpp \
+        ../../base/ReProgramArgs.cpp \
         ../../gui/ReStateStorage.cpp \
         ../../gui/ReGuiApplication.cpp \
         ../../gui/ReGuiValidator.cpp \
@@ -32,6 +33,6 @@ SOURCES += main.cpp\
 
 HEADERS  += mainwindow.hpp \
        CommandProcessor.hpp \
-    recommand.hpp
+       recommand.hpp
 
 FORMS    += mainwindow.ui
index eb79887f42695f10655832494ac0f86b752fb2df..fd18d99cc53d4b8b19fb92b5dd213210766188b3 100644 (file)
@@ -612,6 +612,28 @@ void ReProgramArgs::help(const char* message, bool issueLastError,
        }
 }
 
+/** Initializes the options from the program arguments.
+ *
+ * While arguments are preceded by an '-' they will be treated as options.
+ * The rest of arguments are stored for retrieving with <code>getArg()</code>.
+ *
+ * @param argc                 the count of program arguments (inclusive options)
+ * @param argv                 the argument vector
+ * @param hasProgram   <code>true</code>: the first argument is the program name
+ *
+ * @throws ReException
+ */
+void ReProgramArgs::init(const QStringList& args, bool hasProgram) {
+       QByteArrayList args2;
+       const char** args3 = new const char*[args.count()];
+       for (int ix = 0; ix < args.count(); ix++){
+               args2.append(I18N::s2b(args.at(ix)));
+               args3[ix] = args2.at(ix).constData();
+       }
+       init(args.count(), args3, hasProgram);
+       delete[] args3;
+}
+
 /** Initializes the options from the program arguments.
  *
  * While arguments are preceded by an '-' they will be treated as options.
index 4363a447959a1e12a84587b6a5b809f202e09ba2..955df4e1056c06a8615ac9c0ac55df2bd4042aad 100644 (file)
@@ -82,6 +82,7 @@ public:
        inline void init(int argc, char* argv[]){
                return init(argc, (const char**) argv, true);
        }
+       void init(const QStringList& args, bool hasProgram);
        const char* programName() const;
        void setLastError(const char* message);
        void setLastError(const QByteArray& message);