Skip to content

Commit 66429a6

Browse files
author
Baxter Grace
committed
add vi-like-bindings
1 parent f303550 commit 66429a6

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

Diff for: bullet/charDef.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
# Keyboard mapping macros
3+
# Keyboard mapping macros
44

55
LINE_BEGIN_KEY = 1
66
LINE_END_KEY = 5
@@ -32,3 +32,5 @@
3232
BACK_SPACE_CHAR = 8
3333
SPACE_CHAR = ord(' ')
3434
INTERRUPT_KEY = 3
35+
VIM_UP_KEY = ord('k')
36+
VIM_DOWN_KEY = ord('j')

Diff for: bullet/client.py

+46-40
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
# Reusable private utility class
1111
class myInput:
12-
def __init__(self,
13-
word_color: str = colors.foreground["default"],
14-
password: bool = False,
12+
def __init__(self,
13+
word_color: str = colors.foreground["default"],
14+
password: bool = False,
1515
hidden: str = '*'
1616
):
17-
''' Constructor for myInput
17+
''' Constructor for myInput
1818
Args:
1919
word_color: color of input characters.
2020
password: Whether input is password.
@@ -109,10 +109,10 @@ def input(self):
109109
@keyhandler.init
110110
class Bullet:
111111
def __init__(
112-
self,
112+
self,
113113
prompt: str = "",
114-
choices: list = [],
115-
bullet: str = "●",
114+
choices: list = [],
115+
bullet: str = "●",
116116
bullet_color: str = colors.foreground["default"],
117117
word_color: str = colors.foreground["default"],
118118
word_on_switch: str = colors.REVERSE,
@@ -151,12 +151,12 @@ def __init__(
151151
self.pad_right = pad_right
152152

153153
self.max_width = len(max(self.choices, key = len)) + self.pad_right
154-
154+
155155
def renderBullets(self):
156156
for i in range(len(self.choices)):
157157
self.printBullet(i)
158158
utils.forceWrite('\n')
159-
159+
160160
def printBullet(self, idx):
161161
utils.forceWrite(' ' * (self.indent + self.align))
162162
back_color = self.background_on_switch if idx == self.pos else self.background_color
@@ -170,6 +170,7 @@ def printBullet(self, idx):
170170
utils.moveCursorHead()
171171

172172
@keyhandler.register(ARROW_UP_KEY)
173+
@keyhandler.register(VIM_UP_KEY)
173174
def moveUp(self):
174175
if self.pos - 1 < 0:
175176
return
@@ -182,6 +183,7 @@ def moveUp(self):
182183
self.printBullet(self.pos)
183184

184185
@keyhandler.register(ARROW_DOWN_KEY)
186+
@keyhandler.register(VIM_DOWN_KEY)
185187
def moveDown(self):
186188
if self.pos + 1 >= len(self.choices):
187189
return
@@ -227,10 +229,10 @@ def launch(self, default = None):
227229
@keyhandler.init
228230
class Check:
229231
def __init__(
230-
self,
232+
self,
231233
prompt: str = "",
232-
choices: list = [],
233-
check: str = "√",
234+
choices: list = [],
235+
check: str = "√",
234236
check_color: str = colors.foreground["default"],
235237
check_on_switch: str = colors.REVERSE,
236238
word_color: str = colors.foreground["default"],
@@ -272,12 +274,12 @@ def __init__(
272274
self.pad_right = pad_right
273275

274276
self.max_width = len(max(self.choices, key = len)) + self.pad_right
275-
277+
276278
def renderRows(self):
277279
for i in range(len(self.choices)):
278280
self.printRow(i)
279281
utils.forceWrite('\n')
280-
282+
281283
def printRow(self, idx):
282284
utils.forceWrite(' ' * (self.indent + self.align))
283285
back_color = self.background_on_switch if idx == self.pos else self.background_color
@@ -297,6 +299,7 @@ def toggleRow(self):
297299
self.printRow(self.pos)
298300

299301
@keyhandler.register(ARROW_UP_KEY)
302+
@keyhandler.register(VIM_UP_KEY)
300303
def moveUp(self):
301304
if self.pos - 1 < 0:
302305
return
@@ -309,6 +312,7 @@ def moveUp(self):
309312
self.printRow(self.pos)
310313

311314
@keyhandler.register(ARROW_DOWN_KEY)
315+
@keyhandler.register(VIM_DOWN_KEY)
312316
def moveDown(self):
313317
if self.pos + 1 >= len(self.choices):
314318
return
@@ -357,8 +361,8 @@ def launch(self, default = []):
357361

358362
class YesNo:
359363
def __init__(
360-
self,
361-
prompt,
364+
self,
365+
prompt,
362366
indent = 0,
363367
word_color = colors.foreground["default"]
364368
):
@@ -376,7 +380,7 @@ def valid(self, ans):
376380
utils.forceWrite('\b' * len(ans))
377381
return False
378382
return True
379-
383+
380384
def launch(self, default = 'y'):
381385
default = default.lower()
382386
if not (default == 'y' or default == 'n'):
@@ -394,9 +398,9 @@ def launch(self, default = 'y'):
394398

395399
class Input:
396400
def __init__(
397-
self,
398-
prompt,
399-
indent = 0,
401+
self,
402+
prompt,
403+
indent = 0,
400404
word_color = colors.foreground["default"],
401405
strip = False,
402406
pattern = ""
@@ -408,7 +412,7 @@ def __init__(
408412
self.word_color = word_color
409413
self.strip = strip
410414
self.pattern = pattern
411-
415+
412416
def valid(self, ans):
413417
if not bool(re.match(self.pattern, ans)):
414418
utils.moveCursorUp(1)
@@ -443,10 +447,10 @@ def launch(self, default = ""):
443447

444448
class Password:
445449
def __init__(
446-
self,
447-
prompt,
448-
indent = 0,
449-
hidden = '*',
450+
self,
451+
prompt,
452+
indent = 0,
453+
hidden = '*',
450454
word_color = colors.foreground["default"]
451455
):
452456
self.indent = indent
@@ -455,16 +459,16 @@ def __init__(
455459
self.prompt = prompt
456460
self.hidden = hidden
457461
self.word_color = word_color
458-
462+
459463
def launch(self):
460464
utils.forceWrite(' ' * self.indent + self.prompt)
461465
return myInput(password = True, hidden = self.hidden, word_color = self.word_color).input()
462466

463467
class Numbers:
464468
def __init__(
465-
self,
466-
prompt,
467-
indent = 0,
469+
self,
470+
prompt,
471+
indent = 0,
468472
word_color = colors.foreground["default"],
469473
type = float
470474
):
@@ -474,7 +478,7 @@ def __init__(
474478
self.prompt = prompt
475479
self.word_color = word_color
476480
self.type = type
477-
481+
478482
def valid(self, ans):
479483
try:
480484
float(ans)
@@ -485,7 +489,7 @@ def valid(self, ans):
485489
utils.forceWrite(' ' * len(ans))
486490
utils.forceWrite('\b' * len(ans))
487491
return False
488-
492+
489493
def launch(self, default = None):
490494
if default is not None:
491495
try:
@@ -505,9 +509,9 @@ def launch(self, default = None):
505509

506510
class VerticalPrompt:
507511
def __init__(
508-
self,
509-
components,
510-
spacing = 1,
512+
self,
513+
components,
514+
spacing = 1,
511515
separator = "",
512516
separator_color = colors.foreground["default"]
513517
):
@@ -524,7 +528,7 @@ def __init__(
524528
def summarize(self):
525529
for prompt, answer in self.result:
526530
print(prompt, answer)
527-
531+
528532
def launch(self):
529533
for ui in self.components:
530534
self.result.append((ui.prompt, ui.launch()))
@@ -537,9 +541,9 @@ def launch(self):
537541
@keyhandler.init
538542
class ScrollBar:
539543
def __init__(
540-
self,
544+
self,
541545
prompt: str = "",
542-
choices: list = [],
546+
choices: list = [],
543547
pointer = "→",
544548
up_indicator: str = "↑",
545549
down_indicator: str = "↓",
@@ -592,7 +596,7 @@ def __init__(
592596
# scrollbar won't move if pos is in range [top, top + height)
593597
# scrollbar moves up if pos < top
594598
# scrollbar moves down if pos > top + height - 1
595-
599+
596600
def renderRows(self):
597601
self.printRow(self.top, indicator = self.up_indicator if self.top != 0 else '')
598602
utils.forceWrite('\n')
@@ -604,7 +608,7 @@ def renderRows(self):
604608

605609
self.printRow(i + 1, indicator= self.down_indicator if self.top + self.height != len(self.choices) else '')
606610
utils.forceWrite('\n')
607-
611+
608612
def printRow(self, idx, indicator=''):
609613
utils.forceWrite(' ' * (self.indent + self.align))
610614
back_color = self.background_on_switch if idx == self.pos else self.background_color
@@ -620,6 +624,7 @@ def printRow(self, idx, indicator=''):
620624
utils.moveCursorHead()
621625

622626
@keyhandler.register(ARROW_UP_KEY)
627+
@keyhandler.register(VIM_UP_KEY)
623628
def moveUp(self):
624629
if self.pos == self.top:
625630
if self.top == 0:
@@ -640,6 +645,7 @@ def moveUp(self):
640645
self.printRow(self.pos)
641646

642647
@keyhandler.register(ARROW_DOWN_KEY)
648+
@keyhandler.register(VIM_DOWN_KEY)
643649
def moveDown(self):
644650
if self.pos == self.top + self.height - 1:
645651
if self.top + self.height == len(self.choices):
@@ -688,7 +694,7 @@ def launch(self):
688694

689695
class SlidePrompt:
690696
def __init__(
691-
self,
697+
self,
692698
components
693699
):
694700
self.idx = 0

0 commit comments

Comments
 (0)