added history

This commit is contained in:
fluxgen 2002-11-12 19:20:31 +00:00
parent d1292fc599
commit 4897fc2288
3 changed files with 93 additions and 8 deletions

View file

@ -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 <unistd.h>
#include <iostream>
#include <fstream>
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<<m_runtext<<endl;
else
cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl;
}
m_end = true; // mark end of processing
}
bool FbRun::loadHistory(const char *filename) {
if (filename == 0)
return false;
ifstream infile(filename);
if (!infile) {
//even though we fail to load file, we should try save to it
m_history_file = filename;
return false;
}
// clear old history and load new one from file
m_history.clear();
// each line is a command
string line;
while (!infile.eof()) {
getline(infile, line);
if (line.size()) // don't add empty lines
m_history.push_back(line);
}
// set no current histor to display
m_current_history_item = m_history.size();
// set history file
m_history_file = filename;
return true;
}
bool FbRun::loadFont(const string &fontname) {
if (!m_font.load(fontname.c_str()))
return false;
@ -171,6 +208,16 @@ void FbRun::handleEvent(XEvent * const xev) {
} else if (! IsModifierKey(ks) && !IsCursorKey(ks)) {
m_runtext+=keychar[0]; // append character
redrawLabel();
} else if (IsCursorKey(ks)) {
switch (ks) {
case XK_Up:
prevHistoryItem();
break;
case XK_Down:
nextHistoryItem();
break;
}
redrawLabel();
}
} break;
case Expose:
@ -203,3 +250,22 @@ void FbRun::setNoMaximize() {
sh.min_height = height;
XSetWMNormalHints(m_display, m_win, &sh);
}
void FbRun::prevHistoryItem() {
if (m_current_history_item > 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];
}

View file

@ -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 <string>
#include <vector>
/**
Creates and managed a run window
TODO: a command history
*/
class FbRun: public FbTk::EventHandler<XEvent> {
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<std::string> m_history; ///< history list of commands
size_t m_current_history_item;
std::string m_history_file;
};
#endif // FBRUN_HH

View file

@ -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 <string>
#include <iostream>
@ -57,6 +58,7 @@ void showUsage(const char *progname) {
" -fg [color name] Foreground text color"<<endl<<
" -bg [color name] Background color"<<endl<<
" -a Antialias"<<endl<<
" -hf [history file] History file to load (default ~/.fluxbox/history)"<<endl<<
" -help Show this help"<<endl<<endl<<
"Example: fbrun -fg black -bg white -text xterm -title \"run xterm\""<<endl;
}
@ -73,7 +75,7 @@ int main(int argc, char **argv) {
string foreground("black"); // text color
string background("white"); // text background color
string display_name; // name of the display connection
string history_file("~/.fluxbox/fbrun_history"); // command history file
// parse arguments
for (int i=1; i<argc; i++) {
if (strcmp(argv[i], "-font") == 0 && i+1 < argc) {
@ -109,6 +111,8 @@ int main(int argc, char **argv) {
background = argv[i];
} else if (strcmp(argv[i], "-a") == 0) {
antialias = true;
} else if (strcmp(argv[i], "-hf") == 0 && i+1 < argc) {
history_file = argv[++i];
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
showUsage(argv[0]);
exit(0);
@ -159,6 +163,11 @@ int main(int argc, char **argv) {
fbrun.resize(width, height);
if (antialias)
fbrun.setAntialias(antialias);
// expand and load command history
string expanded_filename = StringUtil::expandFilename(history_file);
if (!fbrun.loadHistory(expanded_filename.c_str()))
cerr<<"FbRun Warning: Failed to load history file: "<<expanded_filename<<endl;
fbrun.setTitle(title);
fbrun.setText(text);
fbrun.show();