Skip to content

Commit

Permalink
nimbox: completely wrap Nimbox in nimboxext
Browse files Browse the repository at this point in the history
- unused (and potentially conflicting) functions such as nimbox.`add`
are not included now
- environment variable `NIMMM_256` is only checked once now at startup
  • Loading branch information
joachimschmidt557 committed Jul 21, 2024
1 parent 0c80087 commit 4be86b8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 60 deletions.
43 changes: 21 additions & 22 deletions src/draw.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import std/[times, sets, os, strformat, strutils, options]

import nimbox
import lscolors/style
import wcwidth

Expand Down Expand Up @@ -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
Expand All @@ -77,36 +76,36 @@ 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:
sty.fg.get.lsColorToNimboxColor()
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,
Expand All @@ -125,26 +124,26 @@ 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)

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) =
Expand All @@ -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,
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/keymap.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[streams, parsecfg, tables, os]

import nimbox
import nimboxext

type
Action* = enum
Expand Down Expand Up @@ -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:
Expand Down
62 changes: 53 additions & 9 deletions src/nimboxext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 11 additions & 25 deletions src/nimmm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]()

Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/readline.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import std/unicode

import nimbox
import nimboxext

type
ProcessInputTextModeResult* = enum
PrNoAction,
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
Expand Down

0 comments on commit 4be86b8

Please sign in to comment.