From f8daa60b310fb656f729b014f08652cef28cb7bf Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Mon, 20 Jul 2020 00:19:42 +0300 Subject: [PATCH] expand '%w' in 'promptfmt' with a trailing slash Related #421 --- doc.go | 2 +- docstring.go | 2 +- lf.1 | 3 ++- opts.go | 2 +- ui.go | 17 +++++++++++++---- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/doc.go b/doc.go index cd532ed..660b92a 100644 --- a/doc.go +++ b/doc.go @@ -717,7 +717,7 @@ other command: GIT_PS1_SHOWUNTRACKEDFILES=auto GIT_PS1_SHOWUPSTREAM=auto git=$(__git_ps1 " (%s)") || true - fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f$git\033[0m" + fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[1m%f$git\033[0m" lf -remote "send $id set promptfmt \"$fmt\"" }} diff --git a/docstring.go b/docstring.go index dd6ddb3..be83fa2 100644 --- a/docstring.go +++ b/docstring.go @@ -749,7 +749,7 @@ define any other command: GIT_PS1_SHOWUNTRACKEDFILES=auto GIT_PS1_SHOWUPSTREAM=auto git=$(__git_ps1 " (%s)") || true - fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f$git\033[0m" + fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[1m%f$git\033[0m" lf -remote "send $id set promptfmt \"$fmt\"" }} diff --git a/lf.1 b/lf.1 index 0d7a5f9..d5ebe6b 100644 --- a/lf.1 +++ b/lf.1 @@ -151,6 +151,7 @@ The following options can be used to customize the behavior of lf: shellopts string (default '') sortby string (default 'natural') timefmt string (default 'Mon Jan _2 15:04:05 2006') + truncatechar string (default '~') .EE .PP The following variables are exported for shell commands: @@ -655,7 +656,7 @@ There is a special command 'on-cd' that runs a shell command when it is defined GIT_PS1_SHOWUNTRACKEDFILES=auto GIT_PS1_SHOWUPSTREAM=auto git=$(__git_ps1 " (%s)") || true - fmt="\e033[32;1m%u@%h\e033[0m:\e033[34;1m%w/\e033[0m\e033[1m%f$git\e033[0m" + fmt="\e033[32;1m%u@%h\e033[0m:\e033[34;1m%w\e033[0m\e033[1m%f$git\e033[0m" lf -remote "send $id set promptfmt \e"$fmt\e"" }} .EE diff --git a/opts.go b/opts.go index d71833e..db8e6b8 100644 --- a/opts.go +++ b/opts.go @@ -91,7 +91,7 @@ func init() { gOpts.filesep = "\n" gOpts.ifs = "" gOpts.previewer = "" - gOpts.promptfmt = "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m" + gOpts.promptfmt = "\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[1m%f\033[0m" gOpts.shell = gDefaultShell gOpts.timefmt = time.ANSIC gOpts.truncatechar = "~" diff --git a/ui.go b/ui.go index b036394..4cd7f9f 100644 --- a/ui.go +++ b/ui.go @@ -574,12 +574,19 @@ func (ui *ui) drawPromptLine(nav *nav) { fg, bg := termbox.ColorDefault, termbox.ColorDefault pwd := nav.currDir().path - nohome := strings.TrimPrefix(pwd, gUser.HomeDir) - if len(nohome) < len(pwd) { - pwd = "~/" + nohome + + if strings.HasPrefix(pwd, gUser.HomeDir) { + pwd = filepath.Join("~", strings.TrimPrefix(pwd, gUser.HomeDir)) } + pwd = filepath.Clean(pwd) + sep := string(filepath.Separator) + + if !strings.HasSuffix(pwd, sep) { + pwd += sep + } + var fname string curr, err := nav.currFile() if err == nil { @@ -593,9 +600,11 @@ func (ui *ui) drawPromptLine(nav *nav) { prompt = strings.Replace(prompt, "%f", fname, -1) if printLength(strings.Replace(prompt, "%w", pwd, -1)) > ui.promptWin.w { - sep := string(filepath.Separator) names := strings.Split(pwd, sep) for i := range names { + if names[i] == "" { + continue + } r, _ := utf8.DecodeRuneInString(names[i]) names[i] = string(r) if printLength(strings.Replace(prompt, "%w", strings.Join(names, sep), -1)) <= ui.promptWin.w {