Skip to content

Commit

Permalink
add vi-like-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Baxter Grace committed Mar 6, 2019
1 parent f303550 commit 66429a6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
4 changes: 3 additions & 1 deletion bullet/charDef.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

# Keyboard mapping macros
# Keyboard mapping macros

LINE_BEGIN_KEY = 1
LINE_END_KEY = 5
Expand Down Expand Up @@ -32,3 +32,5 @@
BACK_SPACE_CHAR = 8
SPACE_CHAR = ord(' ')
INTERRUPT_KEY = 3
VIM_UP_KEY = ord('k')
VIM_DOWN_KEY = ord('j')
86 changes: 46 additions & 40 deletions bullet/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

# Reusable private utility class
class myInput:
def __init__(self,
word_color: str = colors.foreground["default"],
password: bool = False,
def __init__(self,
word_color: str = colors.foreground["default"],
password: bool = False,
hidden: str = '*'
):
''' Constructor for myInput
''' Constructor for myInput
Args:
word_color: color of input characters.
password: Whether input is password.
Expand Down Expand Up @@ -109,10 +109,10 @@ def input(self):
@keyhandler.init
class Bullet:
def __init__(
self,
self,
prompt: str = "",
choices: list = [],
bullet: str = "●",
choices: list = [],
bullet: str = "●",
bullet_color: str = colors.foreground["default"],
word_color: str = colors.foreground["default"],
word_on_switch: str = colors.REVERSE,
Expand Down Expand Up @@ -151,12 +151,12 @@ def __init__(
self.pad_right = pad_right

self.max_width = len(max(self.choices, key = len)) + self.pad_right

def renderBullets(self):
for i in range(len(self.choices)):
self.printBullet(i)
utils.forceWrite('\n')

def printBullet(self, idx):
utils.forceWrite(' ' * (self.indent + self.align))
back_color = self.background_on_switch if idx == self.pos else self.background_color
Expand All @@ -170,6 +170,7 @@ def printBullet(self, idx):
utils.moveCursorHead()

@keyhandler.register(ARROW_UP_KEY)
@keyhandler.register(VIM_UP_KEY)
def moveUp(self):
if self.pos - 1 < 0:
return
Expand All @@ -182,6 +183,7 @@ def moveUp(self):
self.printBullet(self.pos)

@keyhandler.register(ARROW_DOWN_KEY)
@keyhandler.register(VIM_DOWN_KEY)
def moveDown(self):
if self.pos + 1 >= len(self.choices):
return
Expand Down Expand Up @@ -227,10 +229,10 @@ def launch(self, default = None):
@keyhandler.init
class Check:
def __init__(
self,
self,
prompt: str = "",
choices: list = [],
check: str = "√",
choices: list = [],
check: str = "√",
check_color: str = colors.foreground["default"],
check_on_switch: str = colors.REVERSE,
word_color: str = colors.foreground["default"],
Expand Down Expand Up @@ -272,12 +274,12 @@ def __init__(
self.pad_right = pad_right

self.max_width = len(max(self.choices, key = len)) + self.pad_right

def renderRows(self):
for i in range(len(self.choices)):
self.printRow(i)
utils.forceWrite('\n')

def printRow(self, idx):
utils.forceWrite(' ' * (self.indent + self.align))
back_color = self.background_on_switch if idx == self.pos else self.background_color
Expand All @@ -297,6 +299,7 @@ def toggleRow(self):
self.printRow(self.pos)

@keyhandler.register(ARROW_UP_KEY)
@keyhandler.register(VIM_UP_KEY)
def moveUp(self):
if self.pos - 1 < 0:
return
Expand All @@ -309,6 +312,7 @@ def moveUp(self):
self.printRow(self.pos)

@keyhandler.register(ARROW_DOWN_KEY)
@keyhandler.register(VIM_DOWN_KEY)
def moveDown(self):
if self.pos + 1 >= len(self.choices):
return
Expand Down Expand Up @@ -357,8 +361,8 @@ def launch(self, default = []):

class YesNo:
def __init__(
self,
prompt,
self,
prompt,
indent = 0,
word_color = colors.foreground["default"]
):
Expand All @@ -376,7 +380,7 @@ def valid(self, ans):
utils.forceWrite('\b' * len(ans))
return False
return True

def launch(self, default = 'y'):
default = default.lower()
if not (default == 'y' or default == 'n'):
Expand All @@ -394,9 +398,9 @@ def launch(self, default = 'y'):

class Input:
def __init__(
self,
prompt,
indent = 0,
self,
prompt,
indent = 0,
word_color = colors.foreground["default"],
strip = False,
pattern = ""
Expand All @@ -408,7 +412,7 @@ def __init__(
self.word_color = word_color
self.strip = strip
self.pattern = pattern

def valid(self, ans):
if not bool(re.match(self.pattern, ans)):
utils.moveCursorUp(1)
Expand Down Expand Up @@ -443,10 +447,10 @@ def launch(self, default = ""):

class Password:
def __init__(
self,
prompt,
indent = 0,
hidden = '*',
self,
prompt,
indent = 0,
hidden = '*',
word_color = colors.foreground["default"]
):
self.indent = indent
Expand All @@ -455,16 +459,16 @@ def __init__(
self.prompt = prompt
self.hidden = hidden
self.word_color = word_color

def launch(self):
utils.forceWrite(' ' * self.indent + self.prompt)
return myInput(password = True, hidden = self.hidden, word_color = self.word_color).input()

class Numbers:
def __init__(
self,
prompt,
indent = 0,
self,
prompt,
indent = 0,
word_color = colors.foreground["default"],
type = float
):
Expand All @@ -474,7 +478,7 @@ def __init__(
self.prompt = prompt
self.word_color = word_color
self.type = type

def valid(self, ans):
try:
float(ans)
Expand All @@ -485,7 +489,7 @@ def valid(self, ans):
utils.forceWrite(' ' * len(ans))
utils.forceWrite('\b' * len(ans))
return False

def launch(self, default = None):
if default is not None:
try:
Expand All @@ -505,9 +509,9 @@ def launch(self, default = None):

class VerticalPrompt:
def __init__(
self,
components,
spacing = 1,
self,
components,
spacing = 1,
separator = "",
separator_color = colors.foreground["default"]
):
Expand All @@ -524,7 +528,7 @@ def __init__(
def summarize(self):
for prompt, answer in self.result:
print(prompt, answer)

def launch(self):
for ui in self.components:
self.result.append((ui.prompt, ui.launch()))
Expand All @@ -537,9 +541,9 @@ def launch(self):
@keyhandler.init
class ScrollBar:
def __init__(
self,
self,
prompt: str = "",
choices: list = [],
choices: list = [],
pointer = "→",
up_indicator: str = "↑",
down_indicator: str = "↓",
Expand Down Expand Up @@ -592,7 +596,7 @@ def __init__(
# scrollbar won't move if pos is in range [top, top + height)
# scrollbar moves up if pos < top
# scrollbar moves down if pos > top + height - 1

def renderRows(self):
self.printRow(self.top, indicator = self.up_indicator if self.top != 0 else '')
utils.forceWrite('\n')
Expand All @@ -604,7 +608,7 @@ def renderRows(self):

self.printRow(i + 1, indicator= self.down_indicator if self.top + self.height != len(self.choices) else '')
utils.forceWrite('\n')

def printRow(self, idx, indicator=''):
utils.forceWrite(' ' * (self.indent + self.align))
back_color = self.background_on_switch if idx == self.pos else self.background_color
Expand All @@ -620,6 +624,7 @@ def printRow(self, idx, indicator=''):
utils.moveCursorHead()

@keyhandler.register(ARROW_UP_KEY)
@keyhandler.register(VIM_UP_KEY)
def moveUp(self):
if self.pos == self.top:
if self.top == 0:
Expand All @@ -640,6 +645,7 @@ def moveUp(self):
self.printRow(self.pos)

@keyhandler.register(ARROW_DOWN_KEY)
@keyhandler.register(VIM_DOWN_KEY)
def moveDown(self):
if self.pos == self.top + self.height - 1:
if self.top + self.height == len(self.choices):
Expand Down Expand Up @@ -688,7 +694,7 @@ def launch(self):

class SlidePrompt:
def __init__(
self,
self,
components
):
self.idx = 0
Expand Down

0 comments on commit 66429a6

Please sign in to comment.