From 0761c04286365b8a79064684bda31945d893d5a7 Mon Sep 17 00:00:00 2001 From: TravisTX Date: Wed, 13 Jan 2021 19:30:14 -0700 Subject: [PATCH] feat: use tilde in console title --- src/console_title.go | 15 +++++++++++---- src/console_title_test.go | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/console_title.go b/src/console_title.go index 740a6206..39f0e20a 100644 --- a/src/console_title.go +++ b/src/console_title.go @@ -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 +} diff --git a/src/console_title_test.go b/src/console_title_test.go index e6ff6a52..ef002a55 100644 --- a/src/console_title_test.go +++ b/src/console_title_test.go @@ -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}}",