-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommand.cpp
More file actions
156 lines (139 loc) · 5.88 KB
/
Command.cpp
File metadata and controls
156 lines (139 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<seq_view.h>
namespace SeqView {
set<int> seqSetCommands(ssc, ssc + 16);
Com ssc[] = {SCROLLUP, SCROLLDOWN, SCROLLRIGHT, SCROLLLEFT,
SCROLLMODE, COMPAREMODE, GOTO, GOTOEND,
COMPARETOGGLE, GOTOBEGIN, SCROLLTOP,
SCROLLBOTTOM, NAMEWIDTH, DISPLAYMODE, SETFRAME,
COMPARE};
Command getCommand() {
// Get the command followed by the parameters
int param = 1;
int h, w, h_, w_;
string param_buffer = "";
string command_buffer = "";
bool on_param = true;
bool on_command = false;
getmaxyx(stdscr, h, w);
h_ = h; w_ = w;
// need a map/tree that matches commands to characters
while(TRUE) {
wchar_t ch = getch();
//char ch = getch();
// ESC key clears the buffers
if(ch == 27) {
param = 1;
param_buffer = "";
command_buffer = "";
on_param = true;
on_command = false;
continue;
}
// if a non-number was pushed, convert the
// param_buffer to int and look for the command
if(ch > 57 || ch < 48) {
on_command = true;
on_param = false;
if(param_buffer.length())
param = atoi(param_buffer.c_str());
}
// Start by looking for numbers
if(on_param) {
param_buffer += ch;
}
// if we are looking for a command
// check for special keyboard keys first - because those
// often have numbers > than what char can hold
if(on_command) {
if(ch == KEY_DOWN) {
return Command(SCROLLDOWN, param);
} else if(ch == KEY_UP) {
return Command(SCROLLUP, param);
} else if(ch == KEY_LEFT) {
return Command(SCROLLLEFT, param);
} else if(ch == KEY_RIGHT) {
return Command(SCROLLRIGHT, param);
} else if(ch == KEY_HOME) {
return Command(GOTOBEGIN, param);
} else if(ch == KEY_END) {
return Command(GOTOEND, param);
} else if(ch == KEY_RESIZE) {
return Command(RESIZE, param);
} else if (ch == KEY_SLEFT) {
return Command(NAMEWIDTH, -1);
} else if (ch == KEY_SRIGHT) {
return Command(NAMEWIDTH, 0);
}
// if it makes sense to treat the command as a letter
if(ch < 256) {
command_buffer += ch;
if(!command_buffer.compare("q"))
return Command(QUIT, param);
if(!command_buffer.compare("aq"))
return Command(ALLQUIT, param);
if(!command_buffer.compare("s"))
return Command(SCROLLMODE, param);
if(!command_buffer.compare("j"))
return Command(SCROLLDOWN, param);
if(!command_buffer.compare("J"))
return Command(SCROLLBOTTOM, param);
if(!command_buffer.compare("k"))
return Command(SCROLLUP, param);
if(!command_buffer.compare("K"))
return Command(SCROLLTOP, param);
if(!command_buffer.compare("h"))
return Command(SCROLLLEFT, param);
if(!command_buffer.compare("l"))
return Command(SCROLLRIGHT, param);
if(!command_buffer.compare("H"))
return Command(SHOWHELP, param);
if(!command_buffer.compare("g"))
return Command(GOTOBEGIN, param);
if(!command_buffer.compare("w")) {
if(param_buffer.length() == 0) {
param = 0;
}
return Command(CHANGEFOCUS, param);
}
if(!command_buffer.compare("W")) {
if(param_buffer.length() == 0) {
param = 0;
}
return Command(CHANGEFOCUSREV, param);
}
if(!command_buffer.compare("G")) {
if(param_buffer.length()) {
return Command(GOTO, param);
} else {
return Command(GOTOEND, param);
}
}
if(!command_buffer.compare("d"))
return Command(DISPLAYMODE, param);
if(!command_buffer.compare("f"))
return Command(SETFRAME, param);
if(!command_buffer.compare("n"))
return Command(NAMEWIDTH, param);
if(!command_buffer.compare(";"))
return Command(SPECIAL, param);
if(!command_buffer.compare("c"))
return Command(COMPARETOGGLE, param);
}
// if window size has changed, but not caught by KEY_RESIZE
getmaxyx(stdscr, h, w);
if(h != h_ || w != w_) {
h_ = h; w_ = w;
return Command(RESIZE, param);
}
// if no matches and more than two letters, clear the buffers
if(command_buffer.size() > 2) {
param_buffer = "";
command_buffer = "";
on_command = false;
on_param = true;
param = 1;
}
}
}
}
}