-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
158 lines (105 loc) · 3.95 KB
/
index.js
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
var conf = require('nconf'),
tty = require('tty'),
HabitAPI = require('./lib/habitapi.js'),
view = require('./lib/charmview.js'),
keypress = require('keypress');
conf.argv().file({file: __dirname + "/config.json"}).defaults({
'APIURL': 'https://habitrpg.com/api/v1',
'APIUSER': '001122',
'APITOKEN': 'ABCABC',
'KEYBINDING': 'vi'
});
//process.openStdin();
if (typeof process.stdin.fd === 'number' && tty.isatty(process.stdin.fd)) {
if (process.stdin.setRawMode) {
process.stdin.setRawMode(true);
}
else tty.setRawMode(true);
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
// Keypress doesn't detect the names some keys, so fall
// back to plain sequence in those cases (ch).
view.InputWindow.setKeybinding(conf.get("KEYBINDING"));
keypress(process.stdin);
process.stdin.on('keypress', function(ch, key){
if (key && key.ctrl && key.name == 'c') { // exit on C-c
process.exit(0);
}
else if(key) {
keyPress(key);
} else {
key = ch;
keyPress(key);
}
});
function keyPress(key){
view.LogWindow.keyPress(key);
var kb = view.InputWindow.getKeybinding();
if(view.InputWindow.getMode() == 'normal'){
if(kb == 'vi') {
if(key == '~' || key == '`'){ // ~, ` toggle debug window
view.LogWindow.toggle();
} if(key.name == 'j'){ // j, up arrow: move up
view.TaskList.moveCursor(1);
} else if(key.name == 'k'){ // k, down arrow : move down
view.TaskList.moveCursor(-1);
} else if(key.name == 'J'){ // J move one section down
} else if(key.name == 'K'){ // K move one section up
} else if(key.name == 'd' || key == '-'){ // d, minus to decrement whatever you are on
view.TaskList.decrementHabit();
} else if(key.name == 'space' || key.name == 'x'){ // update habit
view.TaskList.checkItem();
} else if(key.name == 'r'){ // r, manual refresh
HabitAPI.refresh();
} else if(key == '?'){
view.HelpWindow.toggle();
} else if(key == ":"){
view.InputWindow.setMode('command');
} else if(key == "/"){
view.InputWindow.setMode('search');
}
}
if(kb == 'emacs'){
if(key == '`' || key == '~' || key.name == 'f1'){ // f1, toggle debug window
view.LogWindow.toggle();
}
if(key.name == 'n'){ // n or C-n
view.TaskList.moveCursor(1);
} else if(key.name == 'p'){ // p or C-p
view.TaskList.moveCursor(-1);
} else if(key.ctrl && key.name == 'd'){ // C-d to decrement whatever you are on
view.TaskList.decrementHabit();
} else if(key.ctrl && (key.name == 'space' || key.name == '`')){ // C-SPC, C-`
view.TaskList.checkItem();
} else if(key.name == 'r'){ // r, manual refresh
HabitAPI.refresh();
} else if(key == '?'){ // ? for help
view.HelpWindow.toggle();
} else if(key.meta && key.name == 'x'){ // M-x
view.InputWindow.setMode('command');
} else if(key.ctrl && key.name == 's'){ // C-s
view.InputWindow.setMode('search');
}
}
}
if(view.InputWindow.getMode() != 'normal'){
// we are not in normal mode
view.InputWindow.keyPress(key);
}
}
HabitAPI.onDataChange(function(){
view.refreshStatsView();
view.refreshStatusBar();
view.TaskList.refresh();
});
HabitAPI.onConnectedChange(function(){
view.refreshStatusBar();
});
HabitAPI.onLog(function(text){
view.LogWindow.log(text);
});
HabitAPI.onUnsavedChange(function(){
view.refreshStatusBar();
});
HabitAPI.init(conf.get("APIURL"),conf.get("APIUSER"),conf.get("APITOKEN"));