Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate consoletitle logic
- Loading branch information
1 parent
969b544
commit e9c6594
Showing
5 changed files
with
83 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type consoleTitle struct { | ||
env environmentInfo | ||
settings *Settings | ||
} | ||
|
||
// ConsoleTitleStyle defines how to show the title in the console window | ||
type ConsoleTitleStyle string | ||
|
||
const ( | ||
// FolderName show the current folder name | ||
FolderName ConsoleTitleStyle = "folder" | ||
// FullPath show the current path | ||
FullPath ConsoleTitleStyle = "path" | ||
) | ||
|
||
func (t *consoleTitle) getConsoleTitle() string { | ||
switch t.settings.ConsoleTitleStyle { | ||
case FullPath: | ||
return t.formatConsoleTitle(t.env.getcwd()) | ||
case FolderName: | ||
fallthrough | ||
default: | ||
return t.formatConsoleTitle(base(t.env.getcwd(), t.env)) | ||
} | ||
} | ||
|
||
func (t *consoleTitle) formatConsoleTitle(title string) string { | ||
var format string | ||
switch t.env.getShellName() { | ||
case zsh: | ||
format = "%%{\033]0;%s\007%%}" | ||
case bash: | ||
format = "\\[\033]0;%s\007\\]" | ||
default: | ||
format = "\033]0;%s\007" | ||
} | ||
return fmt.Sprintf(format, title) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetConsoleTitle(t *testing.T) { | ||
cases := []struct { | ||
Style ConsoleTitleStyle | ||
Cwd string | ||
PathSeperator string | ||
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"}, | ||
} | ||
|
||
for _, tc := range cases { | ||
settings := &Settings{ | ||
ConsoleTitleStyle: tc.Style, | ||
} | ||
env := new(MockedEnvironment) | ||
env.On("getcwd", nil).Return(tc.Cwd) | ||
env.On("homeDir", nil).Return("/usr/home") | ||
env.On("getPathSeperator", nil).Return(tc.PathSeperator) | ||
env.On("getShellName", nil).Return(tc.ShellName) | ||
ct := &consoleTitle{ | ||
env: env, | ||
settings: settings, | ||
} | ||
got := ct.getConsoleTitle() | ||
assert.Equal(t, tc.Expected, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters