From 4897fc22888c52ba28af48e9bc57465a86ea3d2e Mon Sep 17 00:00:00 2001 From: fluxgen Date: Tue, 12 Nov 2002 19:20:31 +0000 Subject: added history --- util/fbrun/FbRun.cc | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++--- util/fbrun/FbRun.hh | 16 +++++++++--- util/fbrun/main.cc | 13 ++++++++-- 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/util/fbrun/FbRun.cc b/util/fbrun/FbRun.cc index 2042c17..6a0e6d8 100644 --- a/util/fbrun/FbRun.cc +++ b/util/fbrun/FbRun.cc @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: FbRun.cc,v 1.3 2002/11/12 17:10:13 fluxgen Exp $ +// $Id: FbRun.cc,v 1.4 2002/11/12 19:20:31 fluxgen Exp $ #include "FbRun.hh" @@ -32,6 +32,7 @@ #include #include +#include using namespace std; FbRun::FbRun(int x, int y, size_t width): @@ -40,7 +41,8 @@ m_win(None), m_display(BaseDisplay::getXDisplay()), m_bevel(4), m_gc(DefaultGC(m_display, DefaultScreen(m_display))), -m_end(false) { +m_end(false), +m_current_history_item(0) { createWindow(x, y, width + m_bevel, m_font.height()); } @@ -57,11 +59,46 @@ void FbRun::run(const std::string &command) { exit(0); //exit fork } - hide(); + hide(); // hide gui + + // save command history to file + if (m_runtext.size() != 0) { // no need to save empty command + // open file in append mode + ofstream outfile(m_history_file.c_str(), ios::app); + if (outfile) + outfile< 0 && m_history.size() > 0) + m_current_history_item--; + if (m_current_history_item < m_history.size()) + m_runtext = m_history[m_current_history_item]; +} + +void FbRun::nextHistoryItem() { + m_current_history_item++; + if (m_current_history_item >= m_history.size()) { + m_current_history_item = m_history.size(); + m_runtext = ""; + return; + } else + m_runtext = m_history[m_current_history_item]; + +} diff --git a/util/fbrun/FbRun.hh b/util/fbrun/FbRun.hh index ba7bf25..5c8a2dc 100644 --- a/util/fbrun/FbRun.hh +++ b/util/fbrun/FbRun.hh @@ -19,7 +19,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: FbRun.hh,v 1.2 2002/11/12 16:47:37 fluxgen Exp $ +// $Id: FbRun.hh,v 1.3 2002/11/12 19:20:31 fluxgen Exp $ #ifndef FBRUN_HH #define FBRUN_HH @@ -28,10 +28,10 @@ #include "Font.hh" #include +#include /** Creates and managed a run window - TODO: a command history */ class FbRun: public FbTk::EventHandler { public: @@ -54,9 +54,16 @@ public: const FbTk::Font &font() const { return m_font; } /// execute command and exit void run(const std::string &execstring); - /// is this object done? + /// is this application done? bool end() const { return m_end; } + /** + loads history file. + @return true on success, else false + */ + bool loadHistory(const char *filename); private: + void nextHistoryItem(); + void prevHistoryItem(); void drawString(int x, int y, const char *text, size_t len); void getSize(size_t &width, size_t &height); void createWindow(int x, int y, size_t width, size_t height); @@ -72,6 +79,9 @@ private: int m_bevel; ///< distance to window edge from font in pixels GC m_gc; bool m_end; + std::vector m_history; ///< history list of commands + size_t m_current_history_item; + std::string m_history_file; }; #endif // FBRUN_HH diff --git a/util/fbrun/main.cc b/util/fbrun/main.cc index 2237340..574dd76 100644 --- a/util/fbrun/main.cc +++ b/util/fbrun/main.cc @@ -19,10 +19,11 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: main.cc,v 1.2 2002/11/12 16:46:17 fluxgen Exp $ +// $Id: main.cc,v 1.3 2002/11/12 19:16:26 fluxgen Exp $ #include "FbRun.hh" #include "BaseDisplay.hh" +#include "StringUtil.hh" #include #include @@ -57,6 +58,7 @@ void showUsage(const char *progname) { " -fg [color name] Foreground text color"<