1 /***************************************************************************
2 rkcommandhistory - description
3 -------------------
4 begin : Thu Sep 27
5 copyright : (C) 2012 by Thomas Friedrichsmeier
6 email : thomas.friedrichsmeier@kdemail.net
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "rkcommandhistory.h"
19 #include "../settings/rksettingsmoduleconsole.h"
20
21 #include "../debug.h"
22
RKCommandHistory(bool allow_empty,bool allow_dupes)23 RKCommandHistory::RKCommandHistory (bool allow_empty, bool allow_dupes) {
24 RK_TRACE (MISC);
25
26 current_position = 0;
27 RKCommandHistory::allow_empty = allow_empty;
28 RKCommandHistory::allow_dupes = allow_dupes;
29 }
30
~RKCommandHistory()31 RKCommandHistory::~RKCommandHistory () {
32 RK_TRACE (MISC);
33 }
34
up(bool context_sensitive,const QString & current_context)35 bool RKCommandHistory::up (bool context_sensitive, const QString& current_context) {
36 RK_TRACE (MISC);
37
38 int new_pos;
39 if (context_sensitive) {
40 QString filter = current_context;
41 if (filter == current ()) { // user is just browsing through the list. Use previous filter string
42 filter = _current_context;
43 } else { // line appears edited. Use as new filter
44 _current_context = filter;
45 }
46
47 new_pos = -1;
48 for (int i = qMin (current_position - 1, history.size () - 1); i >= 0; --i) {
49 if (history[i].startsWith (filter)) {
50 new_pos = i;
51 break;
52 }
53 }
54 } else {
55 new_pos = current_position - 1;
56 }
57
58 if (new_pos >= 0) {
59 if (current_position == (history.size ())) { // on last line (and going up)? Auto append current context
60 editing_line = current_context;
61 }
62 current_position = new_pos;
63 return true;
64 }
65 return false;
66 }
67
down(bool context_sensitive,const QString & current_context)68 bool RKCommandHistory::down (bool context_sensitive, const QString& current_context) {
69 RK_TRACE (MISC);
70
71 int new_pos;
72 if (context_sensitive) {
73 QString filter = current_context;
74 if (filter == current ()) { // user is just browsing through the list. Use previous filter string
75 filter = _current_context;
76 } else { // line appears edited. Use as new filter
77 _current_context = filter;
78 }
79
80 new_pos = history.size ();
81 for (int i = current_position + 1; i < history.size (); ++i) {
82 if (history[i].startsWith (filter)) {
83 new_pos = i;
84 break;
85 }
86 }
87 } else {
88 new_pos = current_position + 1;
89 }
90
91 if (new_pos <= history.size ()) {
92 current_position = new_pos;
93 return true;
94 }
95 return false;
96 }
97
trim()98 void RKCommandHistory::trim () {
99 if (RKSettingsModuleConsole::maxHistoryLength ()) {
100 for (uint ui = history.size (); ui > RKSettingsModuleConsole::maxHistoryLength (); --ui) {
101 history.pop_front ();
102 }
103 }
104 }
105
106
append(const QString & new_line)107 void RKCommandHistory::append (const QString& new_line) {
108 RK_TRACE (MISC);
109
110 if (allow_empty || (!new_line.isEmpty ())) {
111 if (allow_dupes || (new_line != history.value (history.size () - 1))) {
112 history.append (new_line);
113 trim ();
114 }
115 }
116 goToEnd ();
117 }
118
setHistory(const QStringList & _history,bool append)119 void RKCommandHistory::setHistory (const QStringList& _history, bool append) {
120 RK_TRACE (MISC);
121
122 if (append) history.append (_history);
123 else history = _history;
124
125 trim ();
126 goToEnd ();
127 }
128