Skip to content

Commit

Permalink
Move to os/paths + remove usages of resetTab()
Browse files Browse the repository at this point in the history
resetTab() moves the current index to the top which can be annoying in
many situations
  • Loading branch information
joachimschmidt557 committed Feb 19, 2024
1 parent 5feac28 commit 84e81f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/core.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[os, sets]
import std/[os, sets, paths]

import lscolors/style

Expand All @@ -23,7 +23,7 @@ type

Tab* = object
## Represents a tab
cd*: string
cd*: Path
index*: int
searchQuery*: string

Expand Down Expand Up @@ -55,7 +55,7 @@ type
proc initState*(): State =
## Initializes the default startup state
State(error: ErrNone,
tabs: @[Tab(cd: getCurrentDir(), index: 0,
tabs: @[Tab(cd: paths.getCurrentDir(), index: 0,
searchQuery: "")],
currentTab: 0,
showHidden: false,
Expand Down
50 changes: 26 additions & 24 deletions src/nimmm.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/[os, sets, parseopt, sequtils, algorithm, strutils,
options, re, segfaults, atomics, unicode, selectors]
options, re, segfaults, atomics, unicode, selectors, dirs, paths]

import nimbox
import lscolors
Expand All @@ -12,12 +12,12 @@ proc getIndexOfItem(s: State, name: string): int =
i = paths.binarySearch(name, cmpPaths)
if i > 0 and s.visibleEntriesMask[i]: s.visibleEntries.binarySearch(i) else: 0

proc safeSetCurDir(s: var State, path: string) =
proc safeSetCurDir(s: var State, path: Path) =
var safeDir = path
while not dirExists(safeDir):
safeDir = safeDir.parentDir
setCurrentDir(safeDir)
s.tabs[s.currentTab].cd = getCurrentDir()
s.tabs[s.currentTab].cd = paths.getCurrentDir()

proc visible(entry: DirEntry, showHidden: bool, regex: Option[Regex]): bool =
let
Expand Down Expand Up @@ -47,10 +47,7 @@ proc refresh(s: var State) =
if visible: s.visibleEntries &= i

if s.visibleEntries.len > 0:
if s.currentIndex < 0:
s.currentIndex = 0
elif s.currentIndex > s.visibleEntries.high:
s.currentIndex = s.visibleEntries.high
s.currentIndex = clamp(s.currentIndex, 0, s.visibleEntries.high)

proc rescan(s: var State, lsc: LsColors) =
s.error = ErrNone
Expand Down Expand Up @@ -86,20 +83,19 @@ proc down(s: var State) =
s.currentIndex = 0

proc left(s: var State, lsc: LsColors) =
if parentDir(getCurrentDir()) == "":
if parentDir(paths.getCurrentDir()) == Path(""):
return
let prevDir = getCurrentDir()
s.safeSetCurDir(parentDir(getCurrentDir()))
s.resetTab()
let prevDir = os.getCurrentDir()
s.safeSetCurDir(parentDir(paths.getCurrentDir()))
s.rescan(lsc)
s.currentIndex = getIndexOfItem(s, prevDir)

proc right(s: var State, lsc: LsColors) =
if not s.empty:
if s.currentEntry.info.kind == pcDir:
let prev = getCurrentDir()
let prev = paths.getCurrentDir()
try:
s.safeSetCurDir(s.currentEntry.path)
s.safeSetCurDir(Path(s.currentEntry.path))
s.resetTab()
s.rescan(lsc)
except:
Expand Down Expand Up @@ -194,14 +190,15 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
of EventType.Key:
case event.sym
of Symbol.Escape:
s.resetTab()
s.modeInfo = ModeInfo(mode: MdNormal)
of Symbol.Character:
case event.ch
of 'y', 'Y', 'n', 'N':
let yes = event.ch == 'y' or event.ch == 'Y'
withoutNimBox(nb, enable256Colors):
s.modeInfo.callbackBool(yes)
s.resetTab()

s.modeInfo = ModeInfo(mode: MdNormal)
else:
discard
else:
Expand All @@ -212,11 +209,12 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
of MdInputText:
case processInputTextMode(event, s.modeInfo.input)
of PrCanceled:
s.resetTab()
s.modeInfo = ModeInfo(mode: MdNormal)
of PrComplete:
withoutNimbox(nb, enable256Colors):
s.modeInfo.callbackText(s.modeInfo.input)
s.resetTab()

s.modeInfo = ModeInfo(mode: MdNormal)
of PrNoAction:
discard
# Incremental search mode: Ignore keymap
Expand All @@ -237,7 +235,7 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
of AcQuit:
return
of AcShell:
let cwdBackup = getCurrentDir()
let cwdBackup = paths.getCurrentDir()
withoutNimbox(nb, enable256Colors):
spawnShell()
s.safeSetCurDir(cwdBackup)
Expand Down Expand Up @@ -270,11 +268,15 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
of AcRight:
s.right(lsc)
of AcHomeDir:
s.safeSetCurDir(getHomeDir())
s.resetTab()
s.rescan(lsc)
let
cd = paths.getCurrentDir()
home = Path(getHomeDir())
if cd != home:
s.safeSetCurDir(home)
s.currentIndex = 0
s.rescan(lsc)
of AcNewTab:
s.tabs.add(Tab(cd: getCurrentDir(),
s.tabs.add(Tab(cd: paths.getCurrentDir(),
index: s.currentIndex,
searchQuery: ""))
s.switchTab(lsc, s.tabs.high)
Expand Down Expand Up @@ -341,14 +343,14 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
s.selected.clear()
s.rescan(lsc)
of AcMoveSelected:
let pwdBackup = getCurrentDir()
let pwdBackup = paths.getCurrentDir()
withoutNimbox(nb, enable256Colors):
moveEntries(s.selected)
s.selected.clear()
s.safeSetCurDir(pwdBackup)
s.rescan(lsc)
of AcDeleteSelected:
let pwdBackup = getCurrentDir()
let pwdBackup = paths.getCurrentDir()

s.modeInfo = ModeInfo(mode: MdInputBool,
promptBool: "use force? [y/n]:",
Expand Down

0 comments on commit 84e81f4

Please sign in to comment.