diff options
| author | bptato <nincsnevem662@gmail.com> | 2022-10-04 12:13:27 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2022-10-04 12:13:27 +0200 |
| commit | c4e2de9cd8cad7e28b33e68b1b76f9044fe510be (patch) | |
| tree | 92930e716d3c1ffca8614c030a35c5008655bca4 /src | |
| parent | 10d3154eff79ce53b82fa946556f52f3e10eac90 (diff) | |
| download | chawan-c4e2de9cd8cad7e28b33e68b1b76f9044fe510be.tar.gz | |
WIP pager
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/config.nim | 40 | ||||
| -rw-r--r-- | src/display/client.nim | 44 | ||||
| -rw-r--r-- | src/display/pager.nim | 62 | ||||
| -rw-r--r-- | src/io/buffer.nim | 11 | ||||
| -rw-r--r-- | src/io/lineedit.nim | 10 | ||||
| -rw-r--r-- | src/js/javascript.nim | 2 | ||||
| -rw-r--r-- | src/main.nim | 31 |
7 files changed, 140 insertions, 60 deletions
diff --git a/src/config/config.nim b/src/config/config.nim index fe24db51..56f13382 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -40,7 +40,8 @@ type ACTION_LINED_ESC ActionMap = Table[string, TwtAction] - Config* = object + Config* = ref ConfigObj + ConfigObj = object nmap*: Table[string, string] lemap*: ActionMap stylesheet*: string @@ -145,7 +146,7 @@ proc readUserStylesheet(dir, file: string): string = result = f.readAll() f.close() -proc parseConfig(config: var Config, dir: string, t: TomlValue) = +proc parseConfig(config: Config, dir: string, t: TomlValue) = if "general" in t: let general = t["general"] if "double-width-ambiguous" in general: @@ -184,34 +185,37 @@ proc parseConfig(config: var Config, dir: string, t: TomlValue) = of "white": config.markcolor = CellColor(rgb: false, color: 47u8) of "terminal": config.markcolor = defaultColor -proc parseConfig(config: var Config, dir: string, stream: Stream) = +proc parseConfig(config: Config, dir: string, stream: Stream) = config.parseConfig(dir, parseToml(stream)) -proc parseConfig*(config: var Config, dir: string, s: string) = +proc parseConfig*(config: Config, dir: string, s: string) = config.parseConfig(dir, newStringStream(s)) -proc staticReadConfig(): Config = - result.parseConfig("res", staticRead"res/config.toml") +proc staticReadConfig(): ConfigObj = + var config = new(Config) + config.parseConfig("res", staticRead"res/config.toml") + return config[] const defaultConfig = staticReadConfig() -var gconfig* = defaultConfig -proc readConfig(dir: string) = +proc readConfig(config: Config, dir: string) = let fs = newFileStream(dir / "config.toml") if fs != nil: - gconfig.parseConfig(dir, fs) + config.parseConfig(dir, fs) -proc getNormalAction*(s: string): string = - if gconfig.nmap.hasKey(s): - return gconfig.nmap[s] +proc getNormalAction*(config: Config, s: string): string = + if config.nmap.hasKey(s): + return config.nmap[s] return "" -proc getLinedAction*(s: string): TwtAction = - if gconfig.lemap.hasKey(s): - return gconfig.lemap[s] +proc getLinedAction*(config: Config, s: string): TwtAction = + if config.lemap.hasKey(s): + return config.lemap[s] return NO_ACTION -proc readConfig*() = +proc readConfig*(): Config = + new(result) + result[] = defaultConfig when defined(debug): - readConfig(getCurrentDir() / "res") - readConfig(getConfigDir() / "chawan") + result.readConfig(getCurrentDir() / "res") + result.readConfig(getConfigDir() / "chawan") diff --git a/src/display/client.nim b/src/display/client.nim index ef855017..82fc2299 100644 --- a/src/display/client.nim +++ b/src/display/client.nim @@ -11,6 +11,7 @@ import std/monotimes import css/sheet import config/config +import display/pager import html/dom import html/htmlparser import io/buffer @@ -34,7 +35,9 @@ type errormessage: string userstyle: CSSStylesheet loader: FileLoader - console: Console + console {.jsget.}: Console + pager {.jsget.}: Pager + config: Config jsrt: JSRuntime jsctx: JSContext regex: Option[Regex] @@ -94,7 +97,6 @@ proc addBuffer(client: Client) = client.buffer = client.buffer.next client.buffer.loader = client.loader client.buffer.userstyle = client.userstyle - client.buffer.markcolor = gconfig.markcolor proc prevBuffer(client: Client) {.jsfunc.} = if client.buffer.prev != nil: @@ -361,7 +363,7 @@ proc changeLocation(client: Client) {.jsfunc.} = let buffer = client.buffer var url = buffer.location.serialize(true) client.statusMode() - let status = readLine("URL: ", url, buffer.width) + let status = readLine("URL: ", url, buffer.width, config = client.config) if status: client.loadUrl(url) @@ -448,7 +450,7 @@ proc command(client: Client, src: string) = proc command(client: Client): bool {.jsfunc.} = var iput: string client.statusMode() - let status = readLine("COMMAND: ", iput, client.buffer.width) + let status = readLine("COMMAND: ", iput, client.buffer.width, config = client.config) if status: client.command(iput) return status @@ -473,7 +475,7 @@ proc searchPrev(client: Client) {.jsfunc.} = proc search(client: Client) {.jsfunc.} = client.statusMode() var iput: string - let status = readLine("/", iput, client.buffer.width) + let status = readLine("/", iput, client.buffer.width, config = client.config) if status: if iput.len != 0: client.regex = compileSearchRegex(iput) @@ -483,7 +485,7 @@ proc search(client: Client) {.jsfunc.} = proc searchBack(client: Client) {.jsfunc.} = client.statusMode() var iput: string - let status = readLine("?", iput, client.buffer.width) + let status = readLine("?", iput, client.buffer.width, config = client.config) if status: if iput.len != 0: client.regex = compileSearchRegex(iput) @@ -499,7 +501,7 @@ proc isearch(client: Client) {.jsfunc.} = if mark != nil: client.buffer.removeMark(mark) - let status = readLine("/", iput, client.buffer.width, {}, false, (proc(state: var LineState): bool = + let status = readLine("/", iput, client.buffer.width, {}, false, client.config, (proc(state: var LineState): bool = del_mark let regex = compileSearchRegex($state.news) client.buffer.cpos = cpos @@ -537,7 +539,7 @@ proc isearchBack(client: Client) {.jsfunc.} = template del_mark() = if mark != nil: client.buffer.removeMark(mark) - let status = readLine("?", iput, client.buffer.width, {}, false, (proc(state: var LineState): bool = + let status = readLine("?", iput, client.buffer.width, {}, false, client.config, (proc(state: var LineState): bool = del_mark let regex = compileSearchRegex($state.news) client.buffer.cpos = cpos @@ -567,6 +569,7 @@ proc isearchBack(client: Client) {.jsfunc.} = proc quit(client: Client) {.jsfunc.} = print(HVP(getTermAttributes().height, 0)) + print(EL()) quit(0) proc feedNext(client: Client) {.jsfunc.} = @@ -618,7 +621,7 @@ proc input(client: Client) = let c = client.console.readChar() client.s &= c - let action = getNormalAction(client.s) + let action = getNormalAction(client.config, client.s) client.evalJSFree(action, "<command>") proc followRedirect(client: Client) @@ -628,13 +631,13 @@ proc checkAuth(client: Client) = client.buffer.refreshBuffer() client.statusMode() var username = "" - let ustatus = readLine("Username: ", username, client.buffer.width) + let ustatus = readLine("Username: ", username, client.buffer.width, config = client.config) if not ustatus: client.needsauth = false return client.statusMode() var password = "" - let pstatus = readLine("Password: ", password, client.buffer.width, hide = true) + let pstatus = readLine("Password: ", password, client.buffer.width, hide = true, config = client.config) if not pstatus: client.needsauth = false return @@ -768,14 +771,14 @@ proc jsEventLoop(client: Client) = sleep(wait) proc launchClient*(client: Client, pages: seq[string], ctype: string, dump: bool) = - if gconfig.startup != "": - let s = readFile(gconfig.startup) + if client.config.startup != "": + let s = readFile(client.config.startup) client.console.err = newFileStream(stderr) - client.command0(s, gconfig.startup, silence = true) + client.command0(s, client.config.startup, silence = true) client.jsEventLoop() client.console.err = newStringStream() quit() - client.userstyle = gconfig.stylesheet.parseStylesheet() + client.userstyle = client.config.stylesheet.parseStylesheet() if not stdin.isatty: client.readPipe(ctype) try: @@ -819,8 +822,9 @@ proc log(console: Console, ss: varargs[string]) {.jsfunc.} = proc sleep(client: Client, millis: int) {.jsfunc.} = sleep millis -proc newClient*(): Client = +proc newClient*(config: Config): Client = new(result) + result.config = config result.loader = newFileLoader() result.console = newConsole() let rt = newJSRuntime() @@ -833,14 +837,12 @@ proc newClient*(): Client = ctx.registerType(Client, asglobal = true) global.setOpaque(result) ctx.setProperty(global.val, "client", global.val) - - let consoleClassId = ctx.registerType(Console) - let jsConsole = ctx.newJSObject(consoleClassId) - jsConsole.setOpaque(result.console) - ctx.setProperty(global.val, "console", jsConsole.val) free(global) + ctx.registerType(Console) + ctx.addUrlModule() ctx.addDOMModule() ctx.addHTMLModule() ctx.addRequestModule() + ctx.addPagerModule() diff --git a/src/display/pager.nim b/src/display/pager.nim new file mode 100644 index 00000000..9fc8957d --- /dev/null +++ b/src/display/pager.nim @@ -0,0 +1,62 @@ +import config/config +import io/buffer +import js/javascript + +type + Container = ref object + buffer: Buffer + children: seq[Container] + + Pager* = ref object + rootContainer: Container + container: Container + config: Config + +proc cursorLeft(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorLeft() +proc cursorDown(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorDown() +proc cursorUp(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorUp() +proc cursorRight(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorRight() +proc cursorLineBegin(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorLineBegin() +proc cursorLineEnd(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorLineEnd() +proc cursorNextWord(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorNextWord() +proc cursorPrevWord(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorPrevWord() +proc cursorNextLink(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorNextLink() +proc cursorPrevLink(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorPrevLink() +proc pageDown(pager: Pager) {.jsfunc.} = pager.container.buffer.pageDown() +proc pageUp(pager: Pager) {.jsfunc.} = pager.container.buffer.pageUp() +proc pageRight(pager: Pager) {.jsfunc.} = pager.container.buffer.pageRight() +proc pageLeft(pager: Pager) {.jsfunc.} = pager.container.buffer.pageLeft() +proc halfPageDown(pager: Pager) {.jsfunc.} = pager.container.buffer.halfPageDown() +proc halfPageUp(pager: Pager) {.jsfunc.} = pager.container.buffer.halfPageUp() +proc cursorFirstLine(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorFirstLine() +proc cursorLastLine(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorLastLine() +proc cursorTop(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorTop() +proc cursorMiddle(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorMiddle() +proc cursorBottom(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorBottom() +proc cursorLeftEdge(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorLeftEdge() +proc cursorVertMiddle(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorVertMiddle() +proc cursorRightEdge(pager: Pager) {.jsfunc.} = pager.container.buffer.cursorRightEdge() +proc centerLine(pager: Pager) {.jsfunc.} = pager.container.buffer.centerLine() +proc scrollDown(pager: Pager) {.jsfunc.} = pager.container.buffer.scrollDown() +proc scrollUp(pager: Pager) {.jsfunc.} = pager.container.buffer.scrollUp() +proc scrollLeft(pager: Pager) {.jsfunc.} = pager.container.buffer.scrollLeft() +proc scrollRight(pager: Pager) {.jsfunc.} = pager.container.buffer.scrollRight() +proc lineInfo(pager: Pager) {.jsfunc.} = pager.container.buffer.lineInfo() +proc reshape(pager: Pager) {.jsfunc.} = pager.container.buffer.reshape = true +proc redraw(pager: Pager) {.jsfunc.} = pager.container.buffer.redraw = true + +proc newContainer(): Container = + new(result) + +proc newPager*(config: Config, buffer: Buffer): Pager = + result.config = config + result.rootContainer = newContainer() + +proc addBuffer*(pager: Pager, buffer: Buffer) = + var ncontainer = newContainer() + ncontainer.buffer = buffer + pager.container.children.add(ncontainer) + pager.container = ncontainer + +proc addPagerModule*(ctx: JSContext) = + ctx.registerType(Pager) diff --git a/src/io/buffer.nim b/src/io/buffer.nim index 8b1268ef..b5e46297 100644 --- a/src/io/buffer.nim +++ b/src/io/buffer.nim @@ -8,6 +8,7 @@ import unicode import css/sheet import css/stylednode +import config/config import html/dom import html/tags import html/htmlparser @@ -67,8 +68,8 @@ type next*: Buffer userstyle*: CSSStylesheet loader*: FileLoader - markcolor*: CellColor marks*: seq[Mark] + config*: Config proc newBuffer*(): Buffer = new(result) @@ -713,7 +714,7 @@ proc gotoAnchor*(buffer: Buffer) = proc addMark*(buffer: Buffer, x, y, width: int): Mark = assert y < buffer.lines.len var format = newFormat() - format.bgcolor = buffer.markcolor + format.bgcolor = buffer.config.markcolor result = Mark(x: x, y: y, width: width, format: format) let previ = upperBound(buffer.marks, y, (proc(a: Mark, b: int): int = cmp(a.y, b))) buffer.marks.insert(result, previ) @@ -1139,7 +1140,7 @@ proc click*(buffer: Buffer): Option[Request] = var value = input.value print(HVP(buffer.height + 1, 1)) print(EL()) - let status = readLine("SEARCH: ", value, buffer.width, {'\r', '\n'}) + let status = readLine("SEARCH: ", value, buffer.width, {'\r', '\n'}, config = buffer.config) if status: input.value = value input.invalid = true @@ -1151,7 +1152,7 @@ proc click*(buffer: Buffer): Option[Request] = var value = input.value print(HVP(buffer.height + 1, 1)) print(EL()) - let status = readLine("TEXT: ", value, buffer.width, {'\r', '\n'}, input.inputType == INPUT_PASSWORD) + let status = readLine("TEXT: ", value, buffer.width, {'\r', '\n'}, input.inputType == INPUT_PASSWORD, config = buffer.config) if status: input.value = value input.invalid = true @@ -1163,7 +1164,7 @@ proc click*(buffer: Buffer): Option[Request] = "" print(HVP(buffer.height + 1, 1)) print(EL()) - let status = readLine("Filename: ", path, buffer.width, {'\r', '\n'}) + let status = readLine("Filename: ", path, buffer.width, {'\r', '\n'}, config = buffer.config) if status: let cdir = parseUrl("file://" & getCurrentDir() & DirSep) let path = parseUrl(path, cdir) diff --git a/src/io/lineedit.nim b/src/io/lineedit.nim index 3f9034bf..3d91ea80 100644 --- a/src/io/lineedit.nim +++ b/src/io/lineedit.nim @@ -22,6 +22,7 @@ type LineState* = object displen: int disallowed: set[char] hide: bool + config: Config #TODO get rid of this callback: proc(state: var LineState): bool func lwidth(r: Rune): int = @@ -148,7 +149,7 @@ proc readLine(state: var LineState): bool = let c = stdin.readChar() state.s &= c - var action = getLinedAction(state.s) + var action = getLinedAction(state.config, state.s) if state.escNext: action = NO_ACTION case action @@ -271,7 +272,7 @@ proc readLine(state: var LineState): bool = state.feedNext = true proc readLine*(prompt: string, current: var string, termwidth: int, - disallowed: set[char], hide: bool, + disallowed: set[char], hide: bool, config: Config, callback: proc(state: var LineState): bool): bool = var state: LineState @@ -285,6 +286,7 @@ proc readLine*(prompt: string, current: var string, termwidth: int, state.disallowed = disallowed state.callback = callback state.hide = hide + state.config = config if state.readLine(): current = $state.news @@ -292,5 +294,5 @@ proc readLine*(prompt: string, current: var string, termwidth: int, return false proc readLine*(prompt: string, current: var string, termwidth: int, - disallowed: set[char] = {}, hide = false): bool = - readLine(prompt, current, termwidth, disallowed, hide, (proc(state: var LineState): bool = false)) + disallowed: set[char] = {}, hide = false, config: Config): bool = + readLine(prompt, current, termwidth, disallowed, hide, config, (proc(state: var LineState): bool = false)) diff --git a/src/js/javascript.nim b/src/js/javascript.nim index 24a87622..3047a9de 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -7,6 +7,8 @@ import tables import bindings/quickjs +export options + export JS_NULL, JS_UNDEFINED, JS_FALSE, JS_TRUE, JS_EXCEPTION, JS_UNINITIALIZED diff --git a/src/main.nim b/src/main.nim index 990372ee..817ec190 100644 --- a/src/main.nim +++ b/src/main.nim @@ -8,11 +8,19 @@ import config/config import display/client import utils/twtstr -readConfig() +let conf = readConfig() let params = commandLineParams() +proc version(long: static bool = false): string = + result = "Chawan browser v0.1 " + when defined(debug): + result &= "(debug)" + else: + result &= "(release)" + proc help(i: int) = - let s = """ + let s = version() & """ + Usage: cha [options] [URL(s) or file(s)...] Options: -d, --dump Print page to stdout @@ -41,8 +49,7 @@ while i < params.len: continue case param of "-v", "--version": - echo "Chawan browser v0.1 ", - when defined(debug): "(debug)" else: "(release)" + echo version(true) quit(0) of "-T": inc i @@ -57,13 +64,13 @@ while i < params.len: of "-c", "--css": inc i if i < params.len: - gconfig.stylesheet &= params[i] + conf.stylesheet &= params[i] else: help(1) of "-o", "--opt": inc i if i < params.len: - gconfig.parseConfig(getCurrentDir(), params[i]) + conf.parseConfig(getCurrentDir(), params[i]) else: help(1) of "-h", "--help": @@ -71,7 +78,7 @@ while i < params.len: of "-r", "--run": inc i if i < params.len: - gconfig.startup = params[i] + conf.startup = params[i] else: help(1) of "--": @@ -83,13 +90,13 @@ while i < params.len: pages.add(param) inc i -if pages.len == 0 and gconfig.startup == "": +if pages.len == 0 and conf.startup == "": if stdin.isatty: help(1) -gconfig.nmap = constructActionTable2(gconfig.nmap) -gconfig.lemap = constructActionTable(gconfig.lemap) +conf.nmap = constructActionTable2(conf.nmap) +conf.lemap = constructActionTable(conf.lemap) -width_table = makewidthtable(gconfig.ambiguous_double) +width_table = makewidthtable(conf.ambiguous_double) -newClient().launchClient(pages, ctype, dump) +newClient(conf).launchClient(pages, ctype, dump) |