diff --git a/internal/dirs/dirs.go b/internal/dirs/dirs.go index 9ef7ff7..f20fa1a 100644 --- a/internal/dirs/dirs.go +++ b/internal/dirs/dirs.go @@ -6,16 +6,27 @@ import ( "strings" ) +const sep = string(os.PathSeparator) + // UnExpand unexpands some directory shortcuts // // $HOME -> ~ // $PWD -> . -// workdir -> . -func UnExpand(dir string) (outdir string) { - outdir = filepath.Clean(dir) - home := os.Getenv("HOME") +// workdir -> / +func UnExpand(dir, workdir string) (outdir string) { + var ( + home string = os.Getenv("HOME") + pwd, _ = os.Getwd() + ) - if pwd, err := os.Getwd(); err == nil && home != pwd { + if dir != "" { + outdir = cleanDir(dir, pwd) + } + + if workdir != "" { + workdir = cleanDir(workdir, pwd) + outdir = strings.Replace(outdir, workdir, "", 1) + } else if home != pwd && pwd != "" { outdir = strings.Replace(outdir, pwd, ".", 1) } @@ -30,6 +41,17 @@ func UnExpand(dir string) (outdir string) { return } +func cleanDir(dir, pwd string) (out string) { + if strings.HasPrefix(dir, ".") { + out = filepath.Clean(dir) + } else if !strings.HasPrefix(dir, sep) { + out = filepath.Join(pwd, dir) + } else { + out = dir + } + return +} + func UnEscape(input string) string { return strings.ReplaceAll(input, "%20", " ") } diff --git a/internal/tables/tables.go b/internal/tables/tables.go index 0c57661..e75525e 100644 --- a/internal/tables/tables.go +++ b/internal/tables/tables.go @@ -282,7 +282,7 @@ func (m model) header() string { default: mode = m.mode.String() if m.workdir != "" { - mode += fmt.Sprintf(" in %s ", dirs.UnExpand(m.workdir)) + mode += fmt.Sprintf(" in %s ", dirs.UnExpand(m.workdir, "")) } } mode += fmt.Sprintf(" %s %s", dot, strings.Join(select_keys, wide_dot)) @@ -322,7 +322,7 @@ func (m model) selectedFiles() (outfile files.Files) { return false } */ -func newRow(file files.File) table.Row { +func newRow(file files.File, workdir string) table.Row { var t, b string t = humanize.Time(file.Date()) if file.IsDir() { @@ -332,7 +332,7 @@ func newRow(file files.File) table.Row { } return table.Row{ dirs.UnEscape(file.Name()), - dirs.UnExpand(filepath.Dir(file.Path())), + dirs.UnExpand(filepath.Dir(file.Path()), workdir), t, b, } @@ -340,7 +340,7 @@ func newRow(file files.File) table.Row { func (m *model) freshRows(preselected bool) (rows []table.Row) { for _, f := range m.files { - r := newRow(f) + r := newRow(f, m.workdir) if !m.readonly { r = append(r, getCheck(preselected)) @@ -477,7 +477,7 @@ func (m *model) sort() { slices.SortStableFunc(m.files, m.sorting.Sorter()) var rows []table.Row for _, file := range m.files { - r := newRow(file) + r := newRow(file, m.workdir) if !m.readonly { r = append(r, getCheck(m.selected[file.String()])) }