diff options
author | fluxgen <fluxgen> | 2002-11-12 19:20:31 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2002-11-12 19:20:31 (GMT) |
commit | 4897fc22888c52ba28af48e9bc57465a86ea3d2e (patch) | |
tree | a20ea83cc7768ac187299bddb729d6d9a873fdbd /util/fbrun | |
parent | d1292fc5999ec46c2bab9645675bdda11b2d9b04 (diff) | |
download | fluxbox_paul-4897fc22888c52ba28af48e9bc57465a86ea3d2e.zip fluxbox_paul-4897fc22888c52ba28af48e9bc57465a86ea3d2e.tar.bz2 |
added history
Diffstat (limited to 'util/fbrun')
-rw-r--r-- | util/fbrun/FbRun.cc | 72 | ||||
-rw-r--r-- | util/fbrun/FbRun.hh | 16 | ||||
-rw-r--r-- | 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 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | // $Id: FbRun.cc,v 1.3 2002/11/12 17:10:13 fluxgen Exp $ | 22 | // $Id: FbRun.cc,v 1.4 2002/11/12 19:20:31 fluxgen Exp $ |
23 | 23 | ||
24 | #include "FbRun.hh" | 24 | #include "FbRun.hh" |
25 | 25 | ||
@@ -32,6 +32,7 @@ | |||
32 | #include <unistd.h> | 32 | #include <unistd.h> |
33 | 33 | ||
34 | #include <iostream> | 34 | #include <iostream> |
35 | #include <fstream> | ||
35 | 36 | ||
36 | using namespace std; | 37 | using namespace std; |
37 | FbRun::FbRun(int x, int y, size_t width): | 38 | FbRun::FbRun(int x, int y, size_t width): |
@@ -40,7 +41,8 @@ m_win(None), | |||
40 | m_display(BaseDisplay::getXDisplay()), | 41 | m_display(BaseDisplay::getXDisplay()), |
41 | m_bevel(4), | 42 | m_bevel(4), |
42 | m_gc(DefaultGC(m_display, DefaultScreen(m_display))), | 43 | m_gc(DefaultGC(m_display, DefaultScreen(m_display))), |
43 | m_end(false) { | 44 | m_end(false), |
45 | m_current_history_item(0) { | ||
44 | createWindow(x, y, width + m_bevel, m_font.height()); | 46 | createWindow(x, y, width + m_bevel, m_font.height()); |
45 | } | 47 | } |
46 | 48 | ||
@@ -57,11 +59,46 @@ void FbRun::run(const std::string &command) { | |||
57 | exit(0); //exit fork | 59 | exit(0); //exit fork |
58 | } | 60 | } |
59 | 61 | ||
60 | hide(); | 62 | hide(); // hide gui |
63 | |||
64 | // save command history to file | ||
65 | if (m_runtext.size() != 0) { // no need to save empty command | ||
66 | // open file in append mode | ||
67 | ofstream outfile(m_history_file.c_str(), ios::app); | ||
68 | if (outfile) | ||
69 | outfile<<m_runtext<<endl; | ||
70 | else | ||
71 | cerr<<"FbRun Warning: Can't write command history to file: "<<m_history_file<<endl; | ||
72 | } | ||
61 | 73 | ||
62 | m_end = true; // mark end of processing | 74 | m_end = true; // mark end of processing |
63 | } | 75 | } |
64 | 76 | ||
77 | bool FbRun::loadHistory(const char *filename) { | ||
78 | if (filename == 0) | ||
79 | return false; | ||
80 | ifstream infile(filename); | ||
81 | if (!infile) { | ||
82 | //even though we fail to load file, we should try save to it | ||
83 | m_history_file = filename; | ||
84 | return false; | ||
85 | } | ||
86 | // clear old history and load new one from file | ||
87 | m_history.clear(); | ||
88 | // each line is a command | ||
89 | string line; | ||
90 | while (!infile.eof()) { | ||
91 | getline(infile, line); | ||
92 | if (line.size()) // don't add empty lines | ||
93 | m_history.push_back(line); | ||
94 | } | ||
95 | // set no current histor to display | ||
96 | m_current_history_item = m_history.size(); | ||
97 | // set history file | ||
98 | m_history_file = filename; | ||
99 | return true; | ||
100 | } | ||
101 | |||
65 | bool FbRun::loadFont(const string &fontname) { | 102 | bool FbRun::loadFont(const string &fontname) { |
66 | if (!m_font.load(fontname.c_str())) | 103 | if (!m_font.load(fontname.c_str())) |
67 | return false; | 104 | return false; |
@@ -171,6 +208,16 @@ void FbRun::handleEvent(XEvent * const xev) { | |||
171 | } else if (! IsModifierKey(ks) && !IsCursorKey(ks)) { | 208 | } else if (! IsModifierKey(ks) && !IsCursorKey(ks)) { |
172 | m_runtext+=keychar[0]; // append character | 209 | m_runtext+=keychar[0]; // append character |
173 | redrawLabel(); | 210 | redrawLabel(); |
211 | } else if (IsCursorKey(ks)) { | ||
212 | switch (ks) { | ||
213 | case XK_Up: | ||
214 | prevHistoryItem(); | ||
215 | break; | ||
216 | case XK_Down: | ||
217 | nextHistoryItem(); | ||
218 | break; | ||
219 | } | ||
220 | redrawLabel(); | ||
174 | } | 221 | } |
175 | } break; | 222 | } break; |
176 | case Expose: | 223 | case Expose: |
@@ -203,3 +250,22 @@ void FbRun::setNoMaximize() { | |||
203 | sh.min_height = height; | 250 | sh.min_height = height; |
204 | XSetWMNormalHints(m_display, m_win, &sh); | 251 | XSetWMNormalHints(m_display, m_win, &sh); |
205 | } | 252 | } |
253 | |||
254 | void FbRun::prevHistoryItem() { | ||
255 | |||
256 | if (m_current_history_item > 0 && m_history.size() > 0) | ||
257 | m_current_history_item--; | ||
258 | if (m_current_history_item < m_history.size()) | ||
259 | m_runtext = m_history[m_current_history_item]; | ||
260 | } | ||
261 | |||
262 | void FbRun::nextHistoryItem() { | ||
263 | m_current_history_item++; | ||
264 | if (m_current_history_item >= m_history.size()) { | ||
265 | m_current_history_item = m_history.size(); | ||
266 | m_runtext = ""; | ||
267 | return; | ||
268 | } else | ||
269 | m_runtext = m_history[m_current_history_item]; | ||
270 | |||
271 | } | ||
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 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | // $Id: FbRun.hh,v 1.2 2002/11/12 16:47:37 fluxgen Exp $ | 22 | // $Id: FbRun.hh,v 1.3 2002/11/12 19:20:31 fluxgen Exp $ |
23 | 23 | ||
24 | #ifndef FBRUN_HH | 24 | #ifndef FBRUN_HH |
25 | #define FBRUN_HH | 25 | #define FBRUN_HH |
@@ -28,10 +28,10 @@ | |||
28 | #include "Font.hh" | 28 | #include "Font.hh" |
29 | 29 | ||
30 | #include <string> | 30 | #include <string> |
31 | #include <vector> | ||
31 | 32 | ||
32 | /** | 33 | /** |
33 | Creates and managed a run window | 34 | Creates and managed a run window |
34 | TODO: a command history | ||
35 | */ | 35 | */ |
36 | class FbRun: public FbTk::EventHandler<XEvent> { | 36 | class FbRun: public FbTk::EventHandler<XEvent> { |
37 | public: | 37 | public: |
@@ -54,9 +54,16 @@ public: | |||
54 | const FbTk::Font &font() const { return m_font; } | 54 | const FbTk::Font &font() const { return m_font; } |
55 | /// execute command and exit | 55 | /// execute command and exit |
56 | void run(const std::string &execstring); | 56 | void run(const std::string &execstring); |
57 | /// is this object done? | 57 | /// is this application done? |
58 | bool end() const { return m_end; } | 58 | bool end() const { return m_end; } |
59 | /** | ||
60 | loads history file. | ||
61 | @return true on success, else false | ||
62 | */ | ||
63 | bool loadHistory(const char *filename); | ||
59 | private: | 64 | private: |
65 | void nextHistoryItem(); | ||
66 | void prevHistoryItem(); | ||
60 | void drawString(int x, int y, const char *text, size_t len); | 67 | void drawString(int x, int y, const char *text, size_t len); |
61 | void getSize(size_t &width, size_t &height); | 68 | void getSize(size_t &width, size_t &height); |
62 | void createWindow(int x, int y, size_t width, size_t height); | 69 | void createWindow(int x, int y, size_t width, size_t height); |
@@ -72,6 +79,9 @@ private: | |||
72 | int m_bevel; ///< distance to window edge from font in pixels | 79 | int m_bevel; ///< distance to window edge from font in pixels |
73 | GC m_gc; | 80 | GC m_gc; |
74 | bool m_end; | 81 | bool m_end; |
82 | std::vector<std::string> m_history; ///< history list of commands | ||
83 | size_t m_current_history_item; | ||
84 | std::string m_history_file; | ||
75 | }; | 85 | }; |
76 | 86 | ||
77 | #endif // FBRUN_HH | 87 | #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 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | // $Id: main.cc,v 1.2 2002/11/12 16:46:17 fluxgen Exp $ | 22 | // $Id: main.cc,v 1.3 2002/11/12 19:16:26 fluxgen Exp $ |
23 | 23 | ||
24 | #include "FbRun.hh" | 24 | #include "FbRun.hh" |
25 | #include "BaseDisplay.hh" | 25 | #include "BaseDisplay.hh" |
26 | #include "StringUtil.hh" | ||
26 | 27 | ||
27 | #include <string> | 28 | #include <string> |
28 | #include <iostream> | 29 | #include <iostream> |
@@ -57,6 +58,7 @@ void showUsage(const char *progname) { | |||
57 | " -fg [color name] Foreground text color"<<endl<< | 58 | " -fg [color name] Foreground text color"<<endl<< |
58 | " -bg [color name] Background color"<<endl<< | 59 | " -bg [color name] Background color"<<endl<< |
59 | " -a Antialias"<<endl<< | 60 | " -a Antialias"<<endl<< |
61 | " -hf [history file] History file to load (default ~/.fluxbox/history)"<<endl<< | ||
60 | " -help Show this help"<<endl<<endl<< | 62 | " -help Show this help"<<endl<<endl<< |
61 | "Example: fbrun -fg black -bg white -text xterm -title \"run xterm\""<<endl; | 63 | "Example: fbrun -fg black -bg white -text xterm -title \"run xterm\""<<endl; |
62 | } | 64 | } |
@@ -73,7 +75,7 @@ int main(int argc, char **argv) { | |||
73 | string foreground("black"); // text color | 75 | string foreground("black"); // text color |
74 | string background("white"); // text background color | 76 | string background("white"); // text background color |
75 | string display_name; // name of the display connection | 77 | string display_name; // name of the display connection |
76 | 78 | string history_file("~/.fluxbox/fbrun_history"); // command history file | |
77 | // parse arguments | 79 | // parse arguments |
78 | for (int i=1; i<argc; i++) { | 80 | for (int i=1; i<argc; i++) { |
79 | if (strcmp(argv[i], "-font") == 0 && i+1 < argc) { | 81 | if (strcmp(argv[i], "-font") == 0 && i+1 < argc) { |
@@ -109,6 +111,8 @@ int main(int argc, char **argv) { | |||
109 | background = argv[i]; | 111 | background = argv[i]; |
110 | } else if (strcmp(argv[i], "-a") == 0) { | 112 | } else if (strcmp(argv[i], "-a") == 0) { |
111 | antialias = true; | 113 | antialias = true; |
114 | } else if (strcmp(argv[i], "-hf") == 0 && i+1 < argc) { | ||
115 | history_file = argv[++i]; | ||
112 | } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) { | 116 | } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) { |
113 | showUsage(argv[0]); | 117 | showUsage(argv[0]); |
114 | exit(0); | 118 | exit(0); |
@@ -159,6 +163,11 @@ int main(int argc, char **argv) { | |||
159 | fbrun.resize(width, height); | 163 | fbrun.resize(width, height); |
160 | if (antialias) | 164 | if (antialias) |
161 | fbrun.setAntialias(antialias); | 165 | fbrun.setAntialias(antialias); |
166 | // expand and load command history | ||
167 | string expanded_filename = StringUtil::expandFilename(history_file); | ||
168 | if (!fbrun.loadHistory(expanded_filename.c_str())) | ||
169 | cerr<<"FbRun Warning: Failed to load history file: "<<expanded_filename<<endl; | ||
170 | |||
162 | fbrun.setTitle(title); | 171 | fbrun.setTitle(title); |
163 | fbrun.setText(text); | 172 | fbrun.setText(text); |
164 | fbrun.show(); | 173 | fbrun.show(); |