jale/editor.nim

158 lines
3.6 KiB
Nim
Raw Normal View History

2021-02-15 20:18:31 +01:00
import strformat
import strutils
import tables
import terminal
import line
import multiline
import keycodes
import event
import renderer
type
JaleEvent* = enum
jeKeypress, jeQuit, jeFinish, jePreRead, jePostRead
2021-02-15 20:18:31 +01:00
EditorState = enum
esOutside, esTyping, esFinishing, esQuitting
2021-02-15 20:18:31 +01:00
LineEditor* = ref object
# permanents
2021-02-15 20:18:31 +01:00
keystrokes*: Event[int]
events*: Event[JaleEvent]
prompt*: string
# permanent internals: none
# per-read contents
content*: Multiline
2021-02-15 20:18:31 +01:00
lastKeystroke*: int
# per-read internals
state: EditorState
2021-02-15 20:18:31 +01:00
rendered: int # how many lines were printed last full refresh
forceRedraw: bool
2021-02-15 20:18:31 +01:00
2021-02-15 20:59:40 +01:00
# getter/setter sorts
proc unfinish*(le: LineEditor) =
le.state = esTyping
2021-02-15 20:59:40 +01:00
proc finish*(le: LineEditor) =
le.state = esFinishing
2021-02-15 20:59:40 +01:00
# can be overwritten to false, inside the event
le.events.call(jeFinish)
2021-02-18 21:51:44 +01:00
proc quit*(le: LineEditor) =
le.state = esQuitting
2021-02-18 21:51:44 +01:00
le.events.call(jeQuit)
proc forceRedraw*(le: LineEditor) =
le.forceRedraw = true
2021-02-15 20:59:40 +01:00
# constructor
2021-02-15 20:18:31 +01:00
proc newLineEditor*: LineEditor =
new(result)
result.content = newMultiline()
result.keystrokes.new()
result.events.new()
result.prompt = ""
result.rendered = 0
result.lastKeystroke = -1
result.forceRedraw = false
result.state = esOutside
2021-02-15 20:18:31 +01:00
2021-02-15 20:59:40 +01:00
# priv/pub methods
proc reset(editor: LineEditor) =
## Resets state to outside, resets internal rendering details
## resets last keystroke, creates new contents
editor.state = esOutside
2021-02-15 20:59:40 +01:00
editor.rendered = 0
editor.content = newMultiline()
editor.lastKeystroke = -1
editor.forceRedraw = false
2021-02-15 20:18:31 +01:00
proc render(editor: LineEditor, line: int = -1, hscroll: bool = true) =
var y = line
if y == -1:
y = editor.content.Y
renderLine(
(
if y == 0:
editor.prompt
else:
" ".repeat(editor.prompt.len())
),
editor.content.getLine(y),
0
)
proc clearLine =
write stdout, "\r" & " ".repeat(terminalWidth())
2021-02-15 20:18:31 +01:00
proc fullRender(editor: LineEditor) =
2021-02-15 20:59:40 +01:00
# from the top cursor pos, it draws the entire multiline prompt, then
# moves cursor to current y
2021-02-15 20:18:31 +01:00
for i in countup(0, editor.content.high()):
editor.render(i, false)
2021-02-15 20:59:40 +01:00
if i < editor.rendered:
cursorDown(1)
else:
write stdout, "\n"
inc editor.rendered
var extraup = 0
while editor.content.len() < editor.rendered:
clearLine()
cursorDown(1)
dec editor.rendered
inc extraup
cursorUp(editor.content.len() - editor.content.Y + extraup)
2021-02-15 20:18:31 +01:00
proc moveCursorToEnd(editor: LineEditor) =
# only called when read finished
2021-02-15 20:59:40 +01:00
if editor.content.high() > editor.content.Y:
cursorDown(editor.content.high() - editor.content.Y)
2021-02-15 20:18:31 +01:00
write stdout, "\n"
proc read*(editor: LineEditor): string =
editor.state = esTyping
editor.events.call(jePreRead)
2021-02-15 20:59:40 +01:00
# starts at the top, full render moves it into the right y
2021-02-15 20:18:31 +01:00
editor.fullRender()
2021-02-15 20:59:40 +01:00
while editor.state == esTyping:
2021-02-15 20:59:40 +01:00
# refresh current line every time
2021-02-15 20:18:31 +01:00
editor.render()
setCursorXPos(editor.content.X + editor.prompt.len())
2021-02-15 20:59:40 +01:00
# get key (with escapes)
2021-02-15 20:18:31 +01:00
let key = getKey()
2021-02-15 20:59:40 +01:00
# record y pos
2021-02-15 20:18:31 +01:00
let preY = editor.content.Y
2021-02-15 20:59:40 +01:00
# call the events
2021-02-15 20:18:31 +01:00
editor.lastKeystroke = key
editor.keystrokes.call(key)
editor.events.call(jeKeypress)
2021-02-15 20:59:40 +01:00
# redraw everything if y changed
if editor.forceRedraw or preY != editor.content.Y:
2021-02-15 20:59:40 +01:00
# move to the top
if preY > 0:
cursorUp(preY)
# move to the right y
2021-02-15 20:18:31 +01:00
editor.fullRender()
if editor.forceRedraw:
editor.forceRedraw = false
editor.events.call(jePostRead)
2021-02-15 20:18:31 +01:00
2021-02-15 20:59:40 +01:00
# move cursor to end
2021-02-15 20:18:31 +01:00
editor.moveCursorToEnd()
2021-02-18 21:51:44 +01:00
result = editor.content.getContent()
2021-02-15 20:59:40 +01:00
editor.reset()