Skip to content

Commit

Permalink
feat: use tilde in console title
Browse files Browse the repository at this point in the history
  • Loading branch information
tradiff authored and JanDeDobbeleer committed Jan 14, 2021
1 parent 369ff4d commit 0761c04
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/console_title.go
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"strings"
"text/template"
)

Expand Down Expand Up @@ -39,22 +40,22 @@ 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)
}

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)
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions src/console_title_test.go
Expand Up @@ -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}}",
Expand Down

0 comments on commit 0761c04

Please sign in to comment.