feat: use tilde in console title

This commit is contained in:
TravisTX 2021-01-13 19:30:14 -07:00 committed by Jan De Dobbeleer
parent 369ff4ddaf
commit 0761c04286
2 changed files with 13 additions and 6 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"strings"
"text/template"
)
@ -39,13 +40,13 @@ func (t *consoleTitle) getConsoleTitle() string {
var title string
switch t.settings.ConsoleTitleStyle {
case FullPath:
title = t.env.getcwd()
title = t.getPwd()
case Template:
title = t.getTemplateText()
case FolderName:
fallthrough
default:
title = base(t.env.getcwd(), t.env)
title = base(t.getPwd(), t.env)
}
return fmt.Sprintf(t.formats.title, title)
}
@ -53,8 +54,8 @@ func (t *consoleTitle) getConsoleTitle() string {
func (t *consoleTitle) getTemplateText() string {
context := &TitleTemplateContext{
Root: t.env.isRunningAsRoot(),
Path: t.env.getcwd(),
Folder: base(t.env.getcwd(), t.env),
Path: t.getPwd(),
Folder: base(t.getPwd(), t.env),
Shell: t.env.getShellName(),
}
tmpl, err := template.New("title").Parse(t.settings.ConsoleTitleTemplate)
@ -69,3 +70,9 @@ func (t *consoleTitle) getTemplateText() string {
}
return buffer.String()
}
func (t *consoleTitle) getPwd() string {
pwd := t.env.getcwd()
pwd = strings.Replace(pwd, t.env.homeDir(), "~", 1)
return pwd
}

View file

@ -16,8 +16,8 @@ func TestGetConsoleTitle(t *testing.T) {
ShellName string
Expected string
}{
{Style: FolderName, Cwd: "/usr/home", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;home\a"},
{Style: FullPath, Cwd: "/usr/home/jan", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;/usr/home/jan\a"},
{Style: FolderName, Cwd: "/usr/home", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;~\a"},
{Style: FullPath, Cwd: "/usr/home/jan", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;~/jan\a"},
{
Style: Template,
Template: "{{.Path}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",