Reduce logic when updating files
This commit is contained in:
1 file changed
+10
-55
@@ -463,21 +463,21 @@ func (app *app) loop() {
|
||||
app.ui.draw(app.nav)
|
||||
case ev := <-app.nav.watcherEvents:
|
||||
if ev.Has(fsnotify.Create) {
|
||||
app.addFile(newFile(ev.Name))
|
||||
app.changeFile(newFile(filepath.Dir(ev.Name)))
|
||||
app.updateFile(ev.Name, true)
|
||||
app.updateFile(filepath.Dir(ev.Name), true)
|
||||
app.ui.loadFile(app, false)
|
||||
app.ui.draw(app.nav)
|
||||
}
|
||||
|
||||
if ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
|
||||
app.removeFile(ev.Name)
|
||||
app.changeFile(newFile(filepath.Dir(ev.Name)))
|
||||
app.updateFile(ev.Name, false)
|
||||
app.updateFile(filepath.Dir(ev.Name), true)
|
||||
app.ui.loadFile(app, false)
|
||||
app.ui.draw(app.nav)
|
||||
}
|
||||
|
||||
if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) {
|
||||
app.changeFile(newFile(ev.Name))
|
||||
app.updateFile(ev.Name, true)
|
||||
currFile, err := app.nav.currFile()
|
||||
if err == nil && currFile.path == ev.Name {
|
||||
app.nav.startPreview()
|
||||
@@ -620,56 +620,7 @@ func (app *app) runShell(s string, args []string, prefix string) {
|
||||
|
||||
}
|
||||
|
||||
func (app *app) addFile(f *file) {
|
||||
dirs := app.nav.dirs
|
||||
if app.ui.dirPrev != nil {
|
||||
dirs = append(dirs, app.ui.dirPrev)
|
||||
}
|
||||
|
||||
for _, dir := range dirs {
|
||||
if dir.path != filepath.Dir(f.path) {
|
||||
continue
|
||||
}
|
||||
|
||||
for i := range dir.allFiles {
|
||||
if dir.allFiles[i].path == f.path {
|
||||
dir.allFiles[i] = f
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
dir.allFiles = append(dir.allFiles, f)
|
||||
name := dir.name()
|
||||
dir.sort()
|
||||
dir.sel(name, app.nav.height)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *app) changeFile(f *file) {
|
||||
dirs := app.nav.dirs
|
||||
if app.ui.dirPrev != nil {
|
||||
dirs = append(dirs, app.ui.dirPrev)
|
||||
}
|
||||
|
||||
for _, dir := range dirs {
|
||||
if dir.path != filepath.Dir(f.path) {
|
||||
continue
|
||||
}
|
||||
|
||||
for i := range dir.allFiles {
|
||||
if dir.allFiles[i].path == f.path {
|
||||
dir.allFiles[i] = f
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
name := dir.name()
|
||||
dir.sort()
|
||||
dir.sel(name, app.nav.height)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *app) removeFile(path string) {
|
||||
func (app *app) updateFile(path string, add bool) {
|
||||
dirs := app.nav.dirs
|
||||
if app.ui.dirPrev != nil {
|
||||
dirs = append(dirs, app.ui.dirPrev)
|
||||
@@ -687,7 +638,11 @@ func (app *app) removeFile(path string) {
|
||||
}
|
||||
}
|
||||
|
||||
if add {
|
||||
allFiles = append(allFiles, newFile(path))
|
||||
}
|
||||
dir.allFiles = allFiles
|
||||
|
||||
name := dir.name()
|
||||
dir.sort()
|
||||
dir.sel(name, app.nav.height)
|
||||
|
||||
Reference in new issue
Block a user