add promptfmt option to configure prompt line

Related #50
This commit is contained in:
Gokcehan 2018-02-22 18:18:30 +03:00
parent 6c0f794a94
commit 61c8b588cd
6 changed files with 48 additions and 8 deletions

View File

@ -81,6 +81,7 @@ var (
"filesep", "filesep",
"ifs", "ifs",
"previewer", "previewer",
"promptfmt",
"shell", "shell",
"sortby", "sortby",
"timefmt", "timefmt",

1
doc.go
View File

@ -84,6 +84,7 @@ The following options can be used to customize the behavior of lf:
filesep string (default "\n") filesep string (default "\n")
ifs string (default '') (not exported if empty) ifs string (default '') (not exported if empty)
previewer string (default '') (not filtered if empty) previewer string (default '') (not filtered if empty)
promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m")
shell string (default 'sh') shell string (default 'sh')
sortby string (default 'natural') sortby string (default 'natural')
timefmt string (default 'Mon Jan _2 15:04:05 2006') timefmt string (default 'Mon Jan _2 15:04:05 2006')

View File

@ -88,6 +88,7 @@ The following options can be used to customize the behavior of lf:
filesep string (default "\n") filesep string (default "\n")
ifs string (default '') (not exported if empty) ifs string (default '') (not exported if empty)
previewer string (default '') (not filtered if empty) previewer string (default '') (not filtered if empty)
promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m")
shell string (default 'sh') shell string (default 'sh')
sortby string (default 'natural') sortby string (default 'natural')
timefmt string (default 'Mon Jan _2 15:04:05 2006') timefmt string (default 'Mon Jan _2 15:04:05 2006')

View File

@ -105,6 +105,8 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.ifs = e.val gOpts.ifs = e.val
case "previewer": case "previewer":
gOpts.previewer = strings.Replace(e.val, "~", gUser.HomeDir, -1) gOpts.previewer = strings.Replace(e.val, "~", gUser.HomeDir, -1)
case "promptfmt":
gOpts.promptfmt = e.val
case "shell": case "shell":
gOpts.shell = e.val gOpts.shell = e.val
case "sortby": case "sortby":

View File

@ -17,6 +17,7 @@ var gOpts struct {
filesep string filesep string
ifs string ifs string
previewer string previewer string
promptfmt string
shell string shell string
sortby string sortby string
timefmt string timefmt string
@ -40,6 +41,7 @@ func init() {
gOpts.scrolloff = 0 gOpts.scrolloff = 0
gOpts.tabstop = 8 gOpts.tabstop = 8
gOpts.filesep = "\n" gOpts.filesep = "\n"
gOpts.promptfmt = "\033[32;1m%u@%h\033[0m:\033[34;1m%w/\033[0m\033[1m%f\033[0m"
gOpts.shell = gDefaultShell gOpts.shell = gDefaultShell
gOpts.sortby = "natural" gOpts.sortby = "natural"
gOpts.timefmt = time.ANSIC gOpts.timefmt = time.ANSIC

49
ui.go
View File

@ -180,6 +180,34 @@ func applyAnsiCodes(s string, fg, bg termbox.Attribute) (termbox.Attribute, term
return fg, bg return fg, bg
} }
func printLength(s string) int {
ind := 0
off := 0
for i := 0; i < len(s); i++ {
r, w := utf8.DecodeRuneInString(s[i:])
if r == gEscapeCode && i+1 < len(s) && s[i+1] == '[' {
j := strings.IndexByte(s[i:min(len(s), i+32)], 'm')
if j == -1 {
continue
}
i += j
continue
}
i += w - 1
if r == '\t' {
ind += gOpts.tabstop - (ind-off)%gOpts.tabstop
} else {
ind += runewidth.RuneWidth(r)
}
}
return ind
}
func (win *win) print(x, y int, fg, bg termbox.Attribute, s string) (termbox.Attribute, termbox.Attribute) { func (win *win) print(x, y int, fg, bg termbox.Attribute, s string) (termbox.Attribute, termbox.Attribute) {
off := x off := x
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
@ -520,29 +548,34 @@ func (ui *ui) drawPromptLine(nav *nav) {
pwd := strings.Replace(dir.path, gUser.HomeDir, "~", -1) pwd := strings.Replace(dir.path, gUser.HomeDir, "~", -1)
pwd = filepath.Clean(pwd) pwd = filepath.Clean(pwd)
var base string var fname string
curr, err := nav.currFile() curr, err := nav.currFile()
if err == nil { if err == nil {
base = filepath.Base(curr.path) fname = filepath.Base(curr.path)
} }
if len(gUser.Username)+len(gHostname)+len(pwd)+len(base)+3 > ui.promptWin.w { var prompt string
prompt = strings.Replace(gOpts.promptfmt, "%u", gUser.Username, -1)
prompt = strings.Replace(prompt, "%h", gHostname, -1)
prompt = strings.Replace(prompt, "%f", fname, -1)
if printLength(strings.Replace(prompt, "%w", pwd, -1)) > ui.promptWin.w {
sep := string(filepath.Separator) sep := string(filepath.Separator)
names := strings.Split(pwd, sep) names := strings.Split(pwd, sep)
for i, _ := range names { for i, _ := range names {
r, _ := utf8.DecodeRuneInString(names[i]) r, _ := utf8.DecodeRuneInString(names[i])
names[i] = string(r) names[i] = string(r)
if len(gUser.Username)+len(gHostname)+len(strings.Join(names, sep))+len(base)+3 <= ui.promptWin.w { if printLength(strings.Replace(prompt, "%w", strings.Join(names, sep), -1)) <= ui.promptWin.w {
break break
} }
} }
pwd = strings.Join(names, sep) pwd = strings.Join(names, sep)
} }
ui.promptWin.printf(0, 0, termbox.AttrBold|termbox.ColorGreen, bg, "%s@%s", gUser.Username, gHostname) prompt = strings.Replace(prompt, "%w", pwd, -1)
ui.promptWin.printf(len(gUser.Username)+len(gHostname)+1, 0, fg, bg, ":")
ui.promptWin.printf(len(gUser.Username)+len(gHostname)+2, 0, termbox.AttrBold|termbox.ColorBlue, bg, "%s/", pwd) ui.promptWin.print(0, 0, fg, bg, prompt)
ui.promptWin.printf(len(gUser.Username)+len(gHostname)+len(pwd)+3, 0, termbox.AttrBold|fg, bg, "%s", base)
} }
func (ui *ui) drawStatLine(nav *nav) { func (ui *ui) drawStatLine(nav *nav) {