Skip to content

Commit

Permalink
fix option processing, async dirs & start select (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak authored Feb 27, 2022
1 parent b4637f9 commit fb66a21
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 142 deletions.
105 changes: 91 additions & 14 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ type app struct {
cmdHistoryInd int
}

var gInvalidate struct {
sort bool
pos bool
dir bool
navSize bool
mouse bool
period bool
}

func init() {
gInvalidate.sort = true
gInvalidate.pos = true
gInvalidate.dir = true
gInvalidate.navSize = true
gInvalidate.mouse = true
gInvalidate.period = true
}

func newApp(ui *ui, nav *nav) *app {
quitChan := make(chan struct{}, 1)

Expand Down Expand Up @@ -245,19 +263,6 @@ func (app *app) loop() {

app.ui.readExpr()

if gSelect != "" {
go func() {
lstat, err := os.Lstat(gSelect)
if err != nil {
app.ui.exprChan <- &callExpr{"echoerr", []string{err.Error()}, 1}
} else if lstat.IsDir() {
app.ui.exprChan <- &callExpr{"cd", []string{gSelect}, 1}
} else {
app.ui.exprChan <- &callExpr{"select", []string{gSelect}, 1}
}
}()
}

if gConfigPath != "" {
if _, err := os.Stat(gConfigPath); !os.IsNotExist(err) {
app.readFile(gConfigPath)
Expand All @@ -272,6 +277,30 @@ func (app *app) loop() {
}
}

// config has been read, now initialize nav with the wd
wd, err := os.Getwd()
if err != nil {
log.Printf("getting current directory: %s", err)
}
if gSelect != "" {
_, err := os.Lstat(gSelect)
if err != nil {
app.ui.exprChan <- &callExpr{"echoerr", []string{err.Error()}, 1}
} else if abs, err := filepath.Abs(gSelect); err == nil {
// if gSelect contains the /. suffix, the directory itself
// should be selected
if len(gSelect) > 2 && gSelect[len(gSelect)-2:] == "/." {
wd = abs
} else {
wd = filepath.Dir(abs)
app.ui.exprChan <- &callExpr{"select", []string{abs}, 1}
}
}
}
app.nav.getDirs(wd)
app.nav.addJumpList()

// execute commands from args
for _, cmd := range gCommands {
p := newParser(strings.NewReader(cmd))

Expand All @@ -285,6 +314,49 @@ func (app *app) loop() {
}

for {

// process invalidate flags
if gInvalidate.sort {
app.nav.sort()
app.ui.sort()
gInvalidate.sort = false
}
if gInvalidate.pos {
app.nav.position()
gInvalidate.pos = false
}
if gInvalidate.dir {
app.ui.loadFile(app.nav, true)
app.ui.loadFileInfo(app.nav)
app.ui.draw(app.nav)
gInvalidate.dir = false
}
if gInvalidate.navSize {
app.ui.renew()
if app.nav.height != app.ui.wins[0].h {
app.nav.height = app.ui.wins[0].h
app.nav.regCache = make(map[string]*reg)
}
gInvalidate.navSize = false
}
if gInvalidate.mouse {
if gOpts.mouse {
app.ui.screen.EnableMouse()
} else {
app.ui.screen.DisableMouse()
}
gInvalidate.mouse = false
}
if gInvalidate.period {
if gOpts.period == 0 {
app.ticker.Stop()
} else {
app.ticker.Stop()
app.ticker = time.NewTicker(time.Duration(gOpts.period) * time.Second)
}
gInvalidate.period = false
}

select {
case <-app.quitChan:
if app.nav.copyTotal > 0 {
Expand Down Expand Up @@ -369,7 +441,12 @@ func (app *app) loop() {
}
app.ui.draw(app.nav)
case d := <-app.nav.dirChan:
app.nav.checkDir(d)

if !app.nav.checkDir(d) {
log.Printf("debug: dirChan skip/reload %s", d.path)
continue
}
log.Printf("debug: dirChan %s %t", d.path, d.loading)

if gOpts.dircache {
prev, ok := app.nav.dirCache[d.path]
Expand Down
Loading

0 comments on commit fb66a21

Please sign in to comment.