mirror of https://github.com/japl-lang/jale.git
terminal resize awareness
This commit is contained in:
parent
c222cb1a19
commit
add1dd8c31
|
@ -1,4 +1,5 @@
|
|||
import jale/plugin/defaults
|
||||
import jale/plugin/viewprotector
|
||||
import jale/editor
|
||||
import jale/templates
|
||||
import jale/multiline
|
||||
|
@ -22,6 +23,7 @@ e.bindKey("ctrl+s"):
|
|||
|
||||
e.prompt = ""
|
||||
e.populateDefaults(enterSubmits = false, ctrlForVerticalMove = false)
|
||||
e.setViewBehavior(vbFullscreen)
|
||||
e.scrollMode = sbAllScroll
|
||||
let result = e.read()
|
||||
if save and paramCount() > 0:
|
||||
|
|
|
@ -12,7 +12,8 @@ import os
|
|||
|
||||
type
|
||||
JaleEvent* = enum
|
||||
jeKeypress, jeQuit, jeFinish, jePreRead, jePostRead
|
||||
jeKeypress, jeQuit, jeFinish, jePreRead, jePostRead, jePreFullRender,
|
||||
jePreKey, jePostKey, jeResize
|
||||
|
||||
EditorState = enum
|
||||
esOutside, esTyping, esFinishing, esQuitting
|
||||
|
@ -37,7 +38,7 @@ type
|
|||
rendered: int # how many lines were printed last full refresh
|
||||
forceRedraw: bool
|
||||
hscroll: int
|
||||
vmax: int
|
||||
vmax*: int
|
||||
vscroll: int
|
||||
|
||||
# getter/setter sorts
|
||||
|
@ -108,6 +109,8 @@ proc fullRender(editor: LineEditor) =
|
|||
# from the top cursor pos, it draws the entire multiline prompt, then
|
||||
# moves cursor to current y
|
||||
|
||||
editor.events.call(jePreFullRender)
|
||||
|
||||
let lastY = min(editor.content.high(), editor.vscroll + editor.vmax - 1)
|
||||
for i in countup(editor.vscroll, lastY):
|
||||
editor.render(i)
|
||||
|
@ -156,7 +159,9 @@ proc read*(editor: LineEditor): string =
|
|||
# call the events
|
||||
editor.lastKeystroke = key
|
||||
editor.keystrokes.call(key)
|
||||
editor.events.call(jePreKey)
|
||||
editor.events.call(jeKeypress)
|
||||
editor.events.call(jePostKey)
|
||||
# autoscroll horizontally based on current scroll and x pos
|
||||
|
||||
# last x rendered
|
||||
|
|
|
@ -3,6 +3,7 @@ import ../keycodes
|
|||
import ../multiline
|
||||
import ../event
|
||||
import ../templates
|
||||
import resize
|
||||
|
||||
import tables
|
||||
|
||||
|
@ -20,10 +21,10 @@ proc bindTerminate*(editor: LineEditor) =
|
|||
if editor.content.getContent() == "":
|
||||
editor.quit()
|
||||
|
||||
|
||||
proc populateDefaults*(editor: LineEditor, enterSubmits = true, ctrlForVerticalMove = true) =
|
||||
editor.bindInput()
|
||||
editor.bindTerminate()
|
||||
editor.bindResize()
|
||||
editor.bindKey("left"):
|
||||
editor.content.left()
|
||||
editor.bindKey("right"):
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
# only works on posix (through handling SIGWINCH signals)
|
||||
|
||||
import ../editor
|
||||
import ../event
|
||||
|
||||
when defined(posix):
|
||||
import posix
|
||||
|
||||
var editors: seq[LineEditor]
|
||||
|
||||
proc bindResize*(ed: LineEditor) =
|
||||
editors.add(ed)
|
||||
|
||||
when defined(posix):
|
||||
onSignal(28):
|
||||
for ed in editors:
|
||||
ed.events.call(jeResize)
|
|
@ -0,0 +1,27 @@
|
|||
import ../editor
|
||||
import ../templates
|
||||
|
||||
import terminal
|
||||
|
||||
type ViewBehavior* = enum
|
||||
vbLines, vbLinesLimited, vbFullscreen
|
||||
|
||||
proc setViewBehavior*(ed: LineEditor, behavior: ViewBehavior) =
|
||||
case behavior:
|
||||
of vbLines:
|
||||
ed.bindEvent(jeResize):
|
||||
ed.vmax = terminalHeight() - 1
|
||||
of vbLinesLimited:
|
||||
discard
|
||||
of vbFullscreen:
|
||||
var resized = false
|
||||
ed.bindEvent(jeResize):
|
||||
ed.vmax = terminalHeight() - 1
|
||||
ed.redraw()
|
||||
resized = true
|
||||
ed.bindEvent(jePreFullRender):
|
||||
if resized:
|
||||
eraseScreen()
|
||||
setCursorPos(0,0)
|
||||
resized = false
|
||||
|
Loading…
Reference in New Issue