Skip to content

Commit

Permalink
refactor: separate consoletitle logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Dec 27, 2020
1 parent 969b544 commit e9c6594
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
8 changes: 0 additions & 8 deletions src/ansi_renderer.go
Expand Up @@ -33,7 +33,6 @@ type formats struct {
linechange string
left string
right string
title string
creset string
clearOEL string
saveCursorPosition string
Expand All @@ -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%}"
Expand All @@ -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\\]"
Expand All @@ -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"
Expand All @@ -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)
}
Expand Down
42 changes: 42 additions & 0 deletions src/console_title.go
@@ -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 changes: 37 additions & 0 deletions src/console_title_test.go
@@ -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)
}
}
11 changes: 4 additions & 7 deletions src/engine.go
Expand Up @@ -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 {
Expand Down
7 changes: 0 additions & 7 deletions src/settings.go
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit e9c6594

Please sign in to comment.