replace workdir with . in tables

This commit is contained in:
Lilian Jónsdóttir 2024-06-19 22:21:35 -07:00
parent f7605f6eab
commit 966643fd2e
3 changed files with 15 additions and 8 deletions

View file

@ -9,7 +9,9 @@ import (
// UnExpand unexpands some directory shortcuts
//
// $HOME -> ~
func UnExpand(dir string) (outdir string) {
// $PWD -> .
// workdir -> .
func UnExpand(dir, workdir string) (outdir string) {
var (
home = os.Getenv("HOME")
pwd string
@ -18,9 +20,13 @@ func UnExpand(dir string) (outdir string) {
outdir = filepath.Clean(dir)
if workdir != "" {
outdir = strings.Replace(outdir, workdir, ".", 1)
}
pwd, err = os.Getwd()
if err == nil && home != pwd {
outdir = strings.Replace(outdir, pwd, "$PWD", 1)
outdir = strings.Replace(outdir, pwd, ".", 1)
}
outdir = strings.Replace(outdir, home, "~", 1)

View file

@ -89,7 +89,7 @@ func newInfosModel(is trash.Infos, width, height int, readonly, preselected bool
}
r := table.Row{
i.Name(),
dirs.UnExpand(filepath.Dir(i.OGPath())),
dirs.UnExpand(filepath.Dir(i.OGPath()), ""),
t,
b,
}
@ -120,7 +120,7 @@ func newInfosModel(is trash.Infos, width, height int, readonly, preselected bool
return m
}
func newFilesModel(fs files.Files, width, height int, readonly, preselected bool) model {
func newFilesModel(fs files.Files, width, height int, readonly, preselected bool, workdir string) model {
var (
fwidth int = int(math.Round(float64(width-woffset) * 0.4))
owidth int = int(math.Round(float64(width-woffset) * 0.2))
@ -150,7 +150,7 @@ func newFilesModel(fs files.Files, width, height int, readonly, preselected bool
}
r := table.Row{
f.Name(),
dirs.UnExpand(f.Path()),
dirs.UnExpand(f.Path(), workdir),
t,
b,
}
@ -473,8 +473,8 @@ func InfoTable(is trash.Infos, width, height int, readonly, preselected bool, mo
}
}
func FilesTable(fs files.Files, width, height int, readonly, preselected bool) ([]int, error) {
if endmodel, err := tea.NewProgram(newFilesModel(fs, width, height, readonly, preselected)).Run(); err != nil {
func FilesTable(fs files.Files, width, height int, readonly, preselected bool, workdir string) ([]int, error) {
if endmodel, err := tea.NewProgram(newFilesModel(fs, width, height, readonly, preselected, workdir)).Run(); err != nil {
return []int{}, err
} else {
m, ok := endmodel.(model)

View file

@ -94,7 +94,8 @@ var (
return nil
}
indices, err := tables.FilesTable(fls, termwidth, termheight, false, !f.Blank())
log.Debugf("what is workdir? it's %s", workdir)
indices, err := tables.FilesTable(fls, termwidth, termheight, false, !f.Blank(), workdir)
if err != nil {
return err
}