mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-10 04:54:03 -08:00
refactor: separate consoletitle logic
This commit is contained in:
parent
969b54420a
commit
e9c65948c1
|
@ -33,7 +33,6 @@ type formats struct {
|
|||
linechange string
|
||||
left string
|
||||
right string
|
||||
title string
|
||||
creset string
|
||||
clearOEL string
|
||||
saveCursorPosition string
|
||||
|
@ -55,7 +54,6 @@ func (r *AnsiRenderer) init(shell string) {
|
|||
r.formats.linechange = "%%{\x1b[%d%s%%}"
|
||||
r.formats.left = "%%{\x1b[%dC%%}"
|
||||
r.formats.right = "%%{\x1b[%dD%%}"
|
||||
r.formats.title = "%%{\033]0;%s\007%%}"
|
||||
r.formats.creset = "%{\x1b[0m%}"
|
||||
r.formats.clearOEL = "%{\x1b[K%}"
|
||||
r.formats.saveCursorPosition = "%{\x1b7%}"
|
||||
|
@ -64,7 +62,6 @@ func (r *AnsiRenderer) init(shell string) {
|
|||
r.formats.linechange = "\\[\x1b[%d%s\\]"
|
||||
r.formats.left = "\\[\x1b[%dC\\]"
|
||||
r.formats.right = "\\[\x1b[%dD\\]"
|
||||
r.formats.title = "\\[\033]0;%s\007\\]"
|
||||
r.formats.creset = "\\[\x1b[0m\\]"
|
||||
r.formats.clearOEL = "\\[\x1b[K\\]"
|
||||
r.formats.saveCursorPosition = "\\[\x1b7\\]"
|
||||
|
@ -73,7 +70,6 @@ func (r *AnsiRenderer) init(shell string) {
|
|||
r.formats.linechange = "\x1b[%d%s"
|
||||
r.formats.left = "\x1b[%dC"
|
||||
r.formats.right = "\x1b[%dD"
|
||||
r.formats.title = "\033]0;%s\007"
|
||||
r.formats.creset = "\x1b[0m"
|
||||
r.formats.clearOEL = "\x1b[K"
|
||||
r.formats.saveCursorPosition = "\x1b7"
|
||||
|
@ -99,10 +95,6 @@ func (r *AnsiRenderer) changeLine(numberOfLines int) {
|
|||
r.buffer.WriteString(fmt.Sprintf(r.formats.linechange, numberOfLines, position))
|
||||
}
|
||||
|
||||
func (r *AnsiRenderer) setConsoleTitle(title string) {
|
||||
r.buffer.WriteString(fmt.Sprintf(r.formats.title, title))
|
||||
}
|
||||
|
||||
func (r *AnsiRenderer) creset() {
|
||||
r.buffer.WriteString(r.formats.creset)
|
||||
}
|
||||
|
|
42
src/console_title.go
Normal file
42
src/console_title.go
Normal file
|
@ -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)
|
||||
}
|
37
src/console_title_test.go
Normal file
37
src/console_title_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -149,14 +149,11 @@ func (e *engine) render() {
|
|||
}
|
||||
}
|
||||
if e.settings.ConsoleTitle {
|
||||
switch e.settings.ConsoleTitleStyle {
|
||||
case FullPath:
|
||||
e.renderer.setConsoleTitle(e.env.getcwd())
|
||||
case FolderName:
|
||||
fallthrough
|
||||
default:
|
||||
e.renderer.setConsoleTitle(base(e.env.getcwd(), e.env))
|
||||
title := &consoleTitle{
|
||||
env: e.env,
|
||||
settings: e.settings,
|
||||
}
|
||||
e.renderer.print(title.getConsoleTitle())
|
||||
}
|
||||
e.renderer.creset()
|
||||
if e.settings.FinalSpace {
|
||||
|
|
|
@ -22,9 +22,6 @@ type BlockType string
|
|||
// BlockAlignment aligment of a Block
|
||||
type BlockAlignment string
|
||||
|
||||
// ConsoleTitleStyle defines how to show the title in the console window
|
||||
type ConsoleTitleStyle string
|
||||
|
||||
const (
|
||||
// Prompt writes one or more Segments
|
||||
Prompt BlockType = "prompt"
|
||||
|
@ -36,10 +33,6 @@ const (
|
|||
Left BlockAlignment = "left"
|
||||
// Right aligns right
|
||||
Right BlockAlignment = "right"
|
||||
// FolderName show the current folder name
|
||||
FolderName ConsoleTitleStyle = "folder"
|
||||
// FullPath show the current path
|
||||
FullPath ConsoleTitleStyle = "path"
|
||||
)
|
||||
|
||||
// Block defines a part of the prompt with optional segments
|
||||
|
|
Loading…
Reference in a new issue