Add (wip) more and less

This commit is contained in:
joachimschmidt557 2019-04-05 18:01:02 +02:00
parent 5d328998f8
commit 847cd55617
3 changed files with 62 additions and 1 deletions

52
more.nim Normal file
View file

@ -0,0 +1,52 @@
import cligen, terminal
import common
proc redraw(lineBuffer:seq[string], topLine:int) =
eraseScreen(stdout)
for i in topLine .. topLine + terminalHeight():
writeLine(stdout, lineBuffer[i])
let percent = (100 * (topLine / lineBuffer.len)).int
stdout.styledWrite(fgBlack, bgWhite, "--More--(" & $(percent) & "%)")
proc main(files:seq[string]) =
if files.len == 0:
err "more: bad usage"
quit 1
addQuitProc(resetAttributes)
var lineBuffer:seq[string]
for file in files:
for line in lines(file):
lineBuffer.add(line)
if lineBuffer.len < terminalHeight():
for line in lineBuffer:
writeLine(stdout, line)
return
let
minTopLine = 0
maxTopLine = lineBuffer.high - terminalHeight()
var
command = '\0'
topLine = minTopLine
while true:
redraw(lineBuffer, topLine)
command = getch()
if command == 'q':
break
if command == 'j':
inc topLine
if topLine > maxTopLine: topLine = maxTopLine
if command == 'k':
dec topLine
if topLine < minTopLine: topLine = minTopLine
if command == 'f':
topLine += terminalHeight()
if topLine > maxTopLine: topLine = maxTopLine
if command == 'b':
topLine -= terminalHeight()
if topLine < minTopLine: topLine = minTopLine
if command == 'g':
topLine = minTopLine
if command == 'G':
topLine = maxTopLine
dispatch(main, version=("version", nimbaseVersion))