From 4be86b80affe2dfe3e84a0e0cb079054f071e8e1 Mon Sep 17 00:00:00 2001 From: joachimschmidt557 Date: Sun, 21 Jul 2024 16:30:44 +0200 Subject: [PATCH] nimbox: completely wrap Nimbox in nimboxext - unused (and potentially conflicting) functions such as nimbox.`add` are not included now - environment variable `NIMMM_256` is only checked once now at startup --- src/draw.nim | 43 ++++++++++++++++---------------- src/keymap.nim | 4 +-- src/nimboxext.nim | 62 ++++++++++++++++++++++++++++++++++++++++------- src/nimmm.nim | 36 +++++++++------------------ src/readline.nim | 4 +-- 5 files changed, 89 insertions(+), 60 deletions(-) diff --git a/src/draw.nim b/src/draw.nim index 0defd04..caad5dd 100644 --- a/src/draw.nim +++ b/src/draw.nim @@ -1,6 +1,5 @@ import std/[times, sets, os, strformat, strutils, options] -import nimbox import lscolors/style import wcwidth @@ -63,7 +62,7 @@ proc formatPath(path: string, length: int): string = if path.len > length: result.setLen(length) -proc lsColorToNimboxColor(c: style.Color): nimbox.Color = +proc lsColorToNimboxColor(c: style.Color): nimboxext.Color = case c.kind of ck8: case c.ck8Val @@ -77,13 +76,13 @@ proc lsColorToNimboxColor(c: style.Color): nimbox.Color = of c8White: return clrWhite else: return clrWhite -proc lsColorToNimboxColors256(c: style.Color): Option[nimbox.Colors256] = +proc lsColorToNimboxColors256(c: style.Color): Option[nimboxext.Colors256] = case c.kind of ckFixed: - return some nimbox.Colors256(int(c.ckFixedVal)) - else: return none nimbox.Colors256 + return some nimboxext.Colors256(int(c.ckFixedVal)) + else: return none nimboxext.Colors256 -proc getFgColor(entry: DirEntry): nimbox.Color = +proc getFgColor(entry: DirEntry): nimboxext.Color = let sty = entry.style if sty.fg.isSome: @@ -91,22 +90,22 @@ proc getFgColor(entry: DirEntry): nimbox.Color = else: clrWhite -proc getFgColors256(entry: DirEntry): Option[nimbox.Colors256] = +proc getFgColors256(entry: DirEntry): Option[nimboxext.Colors256] = let sty = entry.style if sty.fg.isSome: sty.fg.get.lsColorToNimboxColors256() else: - none nimbox.Colors256 + none nimboxext.Colors256 -proc lsColorToNimboxStyle(sty: style.Style): nimbox.Style = +proc lsColorToNimboxStyle(sty: style.Style): nimboxext.Style = let font = sty.font if font.bold: styBold elif font.underline: styUnderline else: styNone -proc getStyle(entry: DirEntry): nimbox.Style = +proc getStyle(entry: DirEntry): nimboxext.Style = entry.style.lsColorToNimboxStyle() proc drawDirEntry(entry: DirEntry, y: int, highlight: bool, selected: bool, @@ -125,9 +124,9 @@ proc drawDirEntry(entry: DirEntry, y: int, highlight: bool, selected: bool, (if isDir: " /" else: sizeToString(entry.info.size)) & " " & relativePath - fgC8 = c8(getFgColor(entry)) - fg = if highlight: fgHighlight() else: getFgColors256(entry).get(fgC8) - bg = if highlight: c8(clrWhite) else: defaultOrBlack + fgC8 = nb.c8(getFgColor(entry)) + fg = if highlight: nb.fgHighlight() else: getFgColors256(entry).get(fgC8) + bg = if highlight: nb.c8(clrWhite) else: defaultOrBlack style = if highlight: styBold else: getStyle(entry) nb.print(0, y, line, fg, bg, style) @@ -135,16 +134,16 @@ proc drawDirEntry(entry: DirEntry, y: int, highlight: bool, selected: bool, proc drawHeader(numTabs: int, currentTab: int, nb: var Nimbox) = var offsetCd = 6 - nb.print(0, 0, "nimmm", c8(clrYellow), defaultOrBlack, styNone) + nb.print(0, 0, "nimmm", nb.c8(clrYellow), defaultOrBlack, styNone) if numTabs > 1: for i in 1 .. numTabs: let text = $i if i == currentTab+1: - nb.print(offsetCd, 0, text, c8(clrYellow), defaultOrBlack, styBold) + nb.print(offsetCd, 0, text, nb.c8(clrYellow), defaultOrBlack, styBold) else: nb.print(offsetCd, 0, text) offsetCd += text.len + 1 # no wcwidth necessary, only digits + 1 space - nb.print(offsetCd, 0, getCurrentDir(), c8(clrYellow), defaultOrBlack, styBold) + nb.print(offsetCd, 0, getCurrentDir(), nb.c8(clrYellow), defaultOrBlack, styBold) proc drawFooter(index: int, lenEntries: int, lenSelected: int, hidden: bool, search: bool, errMsg: string, nb: var Nimbox) = @@ -157,15 +156,15 @@ proc drawFooter(index: int, lenEntries: int, lenSelected: int, hidden: bool, offsetSelected = offsetS + (if search: 2 else: 0) offsetErrMsg = offsetSelected + (if lenSelected > 0: selectedStr.len + 1 else: 0) - nb.print(0, y, entriesStr, c8(clrYellow), defaultOrBlack) + nb.print(0, y, entriesStr, nb.c8(clrYellow), defaultOrBlack) if hidden: - nb.print(offsetH + 1, y, "H", c8(clrYellow), defaultOrBlack, styBold) + nb.print(offsetH + 1, y, "H", nb.c8(clrYellow), defaultOrBlack, styBold) if search: - nb.print(offsetS + 1, y, "S", c8(clrYellow), defaultOrBlack, styBold) + nb.print(offsetS + 1, y, "S", nb.c8(clrYellow), defaultOrBlack, styBold) if lenSelected > 0: nb.print(offsetSelected + 1, y, selectedStr) if errMsg.len > 0: - nb.print(offsetErrMsg + 1, y, errMsg, c8(clrRed), defaultOrBlack) + nb.print(offsetErrMsg + 1, y, errMsg, nb.c8(clrRed), defaultOrBlack) nb.cursor = (TB_HIDE_CURSOR, TB_HIDE_CURSOR) proc drawInputFooter(prompt: string, query: string, cursorPos: int, @@ -174,7 +173,7 @@ proc drawInputFooter(prompt: string, query: string, cursorPos: int, y = nb.height() - 1 offset = prompt.wcswidth + 1 cursorPos = offset + query[0..cursorPos - 1].wcswidth - nb.print(0, y, prompt, c8(clrYellow), defaultOrBlack) + nb.print(0, y, prompt, nb.c8(clrYellow), defaultOrBlack) nb.print(offset, y, query, defaultOrBlack, defaultOrBlack) nb.cursor = (cursorPos, y) @@ -211,7 +210,7 @@ proc redraw*(s: State, nb: var Nimbox) = "Empty directory" else: "No matching results" - nb.print(0, 2, message, c8(clrYellow), defaultOrBlack) + nb.print(0, 2, message, nb.c8(clrYellow), defaultOrBlack) for i in topIndex .. bottomIndex: let entry = s.entries[s.visibleEntries[i]] drawDirEntry(entry, diff --git a/src/keymap.nim b/src/keymap.nim index b1383a5..5c9cda2 100644 --- a/src/keymap.nim +++ b/src/keymap.nim @@ -1,6 +1,6 @@ import std/[streams, parsecfg, tables, os] -import nimbox +import nimboxext type Action* = enum @@ -136,7 +136,7 @@ proc keymapFromConfig*(): Keymap = discard close(p) -proc nimboxEventToAction*(event: nimbox.Event, keymap: Keymap): Action = +proc nimboxEventToAction*(event: nimboxext.Event, keymap: Keymap): Action = ## Decides whether an action is associated with this event and in that case, ## returns that action case event.kind: diff --git a/src/nimboxext.nim b/src/nimboxext.nim index 7a31fc4..aa823eb 100644 --- a/src/nimboxext.nim +++ b/src/nimboxext.nim @@ -6,26 +6,70 @@ import std/os import nimbox -template colors256Mode*(): bool = - ## Should the 256-colors mode be turned on? - existsEnv("NIMMM_256") +export + nimbox.Colors256, + nimbox.Modifier, + nimbox.Symbol, + nimbox.Mouse, + nimbox.EventType, + nimbox.Event, -template c8*(color: int): int = + nimbox.Color, + nimbox.Style, + nimbox.InputMode, + nimbox.OutputMode, + + nimbox.newNimbox, + nimbox.`inputMode=`, + nimbox.`outputMode=`, + nimbox.shutdown, + nimbox.width, + nimbox.height, + nimbox.clear, + nimbox.present, + nimbox.print, + nimbox.`cursor=`, + nimbox.peekEvent, + + nimbox.TB_HIDE_CURSOR + +type + Nimbox* = object of nimbox.Nimbox + enable256Colors: bool + +proc newNb*(enable256Colors: bool): Nimbox = + ## Wrapper for `newNimbox` + + # This is not optimal, but we use the fact that nimbox.Nimbox is just an empty object. Therefore, we do not use nb further + let nb = newNimbox() + nb.inputMode = inpEsc and inpMouse + if enable256Colors: + nb.outputMode = out256 + + Nimbox(enable256Colors: enable256Colors) + +template withoutNimbox*(nb: var Nimbox, body: untyped) = + let enable256Colors = nb.enable256Colors + nb.shutdown() + body + nb = newNb(enable256Colors) + +proc c8*(nb: Nimbox, color: int): int = ## Convert this color (`ck8`) into ## an int with regards to the current color mode - if colors256Mode(): + if nb.enable256Colors: color - 1 else: color -template c8*(color: Color): int = +proc c8*(nb: Nimbox, color: Color): int = ## Convert this color enum into an int ## with regards to the current color mode - c8(ord(color)) + nb.c8(ord(color)) -template fgHighlight*(): int = +proc fgHighlight*(nb: Nimbox): int = ## Provides a viable foreground color for highlighted items - if colors256Mode(): + if nb.enable256Colors: 16 else: ord(clrBlack) diff --git a/src/nimmm.nim b/src/nimmm.nim index 3ecaab0..44cb450 100644 --- a/src/nimmm.nim +++ b/src/nimmm.nim @@ -3,7 +3,6 @@ import std/[os, sets, parseopt, sequtils, algorithm, strutils, import posix, posix/inotify -import nimbox import lscolors import core, scan, draw, external, nimboxext, keymap, readline @@ -121,26 +120,13 @@ proc right(s: var State) = except: s.error = ErrCannotOpen -template newNb(enable256Colors: bool): Nimbox = - ## Wrapper for `newNimbox` - let nb = newNimbox() - nb.inputMode = inpEsc and inpMouse - if enable256Colors: - nb.outputMode = out256 - nb - -template withoutNimbox(nb: var Nimbox, enable256Colors: bool, body: untyped) = - nb.shutdown() - body - nb = newNb(enable256Colors) - -proc mainLoop(nb: var Nimbox, enable256Colors: bool) = +proc mainLoop(nb: var Nimbox) = let keymap = keyMapFromConfig() var s: State - events = newSeq[nimbox.Event]() + events = newSeq[nimboxext.Event]() terminalFile = open("/dev/tty") selector = newSelector[int]() @@ -195,7 +181,7 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) = case event.ch of 'y', 'Y', 'n', 'N': let yes = event.ch == 'y' or event.ch == 'Y' - withoutNimBox(nb, enable256Colors): + withoutNimBox(nb): case s.modeInfo.boolAction: of IBADelete: let pwdBackup = paths.getCurrentDir() @@ -218,7 +204,7 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) = of PrCanceled: s.modeInfo = ModeInfo(mode: MdNormal) of PrComplete: - withoutNimbox(nb, enable256Colors): + withoutNimbox(nb): let input = s.modeInfo.input case s.modeInfo.textAction: of ITANewFile: @@ -255,7 +241,7 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) = return of AcShell: let cwdBackup = paths.getCurrentDir() - withoutNimbox(nb, enable256Colors): + withoutNimbox(nb): spawnShell() s.safeSetCurDir(cwdBackup) s.rescan() @@ -331,13 +317,13 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) = of AcEdit: if not s.empty: if s.currentEntry.info.kind == pcFile: - withoutNimbox(nb, enable256Colors): + withoutNimbox(nb): editFile(s.currentEntry.path) s.rescan() of AcPager: if not s.empty: if s.currentEntry.info.kind == pcFile: - withoutNimbox(nb, enable256Colors): + withoutNimbox(nb): viewFile(s.currentEntry.path) of AcNewFile: s.modeInfo = ModeInfo(mode: MdInputText, @@ -352,13 +338,13 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) = textCursorPos: relativePath.len, textAction: ITARename) of AcCopySelected: - withoutNimbox(nb, enable256Colors): + withoutNimbox(nb): copyEntries(s.selected) s.selected.clear() s.rescan() of AcMoveSelected: let pwdBackup = paths.getCurrentDir() - withoutNimbox(nb, enable256Colors): + withoutNimbox(nb): moveEntries(s.selected) s.selected.clear() s.safeSetCurDir(pwdBackup) @@ -389,7 +375,7 @@ when isMainModule: setCurrentDir(p.key) else: continue - let enable256Colors = colors256Mode() + let enable256Colors = existsEnv("NIMMM_256") var nb = newNb(enable256Colors) addQuitProc(proc () {.noconv.} = nb.shutdown()) - mainLoop(nb, enable256Colors) + mainLoop(nb) diff --git a/src/readline.nim b/src/readline.nim index 538c245..5869275 100644 --- a/src/readline.nim +++ b/src/readline.nim @@ -1,6 +1,6 @@ import std/unicode -import nimbox +import nimboxext type ProcessInputTextModeResult* = enum @@ -8,7 +8,7 @@ type PrCanceled, PrComplete, -proc processInputTextMode*(event: nimbox.Event, +proc processInputTextMode*(event: nimboxext.Event, input: var string, cursorPos: var int): ProcessInputTextModeResult = ## common input processing for MdInputText and MdSearch