Skip to content

Commit

Permalink
Fix issue #5, prevent entry beyond width
Browse files Browse the repository at this point in the history
  • Loading branch information
genotrance committed Sep 10, 2018
1 parent 06b5c2f commit 04ab51e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 52 deletions.
5 changes: 3 additions & 2 deletions src/snip/actions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,15 @@ proc addNewline*() =
redraw()

proc addChar*() =
if BUFLINE.len() == WIDTH-MARGIN-1:
return

if COL == BUFLINE.len():
BUFLINE &= LASTCHAR
elif COL < BUFLINE.len():
let br = BUFLINE.substr(COL)
BUFLINE = BUFLINE.substr(0, COL-1) & LASTCHAR & br
COL += 1
if COL > WIDTH-MARGIN-1:
COL = WIDTH-MARGIN-1
redrawLine()

proc addSpace() =
Expand Down
116 changes: 66 additions & 50 deletions src/snip/key.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ./globals
import ./keymap
import ./ui

var KCH*: Channel[string]
var KCH*: Channel[seq[string]]
KCH.open()

when defined(windows):
Expand All @@ -23,78 +23,94 @@ else:
proc cleanExit*() =
disable_raw_mode()

proc getKey*(): string {.inline.} =
result = ""
proc getKey*(): seq[string] {.inline.} =
result = @[]
when not defined(windows):
enable_raw_mode()

LASTCHAR = getch()
result = $LASTCHAR.int
var
lchr: char
code = ""

while kbhit() != 0:
LASTCHAR = getch()
result &= $LASTCHAR.int
lchr = getch()
if lchr.int < 32 or lchr.int > 126:
code = $lchr.int
if lchr.int in {0, 27, 224}:
while kbhit() != 0:
lchr = getch()
code &= $lchr.int
result.add(code)
else:
result.add($lchr)

when not defined(windows):
disable_raw_mode()

proc getDialogKey*(max=1, nl=true): string =
result = ""
var ready: bool
var code: string
var
ready: bool
codes: seq[string]

while true:
(ready, code) = KCH.tryRecv()
(ready, codes) = KCH.tryRecv()

if ready:
if KEYMAP.hasKey(code):
let key = KEYMAP[code]
case key
of ENTER, CTRL_ENTER:
return
of BACKSPACE:
if result.len() != 0:
result = result.substr(0, result.len()-2)
eraseLeftDialog()
of ESC, CTRL_C:
return ""
else: discard
else:
if result.len() < max:
let rcode = code.parseInt().char
result &= rcode
stdout.write(rcode)
stdout.flushFile()
if not nl:
break
for code in codes:
if KEYMAP.hasKey(code):
let key = KEYMAP[code]
case key
of ENTER, CTRL_ENTER:
return
of BACKSPACE:
if result.len() != 0:
result = result.substr(0, result.len()-2)
eraseLeftDialog()
of ESC, CTRL_C:
return ""
else: discard
else:
if result.len() < max:
let rcode = code.parseInt().char
result &= rcode
stdout.write(rcode)
stdout.flushFile()
if not nl:
break

proc handleKey*() {.inline.} =
var (ready, code) = KCH.tryRecv()
var (ready, codes) = KCH.tryRecv()

if ready:
if KEYMAP.hasKey(code):
let key = KEYMAP[code]
if KEYACTION.hasKey(key):
let ac = KEYACTION[key]
if ACTIONMAP.hasKey(ac):
ACTIONMAP[ac]()
else:
ACTIONMAP[DEFAULT]()
for code in codes:
if KEYMAP.hasKey(code):
let key = KEYMAP[code]
if KEYACTION.hasKey(key):
let ac = KEYACTION[key]
if ACTIONMAP.hasKey(ac):
ACTIONMAP[ac]()
else:
LASTCHAR = code[0]
ACTIONMAP[DEFAULT]()
lcol()

proc startKey() {.thread.} =
var code = ""
while true:
code = getKey()
if code != "":
if not KCH.trySend(code):
echo "Unable to send key"
var codes = getKey()
if codes.len() != 0:
if not KCH.trySend(codes):
echo "Unable to send key(s)"

proc setupKey*() =
spawn startKey()

when isMainModule:
while true:
var code = getKey()
if code != "":
echo code
if code == "27":
break
var exit = false
while not exit:
for code in getKey():
if code != "":
echo "$#" % code
if code.strip() == "27":
exit = true
break

0 comments on commit 04ab51e

Please sign in to comment.