This commit is contained in:
Gokcehan 2020-07-20 02:47:33 +03:00
parent 9539daf8df
commit 9c891307f3
4 changed files with 86 additions and 88 deletions

8
app.go
View File

@ -212,9 +212,7 @@ func (app *app) loop() {
} }
defer f.Close() defer f.Close()
dir := app.nav.currDir() _, err = f.WriteString(app.nav.currDir().path)
_, err = f.WriteString(dir.path)
if err != nil { if err != nil {
log.Printf("writing last dir file: %s", err) log.Printf("writing last dir file: %s", err)
} }
@ -326,8 +324,8 @@ func (app *app) loop() {
func (app *app) exportFiles() { func (app *app) exportFiles() {
var currFile string var currFile string
if f, err := app.nav.currFile(); err == nil { if curr, err := app.nav.currFile(); err == nil {
currFile = f.path currFile = curr.path
} }
currSelections := app.nav.currSelections() currSelections := app.nav.currSelections()

54
eval.go
View File

@ -385,9 +385,9 @@ func update(app *app) {
case gOpts.incsearch && app.ui.cmdPrefix == "/": case gOpts.incsearch && app.ui.cmdPrefix == "/":
app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight) app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight)
last := app.nav.currDir() dir := app.nav.currDir()
last.ind = app.nav.searchInd dir.ind = app.nav.searchInd
last.pos = app.nav.searchPos dir.pos = app.nav.searchPos
if err := app.nav.searchNext(); err != nil { if err := app.nav.searchNext(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search) app.ui.echoerrf("search: %s: %s", err, app.nav.search)
@ -399,9 +399,9 @@ func update(app *app) {
case gOpts.incsearch && app.ui.cmdPrefix == "?": case gOpts.incsearch && app.ui.cmdPrefix == "?":
app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight) app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight)
last := app.nav.currDir() dir := app.nav.currDir()
last.ind = app.nav.searchInd dir.ind = app.nav.searchInd
last.pos = app.nav.searchPos dir.pos = app.nav.searchPos
if err := app.nav.searchPrev(); err != nil { if err := app.nav.searchPrev(); err != nil {
app.ui.echoerrf("search: %s: %s", err, app.nav.search) app.ui.echoerrf("search: %s: %s", err, app.nav.search)
@ -711,10 +711,10 @@ func (e *callExpr) eval(app *app, args []string) {
if len(e.args) == 0 { if len(e.args) == 0 {
app.nav.toggle() app.nav.toggle()
} else { } else {
curr := app.nav.currDir() dir := app.nav.currDir()
for _, path := range e.args { for _, path := range e.args {
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path = filepath.Join(curr.path, path) path = filepath.Join(dir.path, path)
} }
if _, err := os.Lstat(path); !os.IsNotExist(err) { if _, err := os.Lstat(path); !os.IsNotExist(err) {
app.nav.toggleSelection(path) app.nav.toggleSelection(path)
@ -767,16 +767,16 @@ func (e *callExpr) eval(app *app, args []string) {
return return
} }
} else { } else {
fileOrSelections, err := app.nav.currFileOrSelections() list, err := app.nav.currFileOrSelections()
if err != nil { if err != nil {
app.ui.echoerrf("delete: %s", err) app.ui.echoerrf("delete: %s", err)
return return
} }
if selections := len(fileOrSelections); selections == 1 { if len(list) == 1 {
app.ui.cmdPrefix = "delete '" + fileOrSelections[0] + "' ? [y/N] " app.ui.cmdPrefix = "delete '" + list[0] + "' ? [y/N] "
} else { } else {
app.ui.cmdPrefix = "delete " + strconv.Itoa(selections) + " items? [y/N] " app.ui.cmdPrefix = "delete " + strconv.Itoa(len(list)) + " items? [y/N] "
} }
} }
app.ui.loadFile(app.nav) app.ui.loadFile(app.nav)
@ -851,16 +851,16 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
case "search": case "search":
app.ui.cmdPrefix = "/" app.ui.cmdPrefix = "/"
last := app.nav.currDir() dir := app.nav.currDir()
app.nav.searchInd = last.ind app.nav.searchInd = dir.ind
app.nav.searchPos = last.pos app.nav.searchPos = dir.pos
app.nav.searchBack = false app.nav.searchBack = false
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
case "search-back": case "search-back":
app.ui.cmdPrefix = "?" app.ui.cmdPrefix = "?"
last := app.nav.currDir() dir := app.nav.currDir()
app.nav.searchInd = last.ind app.nav.searchInd = dir.ind
app.nav.searchPos = last.pos app.nav.searchPos = dir.pos
app.nav.searchBack = true app.nav.searchBack = true
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
case "search-next": case "search-next":
@ -1034,9 +1034,9 @@ func (e *callExpr) eval(app *app, args []string) {
return return
} }
if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") { if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") {
last := app.nav.currDir() dir := app.nav.currDir()
last.ind = app.nav.searchInd dir.ind = app.nav.searchInd
last.pos = app.nav.searchPos dir.pos = app.nav.searchPos
app.ui.loadFile(app.nav) app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav) app.ui.loadFileInfo(app.nav)
@ -1104,9 +1104,9 @@ func (e *callExpr) eval(app *app, args []string) {
app.runShell(s, nil, "&") app.runShell(s, nil, "&")
case "/": case "/":
if gOpts.incsearch { if gOpts.incsearch {
last := app.nav.currDir() dir := app.nav.currDir()
last.ind = app.nav.searchInd dir.ind = app.nav.searchInd
last.pos = app.nav.searchPos dir.pos = app.nav.searchPos
} }
log.Printf("search: %s", s) log.Printf("search: %s", s)
app.ui.cmdPrefix = "" app.ui.cmdPrefix = ""
@ -1119,9 +1119,9 @@ func (e *callExpr) eval(app *app, args []string) {
} }
case "?": case "?":
if gOpts.incsearch { if gOpts.incsearch {
last := app.nav.currDir() dir := app.nav.currDir()
last.ind = app.nav.searchInd dir.ind = app.nav.searchInd
last.pos = app.nav.searchPos dir.pos = app.nav.searchPos
} }
log.Printf("search-back: %s", s) log.Printf("search-back: %s", s)
app.ui.cmdPrefix = "" app.ui.cmdPrefix = ""

98
nav.go
View File

@ -620,9 +620,9 @@ func (nav *nav) toggle() {
} }
func (nav *nav) invert() { func (nav *nav) invert() {
last := nav.currDir() dir := nav.currDir()
for _, f := range last.files { for _, f := range dir.files {
path := filepath.Join(last.path, f.Name()) path := filepath.Join(dir.path, f.Name())
nav.toggleSelection(path) nav.toggleSelection(path)
} }
} }
@ -939,25 +939,27 @@ func (nav *nav) sel(path string) error {
} }
func (nav *nav) globSel(pattern string, invert bool) error { func (nav *nav) globSel(pattern string, invert bool) error {
curDir := nav.currDir() dir := nav.currDir()
anyMatches := false anyMatched := false
for i := 0; i < len(curDir.files); i++ { for i := 0; i < len(dir.files); i++ {
match, err := filepath.Match(pattern, curDir.files[i].Name()) matched, err := filepath.Match(pattern, dir.files[i].Name())
if err != nil { if err != nil {
return fmt.Errorf("glob-select: %s", err) return fmt.Errorf("glob-select: %s", err)
} }
if match { if matched {
anyMatches = true anyMatched = true
fpath := filepath.Join(curDir.path, curDir.files[i].Name()) fpath := filepath.Join(dir.path, dir.files[i].Name())
if _, ok := nav.selections[fpath]; ok == invert { if _, ok := nav.selections[fpath]; ok == invert {
nav.toggleSelection(fpath) nav.toggleSelection(fpath)
} }
} }
} }
if !anyMatches {
if !anyMatched {
return fmt.Errorf("glob-select: pattern not found: %s", pattern) return fmt.Errorf("glob-select: pattern not found: %s", pattern)
} }
return nil return nil
} }
@ -985,9 +987,9 @@ func findMatch(name, pattern string) bool {
func (nav *nav) findSingle() int { func (nav *nav) findSingle() int {
count := 0 count := 0
index := 0 index := 0
last := nav.currDir() dir := nav.currDir()
for i := 0; i < len(last.files); i++ { for i := 0; i < len(dir.files); i++ {
if findMatch(last.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
count++ count++
if count > 1 { if count > 1 {
return count return count
@ -996,27 +998,27 @@ func (nav *nav) findSingle() int {
} }
} }
if count == 1 { if count == 1 {
if index > last.ind { if index > dir.ind {
nav.down(index - last.ind) nav.down(index - dir.ind)
} else { } else {
nav.up(last.ind - index) nav.up(dir.ind - index)
} }
} }
return count return count
} }
func (nav *nav) findNext() bool { func (nav *nav) findNext() bool {
last := nav.currDir() dir := nav.currDir()
for i := last.ind + 1; i < len(last.files); i++ { for i := dir.ind + 1; i < len(dir.files); i++ {
if findMatch(last.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.down(i - last.ind) nav.down(i - dir.ind)
return true return true
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := 0; i < last.ind; i++ { for i := 0; i < dir.ind; i++ {
if findMatch(last.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.up(last.ind - i) nav.up(dir.ind - i)
return true return true
} }
} }
@ -1025,17 +1027,17 @@ func (nav *nav) findNext() bool {
} }
func (nav *nav) findPrev() bool { func (nav *nav) findPrev() bool {
last := nav.currDir() dir := nav.currDir()
for i := last.ind - 1; i >= 0; i-- { for i := dir.ind - 1; i >= 0; i-- {
if findMatch(last.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.up(last.ind - i) nav.up(dir.ind - i)
return true return true
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := len(last.files) - 1; i > last.ind; i-- { for i := len(dir.files) - 1; i > dir.ind; i-- {
if findMatch(last.files[i].Name(), nav.find) { if findMatch(dir.files[i].Name(), nav.find) {
nav.down(i - last.ind) nav.down(i - dir.ind)
return true return true
} }
} }
@ -1065,25 +1067,25 @@ func searchMatch(name, pattern string) (matched bool, err error) {
} }
func (nav *nav) searchNext() error { func (nav *nav) searchNext() error {
last := nav.currDir() dir := nav.currDir()
for i := last.ind + 1; i < len(last.files); i++ { for i := dir.ind + 1; i < len(dir.files); i++ {
matched, err := searchMatch(last.files[i].Name(), nav.search) matched, err := searchMatch(dir.files[i].Name(), nav.search)
if err != nil { if err != nil {
return err return err
} }
if matched { if matched {
nav.down(i - last.ind) nav.down(i - dir.ind)
return nil return nil
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := 0; i < last.ind; i++ { for i := 0; i < dir.ind; i++ {
matched, err := searchMatch(last.files[i].Name(), nav.search) matched, err := searchMatch(dir.files[i].Name(), nav.search)
if err != nil { if err != nil {
return err return err
} }
if matched { if matched {
nav.up(last.ind - i) nav.up(dir.ind - i)
return nil return nil
} }
} }
@ -1092,25 +1094,25 @@ func (nav *nav) searchNext() error {
} }
func (nav *nav) searchPrev() error { func (nav *nav) searchPrev() error {
last := nav.currDir() dir := nav.currDir()
for i := last.ind - 1; i >= 0; i-- { for i := dir.ind - 1; i >= 0; i-- {
matched, err := searchMatch(last.files[i].Name(), nav.search) matched, err := searchMatch(dir.files[i].Name(), nav.search)
if err != nil { if err != nil {
return err return err
} }
if matched { if matched {
nav.up(last.ind - i) nav.up(dir.ind - i)
return nil return nil
} }
} }
if gOpts.wrapscan { if gOpts.wrapscan {
for i := len(last.files) - 1; i > last.ind; i-- { for i := len(dir.files) - 1; i > dir.ind; i-- {
matched, err := searchMatch(last.files[i].Name(), nav.search) matched, err := searchMatch(dir.files[i].Name(), nav.search)
if err != nil { if err != nil {
return err return err
} }
if matched { if matched {
nav.down(i - last.ind) nav.down(i - dir.ind)
return nil return nil
} }
} }
@ -1184,12 +1186,12 @@ func (nav *nav) currDir() *dir {
} }
func (nav *nav) currFile() (*file, error) { func (nav *nav) currFile() (*file, error) {
last := nav.dirs[len(nav.dirs)-1] dir := nav.dirs[len(nav.dirs)-1]
if len(last.files) == 0 { if len(dir.files) == 0 {
return nil, fmt.Errorf("empty directory") return nil, fmt.Errorf("empty directory")
} }
return last.files[last.ind], nil return dir.files[dir.ind], nil
} }
type indexedSelections struct { type indexedSelections struct {

14
ui.go
View File

@ -579,8 +579,6 @@ func (ui *ui) drawPromptLine(nav *nav) {
pwd = filepath.Join("~", strings.TrimPrefix(pwd, gUser.HomeDir)) pwd = filepath.Join("~", strings.TrimPrefix(pwd, gUser.HomeDir))
} }
pwd = filepath.Clean(pwd)
sep := string(filepath.Separator) sep := string(filepath.Separator)
if !strings.HasSuffix(pwd, sep) { if !strings.HasSuffix(pwd, sep) {
@ -622,12 +620,12 @@ func (ui *ui) drawPromptLine(nav *nav) {
func (ui *ui) drawStatLine(nav *nav) { func (ui *ui) drawStatLine(nav *nav) {
fg, bg := termbox.ColorDefault, termbox.ColorDefault fg, bg := termbox.ColorDefault, termbox.ColorDefault
currDir := nav.currDir() dir := nav.currDir()
ui.msgWin.print(0, 0, fg, bg, ui.msg) ui.msgWin.print(0, 0, fg, bg, ui.msg)
tot := len(currDir.files) tot := len(dir.files)
ind := min(currDir.ind+1, tot) ind := min(dir.ind+1, tot)
acc := string(ui.keyCount) + string(ui.keyAcc) acc := string(ui.keyCount) + string(ui.keyAcc)
var progress string var progress string
@ -719,13 +717,13 @@ func (ui *ui) draw(nav *nav) {
} }
if gOpts.preview { if gOpts.preview {
f, err := nav.currFile() curr, err := nav.currFile()
if err == nil { if err == nil {
preview := ui.wins[len(ui.wins)-1] preview := ui.wins[len(ui.wins)-1]
if f.IsDir() { if curr.IsDir() {
preview.printDir(ui.dirPrev, nav.selections, nav.saves, ui.colors, ui.icons) preview.printDir(ui.dirPrev, nav.selections, nav.saves, ui.colors, ui.icons)
} else if f.Mode().IsRegular() { } else if curr.Mode().IsRegular() {
preview.printReg(ui.regPrev) preview.printReg(ui.regPrev)
} }
} }