lf/nav.go

763 lines
14 KiB
Go
Raw Normal View History

2016-08-13 12:49:04 +00:00
package main
import (
"bufio"
2016-08-13 12:49:04 +00:00
"errors"
"fmt"
"io"
2016-08-13 12:49:04 +00:00
"log"
"os"
"os/exec"
"path/filepath"
2016-11-10 21:18:56 +00:00
"sort"
"strconv"
2016-08-13 12:49:04 +00:00
"strings"
"time"
2016-08-13 12:49:04 +00:00
)
2016-12-18 15:01:45 +00:00
type linkState byte
const (
2016-12-17 21:47:37 +00:00
notLink linkState = iota
working
broken
)
2016-12-17 21:47:37 +00:00
type file struct {
os.FileInfo
2017-11-19 18:55:13 +00:00
linkState linkState
path string
count int
2016-08-13 12:49:04 +00:00
}
2016-12-17 21:47:37 +00:00
type filesSortable struct {
files []*file
2016-11-10 21:18:56 +00:00
less func(i, j int) bool
}
2016-12-17 21:47:37 +00:00
func (f filesSortable) Len() int { return len(f.files) }
func (f filesSortable) Swap(i, j int) { f.files[i], f.files[j] = f.files[j], f.files[i] }
func (f filesSortable) Less(i, j int) bool { return f.less(i, j) }
2016-11-10 21:18:56 +00:00
// TODO: Replace with `sort.SliceStable` once available
2016-12-17 21:47:37 +00:00
func sortFilesStable(files []*file, less func(i, j int) bool) {
sort.Stable(filesSortable{files: files, less: less})
2016-11-10 21:18:56 +00:00
}
2016-12-17 21:47:37 +00:00
func readdir(path string) ([]*file, error) {
f, err := os.Open(path)
2016-08-13 12:49:04 +00:00
if err != nil {
return nil, err
2016-08-13 12:49:04 +00:00
}
names, err := f.Readdirnames(-1)
f.Close()
2016-12-17 21:47:37 +00:00
fi := make([]*file, 0, len(names))
for _, filename := range names {
fpath := filepath.Join(path, filename)
lstat, lerr := os.Lstat(fpath)
if os.IsNotExist(lerr) {
continue
}
if lerr != nil {
return fi, lerr
}
2016-12-17 21:47:37 +00:00
var linkState linkState
if lstat.Mode()&os.ModeSymlink != 0 {
stat, serr := os.Stat(fpath)
if serr == nil {
2016-12-17 21:47:37 +00:00
linkState = working
lstat = stat
} else {
2016-12-17 21:47:37 +00:00
linkState = broken
log.Printf("getting link destination info: %s", serr)
}
}
2016-12-17 21:47:37 +00:00
fi = append(fi, &file{
FileInfo: lstat,
2017-11-19 18:55:13 +00:00
linkState: linkState,
path: fpath,
count: -1,
})
}
2017-11-19 18:55:13 +00:00
return fi, err
}
2016-08-13 12:49:04 +00:00
2016-12-17 21:47:37 +00:00
type dir struct {
loading bool // directory is loading from disk
loadTime time.Time // current loading or last load time
ind int // index of current entry in fi
pos int // position of current entry in ui
path string // full path of directory
fi []*file // displayed files in directory including or excluding hidden ones
all []*file // all files in directory including hidden ones (same array as fi)
2018-04-18 20:08:28 +00:00
sortType sortType // sort method and options from last sort
}
2017-11-19 18:55:13 +00:00
func newDir(path string) *dir {
time := time.Now()
fi, err := readdir(path)
if err != nil {
log.Printf("reading directory: %s", err)
}
2017-11-19 18:55:13 +00:00
return &dir{
loadTime: time,
path: path,
fi: fi,
all: fi,
2016-08-13 12:49:04 +00:00
}
}
2017-11-19 18:55:13 +00:00
func (dir *dir) renew() {
fi, err := readdir(dir.path)
if err != nil {
log.Printf("reading directory: %s", err)
}
dir.fi = fi
dir.all = fi
2016-08-13 12:49:04 +00:00
}
2017-11-19 18:55:13 +00:00
func (dir *dir) sort() {
2018-04-18 20:08:28 +00:00
dir.sortType = gOpts.sortType
dir.fi = dir.all
2018-04-18 20:08:28 +00:00
switch gOpts.sortType.method {
case naturalSort:
sortFilesStable(dir.fi, func(i, j int) bool {
return naturalLess(strings.ToLower(dir.fi[i].Name()), strings.ToLower(dir.fi[j].Name()))
})
2018-04-18 20:08:28 +00:00
case nameSort:
sortFilesStable(dir.fi, func(i, j int) bool {
return strings.ToLower(dir.fi[i].Name()) < strings.ToLower(dir.fi[j].Name())
})
2018-04-18 20:08:28 +00:00
case sizeSort:
sortFilesStable(dir.fi, func(i, j int) bool {
return dir.fi[i].Size() < dir.fi[j].Size()
})
2018-04-18 20:08:28 +00:00
case timeSort:
sortFilesStable(dir.fi, func(i, j int) bool {
return dir.fi[i].ModTime().Before(dir.fi[j].ModTime())
})
}
2018-04-18 20:08:28 +00:00
if gOpts.sortType.option&reverseSort != 0 {
for i, j := 0, len(dir.fi)-1; i < j; i, j = i+1, j-1 {
dir.fi[i], dir.fi[j] = dir.fi[j], dir.fi[i]
}
}
2018-04-18 20:08:28 +00:00
if gOpts.sortType.option&dirfirstSort != 0 {
sortFilesStable(dir.fi, func(i, j int) bool {
if dir.fi[i].IsDir() == dir.fi[j].IsDir() {
return i < j
}
return dir.fi[i].IsDir()
})
}
// when hidden option is disabled, we move hidden files to the
// beginning of our file list and then set the beginning of displayed
// files to the first non-hidden file in the list
2018-04-18 20:08:28 +00:00
if gOpts.sortType.option&hiddenSort == 0 {
sortFilesStable(dir.fi, func(i, j int) bool {
if dir.fi[i].Name()[0] == '.' && dir.fi[j].Name()[0] == '.' {
return i < j
}
return dir.fi[i].Name()[0] == '.'
})
for i, f := range dir.fi {
if f.Name()[0] != '.' {
dir.fi = dir.fi[i:]
return
}
}
dir.fi = dir.fi[len(dir.fi):]
}
}
2017-11-19 18:55:13 +00:00
func (dir *dir) name() string {
if len(dir.fi) == 0 {
return ""
}
return dir.fi[dir.ind].Name()
}
func (dir *dir) find(name string, height int) {
2016-08-13 12:49:04 +00:00
if len(dir.fi) == 0 {
dir.ind, dir.pos = 0, 0
return
}
dir.ind = min(dir.ind, len(dir.fi)-1)
2016-08-13 12:49:04 +00:00
2017-11-19 18:55:13 +00:00
if dir.fi[dir.ind].Name() != name {
2016-08-13 12:49:04 +00:00
for i, f := range dir.fi {
if f.Name() == name {
2017-11-19 18:55:13 +00:00
dir.ind = i
2016-08-13 12:49:04 +00:00
break
}
}
}
2018-04-15 15:18:39 +00:00
edge := min(min(height/2, gOpts.scrolloff), len(dir.fi)-dir.ind-1)
dir.pos = min(dir.ind, height-edge-1)
2016-08-13 12:49:04 +00:00
}
2016-12-17 21:47:37 +00:00
type nav struct {
dirs []*dir
dirChan chan *dir
regChan chan *reg
dirCache map[string]*dir
regCache map[string]*reg
saves map[string]bool
2017-11-19 18:55:13 +00:00
marks map[string]int
markInd int
height int
search string
2016-08-13 12:49:04 +00:00
}
func newNav(height int) *nav {
wd, err := os.Getwd()
if err != nil {
log.Printf("getting current directory: %s", err)
}
nav := &nav{
dirChan: make(chan *dir),
regChan: make(chan *reg),
dirCache: make(map[string]*dir),
regCache: make(map[string]*reg),
marks: make(map[string]int),
saves: make(map[string]bool),
markInd: 0,
height: height,
}
nav.getDirs(wd)
return nav
}
func (nav *nav) position() {
path := nav.currDir().path
for i := len(nav.dirs) - 2; i >= 0; i-- {
nav.dirs[i].find(filepath.Base(path), nav.height)
path = filepath.Dir(path)
}
}
func (nav *nav) getDirs(wd string) {
2016-12-17 21:47:37 +00:00
var dirs []*dir
2016-08-13 12:49:04 +00:00
for curr, base := wd, ""; !isRoot(base); curr, base = filepath.Dir(curr), filepath.Base(curr) {
dir := nav.loadDir(curr)
dir.find(base, nav.height)
2016-08-13 12:49:04 +00:00
dirs = append(dirs, dir)
}
for i, j := 0, len(dirs)-1; i < j; i, j = i+1, j-1 {
dirs[i], dirs[j] = dirs[j], dirs[i]
}
nav.dirs = dirs
2016-08-13 12:49:04 +00:00
}
2018-04-15 15:18:39 +00:00
func (nav *nav) renew() {
nav.dirCache = make(map[string]*dir)
for _, d := range nav.dirs {
nav.dirCache[d.path] = d
}
2016-08-13 12:49:04 +00:00
for _, d := range nav.dirs {
go func(d *dir) {
s, err := os.Stat(d.path)
if err != nil {
log.Printf("getting directory info: %s", err)
}
if d.loadTime.After(s.ModTime()) {
return
}
d.loadTime = time.Now()
nd := newDir(d.path)
nd.sort()
nav.dirChan <- nd
}(d)
2016-08-13 12:49:04 +00:00
}
for m := range nav.marks {
if _, err := os.Stat(m); os.IsNotExist(err) {
delete(nav.marks, m)
}
}
if len(nav.marks) == 0 {
nav.markInd = 0
}
2016-08-13 12:49:04 +00:00
}
func (nav *nav) reload() {
nav.dirCache = make(map[string]*dir)
nav.regCache = make(map[string]*reg)
wd, err := os.Getwd()
if err != nil {
log.Printf("getting current directory: %s", err)
}
curr, err := nav.currFile()
nav.getDirs(wd)
if err == nil {
last := nav.dirs[len(nav.dirs)-1]
last.fi = append(last.fi, curr)
}
}
func (nav *nav) loadDir(path string) *dir {
d, ok := nav.dirCache[path]
if !ok {
go func() {
d := newDir(path)
d.sort()
d.ind, d.pos = 0, 0
nav.dirChan <- d
}()
2018-04-20 18:28:56 +00:00
d := &dir{loading: true, path: path, sortType: gOpts.sortType}
nav.dirCache[path] = d
return d
}
2018-04-20 18:28:56 +00:00
2018-04-18 20:08:28 +00:00
if d.sortType != gOpts.sortType {
2018-04-20 18:28:56 +00:00
go func() {
d.loading = true
name := d.name()
d.sort()
d.find(name, nav.height)
d.loading = false
nav.dirChan <- d
}()
2018-04-18 20:08:28 +00:00
}
2018-04-20 18:28:56 +00:00
return d
}
func (nav *nav) preview() {
curr, err := nav.currFile()
if err != nil {
return
}
var reader io.Reader
if len(gOpts.previewer) != 0 {
cmd := exec.Command(gOpts.previewer, curr.path, strconv.Itoa(nav.height))
out, err := cmd.StdoutPipe()
if err != nil {
log.Printf("previewing file: %s", err)
}
if err := cmd.Start(); err != nil {
log.Printf("previewing file: %s", err)
}
defer cmd.Wait()
defer out.Close()
reader = out
} else {
f, err := os.Open(curr.path)
if err != nil {
log.Printf("opening file: %s", err)
}
defer f.Close()
reader = f
}
reg := &reg{path: curr.path}
buf := bufio.NewScanner(reader)
for i := 0; i < nav.height && buf.Scan(); i++ {
for _, r := range buf.Text() {
if r == 0 {
reg.lines = []string{"\033[1mbinary\033[0m"}
nav.regChan <- reg
return
}
}
reg.lines = append(reg.lines, buf.Text())
}
if buf.Err() != nil {
log.Printf("loading file: %s", buf.Err())
}
nav.regChan <- reg
}
func (nav *nav) loadReg(ui *ui, path string) *reg {
r, ok := nav.regCache[path]
if !ok {
go nav.preview()
r := &reg{path: path, lines: []string{"\033[1mloading...\033[0m"}}
nav.regCache[path] = r
return r
}
return r
}
func (nav *nav) sort() {
for _, d := range nav.dirs {
2017-11-19 18:55:13 +00:00
name := d.name()
d.sort()
d.find(name, nav.height)
}
}
2016-12-17 21:47:37 +00:00
func (nav *nav) up(dist int) {
2016-08-14 15:39:02 +00:00
dir := nav.currDir()
if dir.ind == 0 {
return
}
dir.ind -= dist
dir.ind = max(0, dir.ind)
2016-08-14 15:39:02 +00:00
dir.pos -= dist
2018-04-15 15:18:39 +00:00
edge := min(min(nav.height/2, gOpts.scrolloff), dir.ind)
2016-08-14 15:39:02 +00:00
dir.pos = max(dir.pos, edge)
}
2016-12-17 21:47:37 +00:00
func (nav *nav) down(dist int) {
2016-08-13 12:49:04 +00:00
dir := nav.currDir()
maxind := len(dir.fi) - 1
if dir.ind >= maxind {
return
}
dir.ind += dist
dir.ind = min(maxind, dir.ind)
2016-08-13 12:49:04 +00:00
dir.pos += dist
2018-04-15 15:18:39 +00:00
edge := min(min(nav.height/2, gOpts.scrolloff), maxind-dir.ind)
2016-09-02 19:47:11 +00:00
// use a smaller value when the height is even and scrolloff is maxed
// in order to stay at the same row as much as possible while up/down
2016-09-02 20:06:25 +00:00
edge = min(edge, nav.height/2+nav.height%2-1)
2016-09-02 19:47:11 +00:00
2016-08-13 12:49:04 +00:00
dir.pos = min(dir.pos, nav.height-edge-1)
dir.pos = min(dir.pos, maxind)
}
2016-12-17 21:47:37 +00:00
func (nav *nav) updir() error {
2016-08-13 12:49:04 +00:00
if len(nav.dirs) <= 1 {
return nil
}
dir := nav.currDir()
nav.dirs = nav.dirs[:len(nav.dirs)-1]
if err := os.Chdir(filepath.Dir(dir.path)); err != nil {
2016-08-13 12:49:04 +00:00
return fmt.Errorf("updir: %s", err)
}
return nil
}
2016-12-17 21:47:37 +00:00
func (nav *nav) open() error {
curr, err := nav.currFile()
if err != nil {
2016-12-18 15:01:45 +00:00
return fmt.Errorf("open: %s", err)
}
2016-12-18 15:01:45 +00:00
2017-11-19 18:55:13 +00:00
path := curr.path
2016-08-13 12:49:04 +00:00
dir := nav.loadDir(path)
2016-08-13 12:49:04 +00:00
2016-08-14 12:37:22 +00:00
nav.dirs = append(nav.dirs, dir)
2016-08-13 12:49:04 +00:00
2016-08-17 20:28:42 +00:00
if err := os.Chdir(path); err != nil {
2016-08-14 12:37:22 +00:00
return fmt.Errorf("open: %s", err)
2016-08-13 12:49:04 +00:00
}
return nil
}
2016-12-17 21:47:37 +00:00
func (nav *nav) top() {
2016-08-13 12:49:04 +00:00
dir := nav.currDir()
dir.ind = 0
dir.pos = 0
}
2017-11-19 18:55:13 +00:00
func (nav *nav) bot() {
dir := nav.currDir()
2017-11-19 18:55:13 +00:00
dir.ind = len(dir.fi) - 1
dir.pos = min(dir.ind, nav.height-1)
}
2016-12-17 21:47:37 +00:00
func (nav *nav) toggleMark(path string) {
if _, ok := nav.marks[path]; ok {
2016-08-13 12:49:04 +00:00
delete(nav.marks, path)
if len(nav.marks) == 0 {
nav.markInd = 0
}
2016-08-13 12:49:04 +00:00
} else {
nav.marks[path] = nav.markInd
nav.markInd = nav.markInd + 1
2016-08-13 12:49:04 +00:00
}
}
2016-12-17 21:47:37 +00:00
func (nav *nav) toggle() {
curr, err := nav.currFile()
if err != nil {
return
}
2017-11-19 18:55:13 +00:00
nav.toggleMark(curr.path)
2016-08-13 12:49:04 +00:00
nav.down(1)
2016-08-13 12:49:04 +00:00
}
2016-12-17 21:47:37 +00:00
func (nav *nav) invert() {
last := nav.currDir()
for _, f := range last.fi {
path := filepath.Join(last.path, f.Name())
nav.toggleMark(path)
}
}
func (nav *nav) unmark() {
nav.marks = make(map[string]int)
nav.markInd = 0
}
2016-12-17 21:47:37 +00:00
func (nav *nav) save(copy bool) error {
2016-08-13 12:49:04 +00:00
if len(nav.marks) == 0 {
curr, err := nav.currFile()
if err != nil {
return errors.New("no file selected")
}
2017-11-19 18:55:13 +00:00
if err := saveFiles([]string{curr.path}, copy); err != nil {
2016-08-13 12:49:04 +00:00
return err
}
nav.saves = make(map[string]bool)
2017-11-19 18:55:13 +00:00
nav.saves[curr.path] = copy
2016-08-13 12:49:04 +00:00
} else {
2017-11-19 18:55:13 +00:00
marks := nav.currMarks()
2016-08-13 12:49:04 +00:00
2017-11-19 18:55:13 +00:00
if err := saveFiles(marks, copy); err != nil {
2016-08-13 12:49:04 +00:00
return err
}
nav.saves = make(map[string]bool)
for f := range nav.marks {
nav.saves[f] = copy
}
2016-08-13 12:49:04 +00:00
}
return nil
}
2016-12-17 21:47:37 +00:00
func (nav *nav) put() error {
list, copy, err := loadFiles()
2016-08-13 12:49:04 +00:00
if err != nil {
return err
}
if len(list) == 0 {
return errors.New("no file in yank/delete buffer")
}
dir := nav.currDir()
cmd := putCommand(list, dir, copy)
2016-08-13 12:49:04 +00:00
if err := cmd.Run(); err != nil {
return fmt.Errorf("putting files: %s", err)
2016-08-13 12:49:04 +00:00
}
2018-03-22 14:54:24 +00:00
if err := saveFiles(nil, false); err != nil {
return fmt.Errorf("clearing yank/delete buffer: %s", err)
}
2016-08-13 12:49:04 +00:00
return nil
}
2016-12-17 21:47:37 +00:00
func (nav *nav) sync() error {
list, copy, err := loadFiles()
if err != nil {
return err
}
nav.saves = make(map[string]bool)
for _, f := range list {
nav.saves[f] = copy
}
return nil
}
2017-11-19 18:55:13 +00:00
func (nav *nav) cd(wd string) error {
wd = strings.Replace(wd, "~", gUser.HomeDir, -1)
wd = filepath.Clean(wd)
if !filepath.IsAbs(wd) {
wd = filepath.Join(nav.currDir().path, wd)
}
if err := os.Chdir(wd); err != nil {
return fmt.Errorf("cd: %s", err)
}
nav.getDirs(wd)
return nil
}
func (nav *nav) find(path string) error {
lstat, err := os.Stat(path)
if err != nil {
return fmt.Errorf("select: %s", err)
}
dir := filepath.Dir(path)
if err := nav.cd(dir); err != nil {
return fmt.Errorf("select: %s", err)
}
base := filepath.Base(path)
last := nav.dirs[len(nav.dirs)-1]
if last.loading {
last.fi = append(last.fi, &file{FileInfo: lstat})
} else {
last.find(base, nav.height)
}
return nil
}
2017-11-19 18:55:13 +00:00
func match(pattern, name string) (matched bool, err error) {
if gOpts.ignorecase {
lpattern := strings.ToLower(pattern)
if !gOpts.smartcase || lpattern == pattern {
pattern = lpattern
name = strings.ToLower(name)
}
}
if gOpts.globsearch {
return filepath.Match(pattern, name)
}
return strings.Contains(name, pattern), nil
}
func (nav *nav) searchNext() error {
last := nav.currDir()
for i := last.ind + 1; i < len(last.fi); i++ {
matched, err := match(nav.search, last.fi[i].Name())
if err != nil {
return err
}
if matched {
nav.down(i - last.ind)
return nil
}
}
if gOpts.wrapscan {
for i := 0; i < last.ind; i++ {
matched, err := match(nav.search, last.fi[i].Name())
if err != nil {
return err
}
if matched {
nav.up(last.ind - i)
return nil
}
}
}
return nil
}
func (nav *nav) searchPrev() error {
last := nav.currDir()
for i := last.ind - 1; i >= 0; i-- {
matched, err := match(nav.search, last.fi[i].Name())
if err != nil {
return err
}
if matched {
nav.up(last.ind - i)
return nil
}
}
if gOpts.wrapscan {
for i := len(last.fi) - 1; i > last.ind; i-- {
matched, err := match(nav.search, last.fi[i].Name())
if err != nil {
return err
}
if matched {
nav.down(i - last.ind)
return nil
}
}
}
return nil
}
2016-12-17 21:47:37 +00:00
func (nav *nav) currDir() *dir {
2016-08-13 12:49:04 +00:00
return nav.dirs[len(nav.dirs)-1]
}
2016-12-17 21:47:37 +00:00
func (nav *nav) currFile() (*file, error) {
2016-08-13 12:49:04 +00:00
last := nav.dirs[len(nav.dirs)-1]
if len(last.fi) == 0 {
return nil, fmt.Errorf("empty directory")
}
return last.fi[last.ind], nil
2016-08-13 12:49:04 +00:00
}
2017-02-11 13:34:18 +00:00
type indexedMarks struct {
paths []string
indices []int
}
2017-02-11 13:34:18 +00:00
func (m indexedMarks) Len() int { return len(m.paths) }
func (m indexedMarks) Swap(i, j int) {
m.paths[i], m.paths[j] = m.paths[j], m.paths[i]
m.indices[i], m.indices[j] = m.indices[j], m.indices[i]
}
2017-02-11 13:34:18 +00:00
func (m indexedMarks) Less(i, j int) bool { return m.indices[i] < m.indices[j] }
2016-12-17 21:47:37 +00:00
func (nav *nav) currMarks() []string {
paths := make([]string, 0, len(nav.marks))
indices := make([]int, 0, len(nav.marks))
for path, index := range nav.marks {
paths = append(paths, path)
indices = append(indices, index)
}
2017-02-11 13:34:18 +00:00
sort.Sort(indexedMarks{paths: paths, indices: indices})
return paths
2016-08-13 12:49:04 +00:00
}