replace workdir with /

and do it right this time

I hope...
This commit is contained in:
Lilian Jónsdóttir 2024-07-15 17:10:38 -07:00
parent f193d0f5b3
commit 30b605a230
2 changed files with 32 additions and 10 deletions

View file

@ -6,16 +6,27 @@ import (
"strings" "strings"
) )
const sep = string(os.PathSeparator)
// UnExpand unexpands some directory shortcuts // UnExpand unexpands some directory shortcuts
// //
// $HOME -> ~ // $HOME -> ~
// $PWD -> . // $PWD -> .
// workdir -> . // workdir -> /
func UnExpand(dir string) (outdir string) { func UnExpand(dir, workdir string) (outdir string) {
outdir = filepath.Clean(dir) var (
home := os.Getenv("HOME") 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) outdir = strings.Replace(outdir, pwd, ".", 1)
} }
@ -30,6 +41,17 @@ func UnExpand(dir string) (outdir string) {
return 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 { func UnEscape(input string) string {
return strings.ReplaceAll(input, "%20", " ") return strings.ReplaceAll(input, "%20", " ")
} }

View file

@ -282,7 +282,7 @@ func (m model) header() string {
default: default:
mode = m.mode.String() mode = m.mode.String()
if m.workdir != "" { 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)) 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 return false
} */ } */
func newRow(file files.File) table.Row { func newRow(file files.File, workdir string) table.Row {
var t, b string var t, b string
t = humanize.Time(file.Date()) t = humanize.Time(file.Date())
if file.IsDir() { if file.IsDir() {
@ -332,7 +332,7 @@ func newRow(file files.File) table.Row {
} }
return table.Row{ return table.Row{
dirs.UnEscape(file.Name()), dirs.UnEscape(file.Name()),
dirs.UnExpand(filepath.Dir(file.Path())), dirs.UnExpand(filepath.Dir(file.Path()), workdir),
t, t,
b, b,
} }
@ -340,7 +340,7 @@ func newRow(file files.File) table.Row {
func (m *model) freshRows(preselected bool) (rows []table.Row) { func (m *model) freshRows(preselected bool) (rows []table.Row) {
for _, f := range m.files { for _, f := range m.files {
r := newRow(f) r := newRow(f, m.workdir)
if !m.readonly { if !m.readonly {
r = append(r, getCheck(preselected)) r = append(r, getCheck(preselected))
@ -477,7 +477,7 @@ func (m *model) sort() {
slices.SortStableFunc(m.files, m.sorting.Sorter()) slices.SortStableFunc(m.files, m.sorting.Sorter())
var rows []table.Row var rows []table.Row
for _, file := range m.files { for _, file := range m.files {
r := newRow(file) r := newRow(file, m.workdir)
if !m.readonly { if !m.readonly {
r = append(r, getCheck(m.selected[file.String()])) r = append(r, getCheck(m.selected[file.String()]))
} }