Implement file changes
This commit is contained in:
2 files changed
+120
-85
No files matched your search
@@ -424,6 +424,29 @@ func (app *app) loop() {
|
||||
}
|
||||
}
|
||||
|
||||
app.ui.draw(app.nav)
|
||||
case f := <-app.nav.fileChan:
|
||||
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)
|
||||
}
|
||||
app.ui.loadFile(app, false)
|
||||
app.ui.draw(app.nav)
|
||||
case ev := <-app.ui.evChan:
|
||||
e := app.ui.readEvent(ev, app.nav)
|
||||
@@ -464,6 +487,18 @@ func (app *app) loop() {
|
||||
app.ui.loadFile(app, false)
|
||||
app.ui.draw(app.nav)
|
||||
}
|
||||
|
||||
if ev.Has(fsnotify.Write) {
|
||||
go func() {
|
||||
app.nav.fileChan <- newFile(ev.Name)
|
||||
}()
|
||||
currFile, err := app.nav.currFile()
|
||||
if err == nil && currFile.path == ev.Name {
|
||||
app.nav.startPreview()
|
||||
app.nav.previewChan <- ev.Name
|
||||
}
|
||||
app.ui.draw(app.nav)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,88 @@ type file struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func newFile(path string) *file {
|
||||
lstat, err := os.Lstat(path)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("getting file information: %s", err)
|
||||
return &file{
|
||||
FileInfo: &fakeStat{name: filepath.Base(path)},
|
||||
linkState: notLink,
|
||||
linkTarget: "",
|
||||
path: path,
|
||||
dirCount: -1,
|
||||
dirSize: -1,
|
||||
accessTime: time.Unix(0, 0),
|
||||
changeTime: time.Unix(0, 0),
|
||||
ext: filepath.Ext(path),
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
var linkState linkState
|
||||
var linkTarget string
|
||||
|
||||
if lstat.Mode()&os.ModeSymlink != 0 {
|
||||
stat, err := os.Stat(path)
|
||||
if err == nil {
|
||||
linkState = working
|
||||
lstat = stat
|
||||
} else {
|
||||
linkState = broken
|
||||
}
|
||||
linkTarget, err = os.Readlink(path)
|
||||
if err != nil {
|
||||
log.Printf("reading link target: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
ts := times.Get(lstat)
|
||||
at := ts.AccessTime()
|
||||
var ct time.Time
|
||||
// from times docs: ChangeTime() panics unless HasChangeTime() is true
|
||||
if ts.HasChangeTime() {
|
||||
ct = ts.ChangeTime()
|
||||
} else {
|
||||
// fall back to ModTime if ChangeTime cannot be determined
|
||||
ct = lstat.ModTime()
|
||||
}
|
||||
|
||||
// returns an empty string if extension could not be determined
|
||||
// i.e. directories, filenames without extensions
|
||||
ext := filepath.Ext(path)
|
||||
|
||||
dirCount := -1
|
||||
if lstat.IsDir() && gOpts.dircounts {
|
||||
d, err := os.Open(path)
|
||||
if err != nil {
|
||||
dirCount = -2
|
||||
} else {
|
||||
names, err := d.Readdirnames(1000)
|
||||
d.Close()
|
||||
|
||||
if names == nil && err != io.EOF {
|
||||
dirCount = -2
|
||||
} else {
|
||||
dirCount = len(names)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &file{
|
||||
FileInfo: lstat,
|
||||
linkState: linkState,
|
||||
linkTarget: linkTarget,
|
||||
path: path,
|
||||
dirCount: dirCount,
|
||||
dirSize: -1,
|
||||
accessTime: at,
|
||||
changeTime: ct,
|
||||
ext: ext,
|
||||
err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (file *file) TotalSize() int64 {
|
||||
if file.IsDir() {
|
||||
if file.dirSize >= 0 {
|
||||
@@ -71,91 +153,7 @@ func readdir(path string) ([]*file, error) {
|
||||
|
||||
files := make([]*file, 0, len(names))
|
||||
for _, fname := range names {
|
||||
fpath := filepath.Join(path, fname)
|
||||
|
||||
lstat, err := os.Lstat(fpath)
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("getting file information: %s", err)
|
||||
files = append(files, &file{
|
||||
FileInfo: &fakeStat{name: fname},
|
||||
linkState: notLink,
|
||||
linkTarget: "",
|
||||
path: fpath,
|
||||
dirCount: -1,
|
||||
dirSize: -1,
|
||||
accessTime: time.Unix(0, 0),
|
||||
changeTime: time.Unix(0, 0),
|
||||
ext: filepath.Ext(fpath),
|
||||
err: err,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
var linkState linkState
|
||||
var linkTarget string
|
||||
|
||||
if lstat.Mode()&os.ModeSymlink != 0 {
|
||||
stat, err := os.Stat(fpath)
|
||||
if err == nil {
|
||||
linkState = working
|
||||
lstat = stat
|
||||
} else {
|
||||
linkState = broken
|
||||
}
|
||||
linkTarget, err = os.Readlink(fpath)
|
||||
if err != nil {
|
||||
log.Printf("reading link target: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
ts := times.Get(lstat)
|
||||
at := ts.AccessTime()
|
||||
var ct time.Time
|
||||
// from times docs: ChangeTime() panics unless HasChangeTime() is true
|
||||
if ts.HasChangeTime() {
|
||||
ct = ts.ChangeTime()
|
||||
} else {
|
||||
// fall back to ModTime if ChangeTime cannot be determined
|
||||
ct = lstat.ModTime()
|
||||
}
|
||||
|
||||
// returns an empty string if extension could not be determined
|
||||
// i.e. directories, filenames without extensions
|
||||
ext := filepath.Ext(fpath)
|
||||
|
||||
dirCount := -1
|
||||
if lstat.IsDir() && gOpts.dircounts {
|
||||
d, err := os.Open(fpath)
|
||||
if err != nil {
|
||||
dirCount = -2
|
||||
} else {
|
||||
names, err := d.Readdirnames(1000)
|
||||
d.Close()
|
||||
|
||||
if names == nil && err != io.EOF {
|
||||
dirCount = -2
|
||||
} else {
|
||||
dirCount = len(names)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
files = append(files, &file{
|
||||
FileInfo: lstat,
|
||||
linkState: linkState,
|
||||
linkTarget: linkTarget,
|
||||
path: fpath,
|
||||
dirCount: dirCount,
|
||||
dirSize: -1,
|
||||
accessTime: at,
|
||||
changeTime: ct,
|
||||
ext: ext,
|
||||
err: nil,
|
||||
})
|
||||
files = append(files, newFile(filepath.Join(path, fname)))
|
||||
}
|
||||
|
||||
return files, err
|
||||
@@ -445,6 +443,7 @@ type nav struct {
|
||||
dirPreviewChan chan *dir
|
||||
dirChan chan *dir
|
||||
regChan chan *reg
|
||||
fileChan chan *file
|
||||
dirCache map[string]*dir
|
||||
regCache map[string]*reg
|
||||
saves map[string]bool
|
||||
@@ -595,6 +594,7 @@ func newNav(height int) *nav {
|
||||
dirPreviewChan: make(chan *dir, 1024),
|
||||
dirChan: make(chan *dir),
|
||||
regChan: make(chan *reg),
|
||||
fileChan: make(chan *file),
|
||||
dirCache: make(map[string]*dir),
|
||||
regCache: make(map[string]*reg),
|
||||
saves: make(map[string]bool),
|
||||
|
||||
Reference in new issue
Block a user