minpkg/lib/min_io.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import std/[strutils, logging, nre, critbits, terminal] import minline import ../core/parser, ../core/value, ../core/env, ../core/interpreter, ../core/utils var ORIGKEYMAP {.threadvar.}: CritBitTree[KeyCallback] for key, value in KEYMAP.pairs: ORIGKEYMAP[key] = value proc io_module*(i: In) = let def = i.define() def.symbol("clear") do (i: In): stdout.eraseScreen stdout.setCursorPos(0, 0) def.symbol("unmapkey") do (i: In): let vals = i.expect("'sym") let key = vals[0].getString.toLowerAscii if not KEYNAMES.contains(key) and not KEYSEQS.contains(key): raiseInvalid("Unrecognized key: " & key) if KEYMAP.hasKey(key): if ORIGKEYMAP.hasKey(key): KEYMAP[key] = ORIGKEYMAP[key] else: KEYMAP.excl(key) def.symbol("mapkey") do (i: In): let vals = i.expect("'sym", "quot") let key = vals[0].getString.toLowerAscii var q = vals[1] if not KEYNAMES.contains(key) and not KEYSEQS.contains(key): raiseInvalid("Unrecognized key: " & key) var ic = i.copy(i.filename) let action = proc (ed: var LineEditor) {.closure.} = ic.apply(q) KEYMAP[key] = action def.symbol("color") do (i: In): let vals = i.expect("bool") COLOR = vals[0].boolVal def.symbol("notice") do (i: In): let a = i.peek notice $$a def.symbol("info") do (i: In): let a = i.peek info $$a def.symbol("error") do (i: In): let a = i.peek error $$a def.symbol("warn") do (i: In): let a = i.peek warn $$a def.symbol("debug") do (i: In): let a = i.peek debug $$a def.symbol("fatal") do (i: In): let a = i.peek fatal $$a quit(100) def.symbol("column-print") do (i: In): let vals = i.expect("int", "quot") let n = vals[0] let q = vals[1] var c = 0 for s in q.qVal: c.inc stdout.write $$s & spaces(max(0, 15 - ($$s).len)) if c mod n.intVal == 0: echo "" echo "" def.symbol("getchr") do (i: In): i.push getchr().newVal def.symbol("putchr") do (i: In): let ch = i.expect("str") if ch[0].getString.len != 1: raiseInvalid("Symbol putch requires a string containing a single character.") putchr(ch[0].getString[0].cint) def.symbol("password") do (i: In): var ed = initEditor() i.push ed.password("Enter Password: ").newVal def.symbol("ask") do (i: In): var ed = initEditor() let vals = i.expect("str") let s = vals[0] i.push ed.readLine(s.getString & ": ").newVal def.symbol("confirm") do (i: In): var ed = initEditor() let vals = i.expect("str") let s = vals[0] proc confirm(): bool = let answer = ed.readLine(s.getString & " [yes/no]: ") if answer.contains(re"(?i)^y(es)?$"): return true elif answer.contains(re"(?i)^no?$"): return false else: stdout.write "Invalid answer. Please enter 'yes' or 'no': " stdout.flushFile() return confirm() i.push confirm().newVal def.symbol("choose") do (i: In): var ed = initEditor() let vals = i.expect("'sym", "quot") let s = vals[0] var q = vals[1] if q.qVal.len <= 0: raiseInvalid("No choices to display") stdout.writeLine(s.getString) stdout.flushFile() proc choose(): int = var c = 0 for item in q.qVal: if not item.isQuotation or not item.qVal.len == 2 or not item.qVal[ 0].isString or not item.qVal[1].isQuotation: raiseInvalid("Each item of the quotation must be a quotation containing a string and a quotation") c.inc echo "$1 - $2" % [$c, item.qVal[0].getString] let answer = ed.readLine("Enter your choice ($1 - $2): " % ["1", $c]) var choice: int try: choice = answer.parseInt except CatchableError: choice = 0 if choice <= 0 or choice > c: echo "Invalid choice." return choose() else: return choice let choice = choose() i.dequote(q.qVal[choice-1].qVal[1]) def.symbol("write") do (i: In): i.pushSym("fwrite") def.symbol("read") do (i: In): i.pushSym("fread") def.finalize("io") |