Skip to content

Commit

Permalink
move input prompts to draw functionality, replace with action enum
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimschmidt557 committed Jul 20, 2024
1 parent 76cc61c commit 86b0115
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@ type
MdInputBool,
MdSearch,

InputTextAction* = enum
## Actions run on completion of text input
ITANewFile,
ITANewDir,
ITARename,

InputBoolAction* = enum
## Actions run on completion of boolinput
IBADelete,

ModeInfo* = object
case mode*: Mode
of MdNormal: discard
of MdSearch:
searchCursorPos*: int = 0
of MdInputText:
promptText*: string
input*: string = ""
textCursorPos*: int = 0
textAction*: InputTextAction
callbackText*: proc (input: string)
of MdInputBool:
promptBool*: string
boolAction*: InputBoolAction
callbackBool*: proc (input: bool)

Tab* = object
Expand Down
16 changes: 14 additions & 2 deletions src/draw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ proc errMsg(err: ErrorKind): string =
of ErrCannotShow: "Some entries couldn't be displayed"
of ErrCannotOpen: "Cannot open file"

proc textInputPrompt(textAction: InputTextAction): string =
case textAction
of ITANewFile: "new file:"
of ITANewDir: "new directory:"
of ITARename: "rename:"

proc boolInputPrompt(boolAction: InputBoolAction): string =
case boolAction
of IBADelete: "use force? [y/n]:"

proc redraw*(s: State, nb: var Nimbox) =
let
topIndex = getTopIndex(s.visibleEntries.len, s.currentIndex, nb)
Expand Down Expand Up @@ -220,9 +230,11 @@ proc redraw*(s: State, nb: var Nimbox) =
drawInputFooter("search:", s.currentSearchQuery,
s.modeInfo.searchCursorPos, nb)
of MdInputText:
drawInputFooter(s.modeInfo.promptText, s.modeInfo.input,
let prompt = textInputPrompt(s.modeInfo.textAction)
drawInputFooter(prompt, s.modeInfo.input,
s.modeInfo.textCursorPos, nb)
of MdInputBool:
drawInputFooter(s.modeInfo.promptBool, "", 0, nb)
let prompt = boolInputPrompt(s.modeInfo.boolAction)
drawInputFooter(prompt, "", 0, nb)

nb.present()
12 changes: 6 additions & 6 deletions src/nimmm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ proc right(s: var State, inotifyHandle: var FileHandle, currentDirWatcher: var c
except:
s.error = ErrCannotOpen

template newNb*(enable256Colors: bool): Nimbox =
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) =
template withoutNimbox(nb: var Nimbox, enable256Colors: bool, body: untyped) =
nb.shutdown()
body
nb = newNb(enable256Colors)
Expand Down Expand Up @@ -307,21 +307,21 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
viewFile(s.currentEntry.path)
of AcNewFile:
s.modeInfo = ModeInfo(mode: MdInputText,
promptText: "new file:",
textAction: ITANewFile,
callbackText: proc (input: string) =
newFile(input)
s.rescan(lsc))
of AcNewDir:
s.modeInfo = ModeInfo(mode: MdInputText,
promptText: "new directory:",
textAction: ITANewDir,
callbackText: proc (input: string) =
newDir(input)
s.rescan(lsc))
of AcRename:
let relativePath = extractFilename(s.currentEntry.path)
s.modeInfo = ModeInfo(mode: MdInputText,
promptText: "rename to:",
input: relativePath,
textAction: ITARename,
callbackText: proc (input: string) =
rename(relativePath, input)
s.rescan(lsc))
Expand All @@ -341,7 +341,7 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
let pwdBackup = paths.getCurrentDir()

s.modeInfo = ModeInfo(mode: MdInputBool,
promptBool: "use force? [y/n]:",
boolAction: IBADelete,
callBackBool: proc (input: bool) =
deleteEntries(s.selected, input)
s.selected.clear()
Expand Down

0 comments on commit 86b0115

Please sign in to comment.