refactor: linting checks for all platforms

This commit is contained in:
Jan De Dobbeleer 2020-11-12 09:43:32 +01:00 committed by Jan De Dobbeleer
parent 3c812f916e
commit 8fb190e074
49 changed files with 537 additions and 443 deletions

View file

@ -9,6 +9,7 @@ root = true
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
; Go Code - match go fmt
[*.go]

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
* text=auto eol=lf

View file

@ -27,7 +27,6 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Golang CI
if: matrix.os == 'ubuntu-latest'
uses: golangci/golangci-lint-action@v2
with:
version: v1.31

0
.gitignore vendored Executable file → Normal file
View file

50
.golangci.yml Normal file
View file

@ -0,0 +1,50 @@
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dupl
- errcheck
- exhaustive
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- goprintffuncname
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- lll
linters-settings:
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-tags:
- experimental
disabled-checks:
- ifElseChain
lll:
line-length: 180

View file

@ -27,7 +27,7 @@ func (e *engine) getPowerlineColor(foreground bool) string {
return e.previousActiveSegment.Background
}
func (e *engine) writePowerLineSeparator(background string, foreground string, end bool) {
func (e *engine) writePowerLineSeparator(background, foreground string, end bool) {
symbol := e.activeSegment.PowerlineSymbol
if end {
symbol = e.previousActiveSegment.PowerlineSymbol
@ -75,7 +75,7 @@ func (e *engine) renderSegmentText(text string) {
e.renderPlainSegment(text)
case Diamond:
e.renderDiamondSegment(text)
default:
case Powerline:
e.renderPowerLineSegment(text)
}
e.previousActiveSegment = e.activeSegment
@ -135,7 +135,7 @@ func (e *engine) render() {
cursorMove := e.renderer.setCursorForRightWrite(blockText, block.HorizontalOffset)
e.renderer.print(cursorMove)
e.renderer.print(blockText)
default:
case Left:
e.renderer.print(e.renderBlockSegments(block))
}
}

View file

@ -16,6 +16,11 @@ import (
"github.com/shirou/gopsutil/process"
)
const (
unknown = "unknown"
windowsPlatform = "windows"
)
type environmentInfo interface {
getenv(key string) string
getcwd() string
@ -31,12 +36,12 @@ type environmentInfo interface {
getPlatform() string
hasCommand(command string) bool
runCommand(command string, args ...string) (string, error)
runShellCommand(shell string, command string) string
runShellCommand(shell, command string) string
lastErrorCode() int
getArgs() *args
getBatteryInfo() (*battery.Battery, error)
getShellName() string
getWindowTitle(imageName string, windowTitleRegex string) (string, error)
getWindowTitle(imageName, windowTitleRegex string) (string, error)
}
type environment struct {
@ -124,8 +129,8 @@ func (env *environment) getRuntimeGOOS() string {
}
func (env *environment) getPlatform() string {
if runtime.GOOS == "windows" {
return "windows"
if runtime.GOOS == windowsPlatform {
return windowsPlatform
}
p, _, _, _ := host.PlatformInformation()
@ -146,7 +151,7 @@ func (env *environment) runCommand(command string, args ...string) (string, erro
return strings.TrimSpace(string(out)), nil
}
func (env *environment) runShellCommand(shell string, command string) string {
func (env *environment) runShellCommand(shell, command string) string {
out, err := exec.Command(shell, "-c", command).Output()
if err != nil {
log.Println(err)
@ -177,14 +182,14 @@ func (env *environment) getShellName() string {
p, _ := process.NewProcess(int32(pid))
name, err := p.Name()
if err != nil {
return "unknown"
return unknown
}
if name == "cmd.exe" {
p, _ = p.Parent()
name, err = p.Name()
}
if err != nil {
return "unknown"
return unknown
}
shell := strings.Replace(name, ".exe", "", 1)
return strings.Trim(shell, " ")

2
environment_unix.go Executable file → Normal file
View file

@ -15,6 +15,6 @@ func (env *environment) homeDir() string {
return os.Getenv("HOME")
}
func (env *environment) getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
func (env *environment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
return "", errors.New("not implemented")
}

6
environment_windows.go Executable file → Normal file
View file

@ -25,7 +25,9 @@ func (env *environment) isRunningAsRoot() bool {
if err != nil {
return false
}
defer windows.FreeSid(sid)
defer func() {
_ = windows.FreeSid(sid)
}()
// This appears to cast a null pointer so I'm not sure why this
// works, but this guy says it does and it Works for Me™:
@ -48,6 +50,6 @@ func (env *environment) homeDir() string {
return home
}
func (env *environment) getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
func (env *environment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
return getWindowTitle(imageName, windowTitleRegex)
}

View file

@ -35,7 +35,7 @@ func getImagePid(imageName string) ([]int, error) {
}
// getWindowTitle returns the title of a window linked to a process name
func getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
func getWindowTitle(imageName, windowTitleRegex string) (string, error) {
processPid, err := getImagePid(imageName)
if err != nil {
return "", nil
@ -73,7 +73,9 @@ func processes() ([]WindowsProcess, error) {
if err != nil {
return nil, syscall.GetLastError()
}
defer windows.CloseHandle(handle)
defer func() {
_ = windows.CloseHandle(handle)
}()
// get process infor by looping through the snapshot
var entry windows.ProcessEntry32
@ -109,8 +111,8 @@ var (
)
// EnumWindows call EnumWindows from user32 and returns all active windows
func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
func EnumWindows(enumFunc, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, enumFunc, lparam, 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
@ -122,10 +124,10 @@ func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
}
// GetWindowText returns the title and text of a window from a window handle
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (length int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
length = int32(r0)
if length == 0 {
if e1 != 0 {
err = error(e1)
} else {
@ -135,7 +137,7 @@ func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32,
return
}
// GetWindowTitle searchs for a window attached to the pid
// GetWindowTitle searches for a window attached to the pid
func GetWindowTitle(pid int, windowTitleRegex string) (syscall.Handle, string, error) {
var hwnd syscall.Handle
var title string
@ -166,8 +168,8 @@ func GetWindowTitle(pid int, windowTitleRegex string) (syscall.Handle, string, e
return 1 // continue enumeration
})
// Enumerates all top-level windows on the screen
EnumWindows(cb, 0)
if hwnd == 0 {
err = EnumWindows(cb, 0)
if err != nil || hwnd == 0 {
return 0, "", fmt.Errorf("No window with title '%b' found", pid)
}
return hwnd, title, nil

0
main.go Executable file → Normal file
View file

View file

@ -5,22 +5,22 @@ import (
"regexp"
)
//Property defines one property of a segment for context
// Property defines one property of a segment for context
type Property string
//general Properties used across Segments
// general Properties used across Segments
const (
//Style indicates with style to use
// Style indicates with style to use
Style Property = "style"
//Prefix adds a text prefix to the segment
// Prefix adds a text prefix to the segment
Prefix Property = "prefix"
//Postfix adds a text postfix to the segment
// Postfix adds a text postfix to the segment
Postfix Property = "postfix"
//ColorBackground color the background or foreground when a specific color is set
// ColorBackground color the background or foreground when a specific color is set
ColorBackground Property = "color_background"
//IgnoreFolders folders to ignore and not run the segment logic
// IgnoreFolders folders to ignore and not run the segment logic
IgnoreFolders Property = "ignore_folders"
//DisplayVersion show the version number or not
// DisplayVersion show the version number or not
DisplayVersion Property = "display_version"
)

View file

@ -6,8 +6,11 @@ import (
"github.com/stretchr/testify/assert"
)
const (
expected = "expected"
)
func TestGetString(t *testing.T) {
expected := "expected"
values := map[Property]interface{}{TextProperty: expected}
properties := properties{
values: values,
@ -17,7 +20,6 @@ func TestGetString(t *testing.T) {
}
func TestGetStringNoEntry(t *testing.T) {
expected := "expected"
values := map[Property]interface{}{}
properties := properties{
values: values,
@ -27,7 +29,6 @@ func TestGetStringNoEntry(t *testing.T) {
}
func TestGetStringNoTextEntry(t *testing.T) {
expected := "expected"
values := map[Property]interface{}{TextProperty: true}
properties := properties{
values: values,
@ -37,7 +38,7 @@ func TestGetStringNoTextEntry(t *testing.T) {
}
func TestGetColor(t *testing.T) {
expected := "#123456"
expected := expectedColor
values := map[Property]interface{}{UserColor: expected}
properties := properties{
values: values,
@ -47,7 +48,7 @@ func TestGetColor(t *testing.T) {
}
func TestDefaultColorWithInvalidColorCode(t *testing.T) {
expected := "#123456"
expected := expectedColor
values := map[Property]interface{}{UserColor: "invalid"}
properties := properties{
values: values,
@ -57,7 +58,7 @@ func TestDefaultColorWithInvalidColorCode(t *testing.T) {
}
func TestDefaultColorWithUnavailableProperty(t *testing.T) {
expected := "#123456"
expected := expectedColor
values := map[Property]interface{}{}
properties := properties{
values: values,

30
renderer.go Executable file → Normal file
View file

@ -59,7 +59,7 @@ func getColorFromName(colorName string, isBackground bool) (string, error) {
return "", errors.New("color name does not exist")
}
//Renderer writes colorized strings
// Renderer writes colorized strings
type Renderer struct {
Buffer *bytes.Buffer
formats *formats
@ -67,8 +67,10 @@ type Renderer struct {
}
const (
//Transparent implies a transparent color
Transparent string = "transparent"
// Transparent implies a transparent color
Transparent = "transparent"
zsh = "zsh"
bash = "bash"
)
func (r *Renderer) init(shell string) {
@ -77,7 +79,7 @@ func (r *Renderer) init(shell string) {
rANSI: "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))",
}
switch shell {
case "zsh":
case zsh:
r.formats.single = "%%{\x1b[%sm%%}%s%%{\x1b[0m%%}"
r.formats.full = "%%{\x1b[%sm\x1b[%sm%%}%s%%{\x1b[0m%%}"
r.formats.transparent = "%%{\x1b[%s;49m\x1b[7m%%}%s%%{\x1b[m\x1b[0m%%}"
@ -87,7 +89,7 @@ func (r *Renderer) init(shell string) {
r.formats.title = "%%{\033]0;%s\007%%}"
r.formats.creset = "%{\x1b[0m%}"
r.formats.clearOEL = "%{\x1b[K%}"
case "bash":
case bash:
r.formats.single = "\\[\x1b[%sm\\]%s\\[\x1b[0m\\]"
r.formats.full = "\\[\x1b[%sm\x1b[%sm\\]%s\\[\x1b[0m\\]"
r.formats.transparent = "\\[\x1b[%s;49m\x1b[7m\\]%s\\[\x1b[m\x1b[0m\\]"
@ -122,7 +124,7 @@ func (r *Renderer) getAnsiFromColorString(colorString string, isBackground bool)
return style.Code()
}
func (r *Renderer) writeColoredText(background string, foreground string, text string) {
func (r *Renderer) writeColoredText(background, foreground, text string) {
var coloredText string
if foreground == Transparent && background != "" {
ansiColor := r.getAnsiFromColorString(background, false)
@ -138,12 +140,12 @@ func (r *Renderer) writeColoredText(background string, foreground string, text s
r.Buffer.WriteString(coloredText)
}
func (r *Renderer) writeAndRemoveText(background string, foreground string, text string, textToRemove string, parentText string) string {
func (r *Renderer) writeAndRemoveText(background, foreground, text, textToRemove, parentText string) string {
r.writeColoredText(background, foreground, text)
return strings.Replace(parentText, textToRemove, "", 1)
}
func (r *Renderer) write(background string, foreground string, text string) {
func (r *Renderer) write(background, foreground, text string) {
// first we match for any potentially valid color enclosed in <>
rex := regexp.MustCompile(`<([#A-Za-z0-9]+)>(.*?)<\/>`)
match := rex.FindAllStringSubmatch(text, -1)
@ -166,12 +168,12 @@ func (r *Renderer) lenWithoutANSI(str string) int {
re := regexp.MustCompile(r.formats.rANSI)
stripped := re.ReplaceAllString(str, "")
switch r.shell {
case "zsh":
stripped = strings.Replace(stripped, "%{", "", -1)
stripped = strings.Replace(stripped, "%}", "", -1)
case "bash":
stripped = strings.Replace(stripped, "\\[", "", -1)
stripped = strings.Replace(stripped, "\\]", "", -1)
case zsh:
stripped = strings.ReplaceAll(stripped, "%{", "")
stripped = strings.ReplaceAll(stripped, "%}", "")
case bash:
stripped = strings.ReplaceAll(stripped, "\\[", "")
stripped = strings.ReplaceAll(stripped, "\\]", "")
}
var i norm.Iter
i.InitString(norm.NFD, stripped)

11
renderer_test.go Executable file → Normal file
View file

@ -4,8 +4,12 @@ import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/gookit/color"
"github.com/stretchr/testify/assert"
)
const (
inputText = "This is white, <#ff5733>this is orange</>, white again"
)
func TestWriteAndRemoveText(t *testing.T) {
@ -13,7 +17,6 @@ func TestWriteAndRemoveText(t *testing.T) {
Buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
inputText := "This is white, <#ff5733>this is orange</>, white again"
text := renderer.writeAndRemoveText("#193549", "#fff", "This is white, ", "This is white, ", inputText)
assert.Equal(t, "<#ff5733>this is orange</>, white again", text)
assert.NotContains(t, renderer.string(), "<#ff5733>")
@ -24,7 +27,6 @@ func TestWriteAndRemoveTextColored(t *testing.T) {
Buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
inputText := "This is white, <#ff5733>this is orange</>, white again"
text := renderer.writeAndRemoveText("#193549", "#ff5733", "this is orange", "<#ff5733>this is orange</>", inputText)
assert.Equal(t, "This is white, , white again", text)
assert.NotContains(t, renderer.string(), "<#ff5733>")
@ -35,8 +37,7 @@ func TestWriteColorOverride(t *testing.T) {
Buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
text := "This is white, <#ff5733>this is orange</>, white again"
renderer.write("#193549", "#ff5733", text)
renderer.write("#193549", "#ff5733", inputText)
assert.NotContains(t, renderer.string(), "<#ff5733>")
}

View file

@ -2,7 +2,7 @@ package main
import "errors"
//Segment represent a single segment and it's configuration
// Segment represent a single segment and it's configuration
type Segment struct {
Type SegmentType `json:"type"`
Style SegmentStyle `json:"style"`
@ -19,65 +19,65 @@ type Segment struct {
active bool
}
//SegmentWriter is the interface used to define what and if to write to the prompt
// SegmentWriter is the interface used to define what and if to write to the prompt
type SegmentWriter interface {
enabled() bool
string() string
init(props *properties, env environmentInfo)
}
//SegmentStyle the syle of segment, for more information, see the constants
// SegmentStyle the syle of segment, for more information, see the constants
type SegmentStyle string
//SegmentType the type of segment, for more information, see the constants
// SegmentType the type of segment, for more information, see the constants
type SegmentType string
const (
//Session represents the user info segment
// Session represents the user info segment
Session SegmentType = "session"
//Path represents the current path segment
// Path represents the current path segment
Path SegmentType = "path"
//Git represents the git status and information
// Git represents the git status and information
Git SegmentType = "git"
//Exit writes the last exit code
// Exit writes the last exit code
Exit SegmentType = "exit"
//Python writes the virtual env name
// Python writes the virtual env name
Python SegmentType = "python"
//Root writes root symbol
// Root writes root symbol
Root SegmentType = "root"
//Time writes the current timestamp
// Time writes the current timestamp
Time SegmentType = "time"
//Text writes a text
// Text writes a text
Text SegmentType = "text"
//Cmd writes the output of a shell command
// Cmd writes the output of a shell command
Cmd SegmentType = "command"
//Battery writes the battery percentage
// Battery writes the battery percentage
Battery SegmentType = "battery"
//Spotify writes the Spotify status for Mac
// Spotify writes the Spotify status for Mac
Spotify SegmentType = "spotify"
//ShellInfo writes which shell we're currently in
// ShellInfo writes which shell we're currently in
ShellInfo SegmentType = "shell"
//Node writes which node version is currently active
// Node writes which node version is currently active
Node SegmentType = "node"
//Os write os specific icon
// Os write os specific icon
Os SegmentType = "os"
//EnvVar writes the content of an environment variable
// EnvVar writes the content of an environment variable
EnvVar SegmentType = "envvar"
//Az writes the Azure subscription info we're currently in
// Az writes the Azure subscription info we're currently in
Az SegmentType = "az"
//Kubectl writes the Kubernetes context we're currently in
// Kubectl writes the Kubernetes context we're currently in
Kubectl SegmentType = "kubectl"
//Dotnet writes which dotnet version is currently active
// Dotnet writes which dotnet version is currently active
Dotnet SegmentType = "dotnet"
//Terraform writes the terraform workspace we're currently in
// Terraform writes the terraform workspace we're currently in
Terraform SegmentType = "terraform"
//Golang writes which go version is currently active
// Golang writes which go version is currently active
Golang SegmentType = "go"
//Powerline writes it Powerline style
// Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
//Plain writes it without ornaments
// Plain writes it without ornaments
Plain SegmentStyle = "plain"
//Diamond writes the prompt shaped with a leading and trailing symbol
// Diamond writes the prompt shaped with a leading and trailing symbol
Diamond SegmentStyle = "diamond"
)
@ -144,5 +144,5 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
segment.props = props
return nil
}
return errors.New("Unable to map writer")
return errors.New("unable to map writer")
}

View file

@ -13,11 +13,11 @@ type az struct {
}
const (
//SubscriptionInfoSeparator is put between the name and ID
// SubscriptionInfoSeparator is put between the name and ID
SubscriptionInfoSeparator Property = "info_separator"
//DisplaySubscriptionID hides or show the subscription GUID
// DisplaySubscriptionID hides or show the subscription GUID
DisplaySubscriptionID Property = "display_id"
//DisplaySubscriptionName hides or shows the subscription display name
// DisplaySubscriptionName hides or shows the subscription display name
DisplaySubscriptionName Property = "display_name"
)

View file

@ -14,21 +14,21 @@ type batt struct {
}
const (
//BatteryIcon to display in front of the battery
// BatteryIcon to display in front of the battery
BatteryIcon Property = "battery_icon"
//DisplayError to display when an error occurs or not
// DisplayError to display when an error occurs or not
DisplayError Property = "display_error"
//ChargingIcon to display when charging
// ChargingIcon to display when charging
ChargingIcon Property = "charging_icon"
//DischargingIcon o display when discharging
// DischargingIcon o display when discharging
DischargingIcon Property = "discharging_icon"
//ChargedIcon to display when fully charged
// ChargedIcon to display when fully charged
ChargedIcon Property = "charged_icon"
//ChargedColor to display when fully charged
// ChargedColor to display when fully charged
ChargedColor Property = "charged_color"
//ChargingColor to display when charging
// ChargingColor to display when charging
ChargingColor Property = "charging_color"
//DischargingColor to display when discharging
// DischargingColor to display when discharging
DischargingColor Property = "discharging_color"
)
@ -58,7 +58,7 @@ func (b *batt) enabled() bool {
case battery.Full:
colorPorperty = ChargedColor
icon = b.props.getString(ChargedIcon, "")
default:
case battery.Empty, battery.Unknown:
b.percentageText = percentageText
return true
}

View file

@ -8,6 +8,10 @@ import (
"github.com/stretchr/testify/assert"
)
const (
expectedColor = "#768954"
)
func setupBatteryTests(state battery.State, batteryLevel float64, props *properties) *batt {
env := &MockedEnvironment{}
bt := &battery.Battery{
@ -55,7 +59,7 @@ func TestBatteryDischarging(t *testing.T) {
}
func TestBatteryBackgroundColor(t *testing.T) {
expected := "#768954"
expected := expectedColor
props := &properties{
background: "#111111",
values: map[Property]interface{}{
@ -70,7 +74,7 @@ func TestBatteryBackgroundColor(t *testing.T) {
}
func TestBatteryBackgroundColorInvalid(t *testing.T) {
expected := "#768954"
expected := expectedColor
props := &properties{
background: expected,
values: map[Property]interface{}{
@ -85,7 +89,7 @@ func TestBatteryBackgroundColorInvalid(t *testing.T) {
}
func TestBatteryForegroundColor(t *testing.T) {
expected := "#768954"
expected := expectedColor
props := &properties{
foreground: "#111111",
values: map[Property]interface{}{
@ -100,7 +104,7 @@ func TestBatteryForegroundColor(t *testing.T) {
}
func TestBatteryForegroundColorInvalid(t *testing.T) {
expected := "#768954"
expected := expectedColor
props := &properties{
foreground: expected,
values: map[Property]interface{}{

View file

@ -9,9 +9,9 @@ type command struct {
}
const (
//ExecutableShell to execute command in
// ExecutableShell to execute command in
ExecutableShell Property = "shell"
//Command to execute
// Command to execute
Command Property = "command"
)

View file

@ -12,8 +12,8 @@ type dotnet struct {
}
const (
//UnsupportedDotnetVersionIcon is displayed when the dotnet version in
//the current folder isn't supported by the installed dotnet SDK set.
// UnsupportedDotnetVersionIcon is displayed when the dotnet version in
// the current folder isn't supported by the installed dotnet SDK set.
UnsupportedDotnetVersionIcon Property = "unsupported_version_icon"
)

View file

@ -7,7 +7,7 @@ type envvar struct {
}
const (
//VarName name of the variable
// VarName name of the variable
VarName Property = "var_name"
)

View file

@ -8,11 +8,11 @@ type exit struct {
}
const (
//DisplayExitCode shows or hides the error code
// DisplayExitCode shows or hides the error code
DisplayExitCode Property = "display_exit_code"
//AlwaysEnabled decides whether or not to always display the exitcode info
// AlwaysEnabled decides whether or not to always display the exitcode info
AlwaysEnabled Property = "always_enabled"
//ErrorColor specify a different foreground color for the error text when using always_show = true
// ErrorColor specify a different foreground color for the error text when using always_show = true
ErrorColor Property = "error_color"
)

62
segment_git.go Executable file → Normal file
View file

@ -28,7 +28,7 @@ type gitStatus struct {
changed bool
}
func (s *gitStatus) string(prefix string, color string) string {
func (s *gitStatus) string(prefix, color string) string {
var status string
stringIfValue := func(value int, prefix string) string {
if value > 0 {
@ -54,63 +54,63 @@ type git struct {
}
const (
//BranchIcon the icon to use as branch indicator
// BranchIcon the icon to use as branch indicator
BranchIcon Property = "branch_icon"
//BranchIdenticalIcon the icon to display when the remote and local branch are identical
// BranchIdenticalIcon the icon to display when the remote and local branch are identical
BranchIdenticalIcon Property = "branch_identical_icon"
//BranchAheadIcon the icon to display when the local branch is ahead of the remote
// BranchAheadIcon the icon to display when the local branch is ahead of the remote
BranchAheadIcon Property = "branch_ahead_icon"
//BranchBehindIcon the icon to display when the local branch is behind the remote
// BranchBehindIcon the icon to display when the local branch is behind the remote
BranchBehindIcon Property = "branch_behind_icon"
//BranchGoneIcon the icon to use when ther's no remote
// BranchGoneIcon the icon to use when ther's no remote
BranchGoneIcon Property = "branch_gone_icon"
//LocalWorkingIcon the icon to use as the local working area changes indicator
// LocalWorkingIcon the icon to use as the local working area changes indicator
LocalWorkingIcon Property = "local_working_icon"
//LocalStagingIcon the icon to use as the local staging area changes indicator
// LocalStagingIcon the icon to use as the local staging area changes indicator
LocalStagingIcon Property = "local_staged_icon"
//DisplayStatus shows the status of the repository
// DisplayStatus shows the status of the repository
DisplayStatus Property = "display_status"
//DisplayStatusDetail shows the detailed status of the repository
// DisplayStatusDetail shows the detailed status of the repository
DisplayStatusDetail Property = "display_status_detail"
//RebaseIcon shows before the rebase context
// RebaseIcon shows before the rebase context
RebaseIcon Property = "rebase_icon"
//CherryPickIcon shows before the cherry-pick context
// CherryPickIcon shows before the cherry-pick context
CherryPickIcon Property = "cherry_pick_icon"
//CommitIcon shows before the detached context
// CommitIcon shows before the detached context
CommitIcon Property = "commit_icon"
//TagIcon shows before the tag context
// TagIcon shows before the tag context
TagIcon Property = "tag_icon"
//DisplayStashCount show stash count or not
// DisplayStashCount show stash count or not
DisplayStashCount Property = "display_stash_count"
//StashCountIcon shows before the stash context
// StashCountIcon shows before the stash context
StashCountIcon Property = "stash_count_icon"
//StatusSeparatorIcon shows between staging and working area
// StatusSeparatorIcon shows between staging and working area
StatusSeparatorIcon Property = "status_separator_icon"
//MergeIcon shows before the merge context
// MergeIcon shows before the merge context
MergeIcon Property = "merge_icon"
//DisplayUpstreamIcon show or hide the upstream icon
// DisplayUpstreamIcon show or hide the upstream icon
DisplayUpstreamIcon Property = "display_upstream_icon"
//GithubIcon shows√ when upstream is github
// GithubIcon shows√ when upstream is github
GithubIcon Property = "github_icon"
//BitbucketIcon shows when upstream is bitbucket
// BitbucketIcon shows when upstream is bitbucket
BitbucketIcon Property = "bitbucket_icon"
//GitlabIcon shows when upstream is gitlab
// GitlabIcon shows when upstream is gitlab
GitlabIcon Property = "gitlab_icon"
//GitIcon shows when the upstream can't be identified
// GitIcon shows when the upstream can't be identified
GitIcon Property = "git_icon"
//WorkingColor if set, the color to use on the working area
// WorkingColor if set, the color to use on the working area
WorkingColor Property = "working_color"
//StagingColor if set, the color to use on the staging area
// StagingColor if set, the color to use on the staging area
StagingColor Property = "staging_color"
//StatusColorsEnabled enables status colors
// StatusColorsEnabled enables status colors
StatusColorsEnabled Property = "status_colors_enabled"
//LocalChangesColor if set, the color to use when there are local changes
// LocalChangesColor if set, the color to use when there are local changes
LocalChangesColor Property = "local_changes_color"
//AheadAndBehindColor if set, the color to use when the branch is ahead and behind the remote
// AheadAndBehindColor if set, the color to use when the branch is ahead and behind the remote
AheadAndBehindColor Property = "ahead_and_behind_color"
//BehindColor if set, the color to use when the branch is ahead and behind the remote
// BehindColor if set, the color to use when the branch is ahead and behind the remote
BehindColor Property = "behind_color"
//AheadColor if set, the color to use when the branch is ahead and behind the remote
// AheadColor if set, the color to use when the branch is ahead and behind the remote
AheadColor Property = "ahead_color"
)
@ -170,7 +170,7 @@ func (g *git) init(props *properties, env environmentInfo) {
g.env = env
}
func (g *git) getStatusDetailString(status *gitStatus, color Property, icon Property, defaultIcon string) string {
func (g *git) getStatusDetailString(status *gitStatus, color, icon Property, defaultIcon string) string {
prefix := g.props.getString(icon, defaultIcon)
foregroundColor := g.props.getColor(color, g.props.foreground)
if !g.props.getBool(DisplayStatusDetail, true) {

43
segment_git_test.go Executable file → Normal file
View file

@ -6,6 +6,10 @@ import (
"github.com/stretchr/testify/assert"
)
const (
changesColor = "#BD8BDE"
)
func TestEnabledGitNotFound(t *testing.T) {
env := new(MockedEnvironment)
env.On("hasCommand", "git").Return(false)
@ -70,12 +74,12 @@ func setupHEADContextEnv(context *detachedContext) *git {
env.On("getFileContent", "/.git/MERGE_HEAD").Return(context.mergeHEAD)
env.On("hasFiles", "/.git/CHERRY_PICK_HEAD").Return(context.cherryPick)
env.On("hasFiles", "/.git/MERGE_HEAD").Return(context.merge)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-parse", "--short", "HEAD"}).Return(context.currentCommit, nil)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "describe", "--tags", "--exact-match"}).Return(context.tagName, nil)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.origin}).Return(context.origin, nil)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.onto}).Return(context.onto, nil)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.cherryPickSHA}).Return(context.cherryPickSHA, nil)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.mergeHEAD}).Return(context.mergeHEAD, nil)
env.mockGitCommand(context.currentCommit, "rev-parse", "--short", "HEAD")
env.mockGitCommand(context.tagName, "describe", "--tags", "--exact-match")
env.mockGitCommand(context.origin, "name-rev", "--name-only", "--exclude=tags/*", context.origin)
env.mockGitCommand(context.onto, "name-rev", "--name-only", "--exclude=tags/*", context.onto)
env.mockGitCommand(context.cherryPickSHA, "name-rev", "--name-only", "--exclude=tags/*", context.cherryPickSHA)
env.mockGitCommand(context.mergeHEAD, "name-rev", "--name-only", "--exclude=tags/*", context.mergeHEAD)
g := &git{
env: env,
repo: &gitRepo{
@ -85,6 +89,11 @@ func setupHEADContextEnv(context *detachedContext) *git {
return g
}
func (m *MockedEnvironment) mockGitCommand(returnValue string, args ...string) {
args = append([]string{"-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
m.On("runCommand", "git", args).Return(returnValue, nil)
}
func TestGetGitDetachedCommitHash(t *testing.T) {
want := "\uf417lalasha1"
context := &detachedContext{
@ -411,7 +420,7 @@ func TestGetUpstreamSymbolGit(t *testing.T) {
}
func TestGetStatusColorLocalChangesStaging(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{
changed: true,
@ -429,7 +438,7 @@ func TestGetStatusColorLocalChangesStaging(t *testing.T) {
}
func TestGetStatusColorLocalChangesWorking(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{},
working: &gitStatus{
@ -448,7 +457,7 @@ func TestGetStatusColorLocalChangesWorking(t *testing.T) {
}
func TestGetStatusColorAheadAndBehind(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{},
working: &gitStatus{},
@ -467,7 +476,7 @@ func TestGetStatusColorAheadAndBehind(t *testing.T) {
}
func TestGetStatusColorAhead(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{},
working: &gitStatus{},
@ -486,7 +495,7 @@ func TestGetStatusColorAhead(t *testing.T) {
}
func TestGetStatusColorBehind(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{},
working: &gitStatus{},
@ -505,7 +514,7 @@ func TestGetStatusColorBehind(t *testing.T) {
}
func TestGetStatusColorDefault(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{},
working: &gitStatus{},
@ -516,7 +525,7 @@ func TestGetStatusColorDefault(t *testing.T) {
repo: repo,
props: &properties{
values: map[Property]interface{}{
BehindColor: "#BD8BDE",
BehindColor: changesColor,
},
},
}
@ -524,7 +533,7 @@ func TestGetStatusColorDefault(t *testing.T) {
}
func TestSetStatusColorBackground(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{
changed: true,
@ -534,7 +543,7 @@ func TestSetStatusColorBackground(t *testing.T) {
repo: repo,
props: &properties{
values: map[Property]interface{}{
LocalChangesColor: "#BD8BDE",
LocalChangesColor: changesColor,
ColorBackground: false,
},
foreground: "#ffffff",
@ -546,7 +555,7 @@ func TestSetStatusColorBackground(t *testing.T) {
}
func TestSetStatusColorForeground(t *testing.T) {
expected := "#BD8BDE"
expected := changesColor
repo := &gitRepo{
staging: &gitStatus{
changed: true,
@ -556,7 +565,7 @@ func TestSetStatusColorForeground(t *testing.T) {
repo: repo,
props: &properties{
values: map[Property]interface{}{
LocalChangesColor: "#BD8BDE",
LocalChangesColor: changesColor,
ColorBackground: true,
},
foreground: "#ffffff",

0
segment_golang_test.go Executable file → Normal file
View file

0
segment_kubectl_test.go Executable file → Normal file
View file

14
segment_node_test.go Executable file → Normal file
View file

@ -6,6 +6,10 @@ import (
"github.com/stretchr/testify/assert"
)
const (
node114 = "1.14"
)
type nodeArgs struct {
enabled bool
nodeVersion string
@ -49,7 +53,7 @@ func TestNodeWriterDisabledNoJSorTSFiles(t *testing.T) {
}
func TestNodeEnabledJSFiles(t *testing.T) {
expected := "1.14"
expected := node114
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
@ -62,7 +66,7 @@ func TestNodeEnabledJSFiles(t *testing.T) {
}
func TestNodeEnabledTsFiles(t *testing.T) {
expected := "1.14"
expected := node114
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
@ -75,7 +79,7 @@ func TestNodeEnabledTsFiles(t *testing.T) {
}
func TestNodeEnabledJsAndTsFiles(t *testing.T) {
expected := "1.14"
expected := node114
args := &nodeArgs{
enabled: true,
nodeVersion: expected,
@ -92,7 +96,7 @@ func TestNodeEnabledNoVersion(t *testing.T) {
expected := ""
args := &nodeArgs{
enabled: true,
nodeVersion: "1.14",
nodeVersion: node114,
hasJS: true,
displayVersion: false,
}
@ -102,7 +106,7 @@ func TestNodeEnabledNoVersion(t *testing.T) {
}
func TestNodeEnabledNodeVersion(t *testing.T) {
expected := "1.14"
expected := node114
args := &nodeArgs{
enabled: true,
nodeVersion: expected,

View file

@ -10,53 +10,53 @@ type osInfo struct {
}
const (
//MacOS the string/icon to use for MacOS
// MacOS the string/icon to use for MacOS
MacOS Property = "macos"
//Linux the string/icon to use for linux
// Linux the string/icon to use for linux
Linux Property = "linux"
//Windows the string/icon to use for windows
// Windows the string/icon to use for windows
Windows Property = "windows"
//WSL the string/icon to use for WSL
// WSL the string/icon to use for WSL
WSL Property = "wsl"
//WSLSeparator shows between WSL and Linux properties when WSL is detected
// WSLSeparator shows between WSL and Linux properties when WSL is detected
WSLSeparator Property = "wsl_separator"
//Alpine the string/icon to use for Alpine
// Alpine the string/icon to use for Alpine
Alpine Property = "alpine"
//Aosc the string/icon to use for Aosc
// Aosc the string/icon to use for Aosc
Aosc Property = "aosc"
//Arch the string/icon to use for Arch
// Arch the string/icon to use for Arch
Arch Property = "arch"
//Centos the string/icon to use for Centos
// Centos the string/icon to use for Centos
Centos Property = "centos"
//Coreos the string/icon to use for Coreos
// Coreos the string/icon to use for Coreos
Coreos Property = "coreos"
//Debian the string/icon to use for Debian
// Debian the string/icon to use for Debian
Debian Property = "debian"
//Devuan the string/icon to use for Devuan
// Devuan the string/icon to use for Devuan
Devuan Property = "devuan"
//Raspbian the string/icon to use for Raspbian
// Raspbian the string/icon to use for Raspbian
Raspbian Property = "raspbian"
//Elementary the string/icon to use for Elementary
// Elementary the string/icon to use for Elementary
Elementary Property = "elementary"
//Fedora the string/icon to use for Fedora
// Fedora the string/icon to use for Fedora
Fedora Property = "fedora"
//Gentoo the string/icon to use for Gentoo
// Gentoo the string/icon to use for Gentoo
Gentoo Property = "gentoo"
//Mageia the string/icon to use for Mageia
// Mageia the string/icon to use for Mageia
Mageia Property = "mageia"
//Manjaro the string/icon to use for Manjaro
// Manjaro the string/icon to use for Manjaro
Manjaro Property = "manjaro"
//Mint the string/icon to use for Mint
// Mint the string/icon to use for Mint
Mint Property = "mint"
//Nixos the string/icon to use for Nixos
// Nixos the string/icon to use for Nixos
Nixos Property = "nixos"
//Opensuse the string/icon to use for Opensuse
// Opensuse the string/icon to use for Opensuse
Opensuse Property = "opensuse"
//Sabayon the string/icon to use for Sabayon
// Sabayon the string/icon to use for Sabayon
Sabayon Property = "sabayon"
//Slackware the string/icon to use for Slackware
// Slackware the string/icon to use for Slackware
Slackware Property = "slackware"
//Ubuntu the string/icon to use for Ubuntu
// Ubuntu the string/icon to use for Ubuntu
Ubuntu Property = "ubuntu"
)
@ -67,7 +67,7 @@ func (n *osInfo) enabled() bool {
func (n *osInfo) string() string {
goos := n.env.getRuntimeGOOS()
switch goos {
case "windows":
case windowsPlatform:
return n.props.getString(Windows, "\uE62A")
case "darwin":
return n.props.getString(MacOS, "\uF179")

24
segment_path.go Executable file → Normal file
View file

@ -13,23 +13,23 @@ type path struct {
}
const (
//FolderSeparatorIcon the path which is split will be separated by this icon
// FolderSeparatorIcon the path which is split will be separated by this icon
FolderSeparatorIcon Property = "folder_separator_icon"
//HomeIcon indicates the $HOME location
// HomeIcon indicates the $HOME location
HomeIcon Property = "home_icon"
//FolderIcon identifies one folder
// FolderIcon identifies one folder
FolderIcon Property = "folder_icon"
//WindowsRegistryIcon indicates the registry location on Windows
// WindowsRegistryIcon indicates the registry location on Windows
WindowsRegistryIcon Property = "windows_registry_icon"
//Agnoster displays a short path with separator icon, this the default style
// Agnoster displays a short path with separator icon, this the default style
Agnoster string = "agnoster"
//AgnosterFull displays all the folder names with the folder_separator_icon
// AgnosterFull displays all the folder names with the folder_separator_icon
AgnosterFull string = "agnoster_full"
//Short displays a shorter path
// Short displays a shorter path
Short string = "short"
//Full displays the full path
// Full displays the full path
Full string = "full"
//Folder displays the current folder
// Folder displays the current folder
Folder string = "folder"
)
@ -96,7 +96,7 @@ func (pt *path) getAgnosterFullPath() string {
if string(pwd[0]) == pathSeparator {
pwd = pwd[1:]
}
return strings.Replace(pwd, pathSeparator, folderSeparator, -1)
return strings.ReplaceAll(pwd, pathSeparator, folderSeparator)
}
func (pt *path) inHomeDir(pwd string) bool {
@ -104,7 +104,7 @@ func (pt *path) inHomeDir(pwd string) bool {
}
func (pt *path) rootLocation(pwd string) string {
//See https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/correcting-powershell-paths
// See https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/correcting-powershell-paths
if strings.HasPrefix(pwd, "Microsoft.PowerShell.Core\\FileSystem::") {
pwd = strings.Replace(pwd, "Microsoft.PowerShell.Core\\FileSystem::", "", 1)
}
@ -162,7 +162,7 @@ func base(path string, env environmentInfo) string {
}
// If empty now, it had only slashes.
if path == "" {
return string(env.getPathSeperator())
return env.getPathSeperator()
}
return path
}

52
segment_path_test.go Executable file → Normal file
View file

@ -78,7 +78,7 @@ func (env *MockedEnvironment) runCommand(command string, args ...string) (string
return arguments.String(0), arguments.Error(1)
}
func (env *MockedEnvironment) runShellCommand(shell string, command string) string {
func (env *MockedEnvironment) runShellCommand(shell, command string) string {
args := env.Called(shell, command)
return args.String(0)
}
@ -108,13 +108,21 @@ func (env *MockedEnvironment) getShellName() string {
return args.String(0)
}
func (env *MockedEnvironment) getWindowTitle(imageName string, windowTitleRegex string) (string, error) {
func (env *MockedEnvironment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
args := env.Called(imageName)
return args.String(0), args.Error(1)
}
const (
homeGates = "/home/gates"
homeBill = "/home/bill"
homeJan = "/usr/home/jan"
homeBillWindows = "C:\\Users\\Bill"
levelDir = "/level"
)
func TestIsInHomeDirTrue(t *testing.T) {
home := "/home/bill"
home := homeBill
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
path := &path{
@ -126,10 +134,10 @@ func TestIsInHomeDirTrue(t *testing.T) {
func TestIsInHomeDirLevelTrue(t *testing.T) {
level := rand.Intn(100)
home := "/home/bill"
home := homeBill
pwd := home
for i := 0; i < level; i++ {
pwd += "/level"
pwd += levelDir
}
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
@ -250,7 +258,7 @@ func TestRootLocationEmptyDir(t *testing.T) {
}
func TestIsInHomeDirFalse(t *testing.T) {
home := "/home/bill"
home := homeBill
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
path := &path{
@ -261,7 +269,7 @@ func TestIsInHomeDirFalse(t *testing.T) {
}
func TestPathDepthInHome(t *testing.T) {
home := "/home/bill"
home := homeBill
pwd := home
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
@ -288,10 +296,10 @@ func TestPathDepthInHomeTrailing(t *testing.T) {
func TestPathDepthInHomeMultipleLevelsDeep(t *testing.T) {
level := rand.Intn(100)
home := "/home/bill"
home := homeBill
pwd := home
for i := 0; i < level; i++ {
pwd += "/level"
pwd += levelDir
}
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
@ -305,10 +313,10 @@ func TestPathDepthInHomeMultipleLevelsDeep(t *testing.T) {
func TestPathDepthOutsideHomeMultipleLevelsDeep(t *testing.T) {
level := rand.Intn(100)
home := "/home/gates"
home := homeGates
pwd := "/usr"
for i := 0; i < level; i++ {
pwd += "/level"
pwd += levelDir
}
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
@ -321,7 +329,7 @@ func TestPathDepthOutsideHomeMultipleLevelsDeep(t *testing.T) {
}
func TestPathDepthOutsideHomeZeroLevelsDeep(t *testing.T) {
home := "/home/gates"
home := homeGates
pwd := "/usr/"
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
@ -334,7 +342,7 @@ func TestPathDepthOutsideHomeZeroLevelsDeep(t *testing.T) {
}
func TestPathDepthOutsideHomeOneLevelDeep(t *testing.T) {
home := "/home/gates"
home := homeGates
pwd := "/usr/location"
env := new(MockedEnvironment)
env.On("homeDir", nil).Return(home)
@ -363,7 +371,7 @@ func TestGetAgnosterFullPath(t *testing.T) {
assert.Equal(t, "usr > location > whatever", got)
}
func testWritePathInfo(home string, pwd string, pathSeparator string) string {
func testWritePathInfo(home, pwd, pathSeparator string) string {
props := &properties{
values: map[Property]interface{}{
FolderSeparatorIcon: " > ",
@ -383,14 +391,14 @@ func testWritePathInfo(home string, pwd string, pathSeparator string) string {
}
func TestWritePathInfoWindowsOutsideHome(t *testing.T) {
home := "C:\\Users\\Bill"
home := homeBillWindows
want := "C: > f > f > location"
got := testWritePathInfo(home, "C:\\Program Files\\Go\\location", "\\")
assert.Equal(t, want, got)
}
func TestWritePathInfoWindowsInsideHome(t *testing.T) {
home := "C:\\Users\\Bill"
home := homeBillWindows
location := home + "\\Documents\\Bill\\location"
want := "~ > f > f > location"
got := testWritePathInfo(home, location, "\\")
@ -398,28 +406,28 @@ func TestWritePathInfoWindowsInsideHome(t *testing.T) {
}
func TestWritePathInfoWindowsOutsideHomeZeroLevels(t *testing.T) {
home := "C:\\Users\\Bill"
home := homeBillWindows
want := "C: > location"
got := testWritePathInfo(home, "C:\\location", "\\")
assert.Equal(t, want, got)
}
func TestWritePathInfoWindowsOutsideHomeOneLevels(t *testing.T) {
home := "C:\\Users\\Bill"
home := homeBillWindows
want := "C: > f > location"
got := testWritePathInfo(home, "C:\\Program Files\\location", "\\")
assert.Equal(t, want, got)
}
func TestWritePathInfoUnixOutsideHome(t *testing.T) {
home := "/usr/home/jan"
home := homeJan
want := "mnt > f > f > location"
got := testWritePathInfo(home, "/mnt/go/test/location", "/")
assert.Equal(t, want, got)
}
func TestWritePathInfoUnixInsideHome(t *testing.T) {
home := "/usr/home/jan"
home := homeJan
location := home + "/docs/jan/location"
want := "~ > f > f > location"
got := testWritePathInfo(home, location, "/")
@ -427,14 +435,14 @@ func TestWritePathInfoUnixInsideHome(t *testing.T) {
}
func TestWritePathInfoUnixOutsideHomeZeroLevels(t *testing.T) {
home := "/usr/home/jan"
home := homeJan
want := "mnt > location"
got := testWritePathInfo(home, "/mnt/location", "/")
assert.Equal(t, want, got)
}
func TestWritePathInfoUnixOutsideHomeOneLevels(t *testing.T) {
home := "/usr/home/jan"
home := homeJan
want := "mnt > f > location"
got := testWritePathInfo(home, "/mnt/folder/location", "/")
assert.Equal(t, want, got)

View file

@ -14,7 +14,7 @@ type python struct {
}
const (
//DisplayVirtualEnv shows or hides the virtual env
// DisplayVirtualEnv shows or hides the virtual env
DisplayVirtualEnv Property = "display_virtual_env"
)
@ -46,7 +46,7 @@ func (p *python) enabled() bool {
p.pythonVersion = strings.Trim(values["version"], " ")
break
}
//last element, Python isn't installed on this machine
// last element, Python isn't installed on this machine
if index == len(pythonVersions)-1 {
return false
}

24
segment_python_test.go Executable file → Normal file
View file

@ -50,11 +50,15 @@ func bootStrapPythonTest(args *pythonArgs) *python {
return python
}
const (
python345 = "Python 3.4.5"
)
func TestPythonWriterDisabledNoPythonFiles(t *testing.T) {
args := newPythonArgs()
args.hasPyFiles = false
args.hasNotebookFiles = false
args.python3Version = "Python 3.4.5"
args.python3Version = python345
python := bootStrapPythonTest(args)
assert.False(t, python.enabled(), "there are no Python files in the current folder")
}
@ -63,7 +67,7 @@ func TestPythonWriterDisabledHasPythonFiles(t *testing.T) {
args := newPythonArgs()
args.hasPyFiles = true
args.hasNotebookFiles = false
args.python3Version = "Python 3.4.5"
args.python3Version = python345
python := bootStrapPythonTest(args)
assert.True(t, python.enabled(), "there should be a Python file in the current folder")
}
@ -72,7 +76,7 @@ func TestPythonWriterDisabledHasJupyterNotebookFiles(t *testing.T) {
args := newPythonArgs()
args.hasPyFiles = false
args.hasNotebookFiles = true
args.python3Version = "Python 3.4.5"
args.python3Version = python345
python := bootStrapPythonTest(args)
assert.True(t, python.enabled(), "there should be a Jupyter Notebook file in the current folder")
}
@ -81,7 +85,7 @@ func TestPythonWriterDisabledHasPyAndJupyterNotebookFiles(t *testing.T) {
args := newPythonArgs()
args.hasPyFiles = true
args.hasNotebookFiles = true
args.python3Version = "Python 3.4.5"
args.python3Version = python345
python := bootStrapPythonTest(args)
assert.True(t, python.enabled(), "there should be a Jupyter Notebook file in the current folder")
}
@ -102,7 +106,7 @@ func TestPythonWriterDisabledNoPythonInstalled(t *testing.T) {
func TestPythonWriterEnabledNoVirtualEnv(t *testing.T) {
args := newPythonArgs()
args.python3Version = "Python 3.4.5"
args.python3Version = python345
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())
assert.Equal(t, "3.4.5", python.string())
@ -110,7 +114,7 @@ func TestPythonWriterEnabledNoVirtualEnv(t *testing.T) {
func TestPythonWriterEnabledVirtualEnvOverrule(t *testing.T) {
args := newPythonArgs()
args.python3Version = "Python 3.4.5"
args.python3Version = python345
args.condaEnvName = "myenv"
props := &properties{
values: map[Property]interface{}{
@ -125,7 +129,7 @@ func TestPythonWriterEnabledVirtualEnvOverrule(t *testing.T) {
func TestPythonWriterEnabledVirtualEnv(t *testing.T) {
args := newPythonArgs()
args.python3Version = "Python 3.4.5"
args.python3Version = python345
args.condaEnvName = "myenv"
expected := fmt.Sprintf("%s %s", args.condaEnvName, "3.4.5")
props := &properties{
@ -142,7 +146,7 @@ func TestPythonWriterEnabledVirtualEnv(t *testing.T) {
func TestPythonWriterEnabledWithVirtualEnv(t *testing.T) {
args := newPythonArgs()
args.virtualEnvName = "venv"
args.python3Version = "Python 3.4.5"
args.python3Version = python345
expected := fmt.Sprintf("%s %s", args.virtualEnvName, "3.4.5")
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())
@ -162,7 +166,7 @@ func TestPythonWriterEnabledWithCondaEnvPath(t *testing.T) {
func TestPythonWriterEnabledWithCondaDefaultEnv(t *testing.T) {
args := newPythonArgs()
args.condaDefaultName = "conda2"
args.python3Version = "Python 3.4.5"
args.python3Version = python345
expected := fmt.Sprintf("%s %s", args.condaDefaultName, "3.4.5")
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())
@ -183,7 +187,7 @@ func TestPythonWriterEnabledWithTwoValidEnvs(t *testing.T) {
args := newPythonArgs()
args.condaEnvName = "conda"
args.condaDefaultName = "conda2"
args.python3Version = "Python 3.4.5"
args.python3Version = python345
expected := fmt.Sprintf("%s %s", args.condaEnvName, "3.4.5")
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())

View file

@ -6,7 +6,7 @@ type root struct {
}
const (
//RootIcon indicates the root user
// RootIcon indicates the root user
RootIcon Property = "root_icon"
)

16
segment_session.go Executable file → Normal file
View file

@ -11,17 +11,17 @@ type session struct {
}
const (
//UserInfoSeparator is put between the user and computer name
// UserInfoSeparator is put between the user and computer name
UserInfoSeparator Property = "user_info_separator"
//UserColor if set, is used to color the user name
// UserColor if set, is used to color the user name
UserColor Property = "user_color"
//HostColor if set, is used to color the computer name
// HostColor if set, is used to color the computer name
HostColor Property = "host_color"
//DisplayHost hides or show the computer name
// DisplayHost hides or show the computer name
DisplayHost Property = "display_host"
//DisplayUser hides or shows the user name
// DisplayUser hides or shows the user name
DisplayUser Property = "display_user"
//SSHIcon shows when in an SSH session
// SSHIcon shows when in an SSH session
SSHIcon Property = "ssh_icon"
)
@ -49,7 +49,9 @@ func (s *session) getFormattedText() string {
if s.activeSSHSession() {
ssh = s.props.getString(SSHIcon, "\uF817 ")
}
return fmt.Sprintf("%s<%s>%s</>%s<%s>%s</>", ssh, s.props.getColor(UserColor, s.props.foreground), username, separator, s.props.getColor(HostColor, s.props.foreground), computername)
userColor := s.props.getColor(UserColor, s.props.foreground)
hostColor := s.props.getColor(HostColor, s.props.foreground)
return fmt.Sprintf("%s<%s>%s</>%s<%s>%s</>", ssh, userColor, username, separator, hostColor, computername)
}
func (s *session) getComputerName() string {

0
segment_session_test.go Executable file → Normal file
View file

0
segment_shell_test.go Executable file → Normal file
View file

View file

@ -13,13 +13,13 @@ type spotify struct {
}
const (
//PlayingIcon indicates a song is playing
// PlayingIcon indicates a song is playing
PlayingIcon Property = "playing_icon"
//PausedIcon indicates a song is paused
// PausedIcon indicates a song is paused
PausedIcon Property = "paused_icon"
//StoppedIcon indicates a song is stopped
// StoppedIcon indicates a song is stopped
StoppedIcon Property = "stopped_icon"
//TrackSeparator is put between the artist and the track
// TrackSeparator is put between the artist and the track
TrackSeparator Property = "track_separator"
)

View file

@ -9,18 +9,19 @@ import (
)
type spotifyArgs struct {
spotifyDarwinRunning string
spotifyDarwinStatus string
spotifyDarwinArtist string
spotifyDarwinTrack string
running string
status string
artist string
track string
runError error
}
func bootStrapSpotifyDarwinTest(args *spotifyArgs) *spotify {
env := new(MockedEnvironment)
env.On("runCommand", "osascript", []string{"-e", "application \"Spotify\" is running"}).Return(args.spotifyDarwinRunning, nil)
env.On("runCommand", "osascript", []string{"-e", "tell application \"Spotify\" to player state as string"}).Return(args.spotifyDarwinStatus, nil)
env.On("runCommand", "osascript", []string{"-e", "tell application \"Spotify\" to artist of current track as string"}).Return(args.spotifyDarwinArtist, nil)
env.On("runCommand", "osascript", []string{"-e", "tell application \"Spotify\" to name of current track as string"}).Return(args.spotifyDarwinTrack, nil)
env.On("runCommand", "osascript", []string{"-e", "application \"Spotify\" is running"}).Return(args.running, args.runError)
env.On("runCommand", "osascript", []string{"-e", "tell application \"Spotify\" to player state as string"}).Return(args.status, nil)
env.On("runCommand", "osascript", []string{"-e", "tell application \"Spotify\" to artist of current track as string"}).Return(args.artist, nil)
env.On("runCommand", "osascript", []string{"-e", "tell application \"Spotify\" to name of current track as string"}).Return(args.track, nil)
props := &properties{}
s := &spotify{
env: env,
@ -31,7 +32,7 @@ func bootStrapSpotifyDarwinTest(args *spotifyArgs) *spotify {
func TestSpotifyDarwinEnabledAndSpotifyNotRunning(t *testing.T) {
args := &spotifyArgs{
spotifyDarwinRunning: "false",
running: "false",
}
s := bootStrapSpotifyDarwinTest(args)
assert.Equal(t, false, s.enabled())
@ -39,10 +40,10 @@ func TestSpotifyDarwinEnabledAndSpotifyNotRunning(t *testing.T) {
func TestSpotifyDarwinEnabledAndSpotifyPlaying(t *testing.T) {
args := &spotifyArgs{
spotifyDarwinRunning: "true",
spotifyDarwinStatus: "playing",
spotifyDarwinArtist: "Candlemass",
spotifyDarwinTrack: "Spellbreaker",
running: "true",
status: "playing",
artist: "Candlemass",
track: "Spellbreaker",
}
s := bootStrapSpotifyDarwinTest(args)
assert.Equal(t, true, s.enabled())
@ -51,10 +52,10 @@ func TestSpotifyDarwinEnabledAndSpotifyPlaying(t *testing.T) {
func TestSpotifyDarwinEnabledAndSpotifyPaused(t *testing.T) {
args := &spotifyArgs{
spotifyDarwinRunning: "true",
spotifyDarwinStatus: "paused",
spotifyDarwinArtist: "Candlemass",
spotifyDarwinTrack: "Spellbreaker",
running: "true",
status: "paused",
artist: "Candlemass",
track: "Spellbreaker",
}
s := bootStrapSpotifyDarwinTest(args)
assert.Equal(t, true, s.enabled())

2
segment_spotify_test.go Executable file → Normal file
View file

@ -1,5 +1,3 @@
// +build windows
package main
import (

View file

@ -10,13 +10,13 @@ import (
)
type spotifyArgs struct {
spotifyWindowsTitle string
spotifyNotRunningError error
title string
runError error
}
func bootStrapSpotifyWindowsTest(args *spotifyArgs) *spotify {
env := new(MockedEnvironment)
env.On("getWindowTitle", "spotify.exe").Return(args.spotifyWindowsTitle, args.spotifyNotRunningError)
env.On("getWindowTitle", "spotify.exe").Return(args.title, args.runError)
props := &properties{}
s := &spotify{
env: env,
@ -27,7 +27,7 @@ func bootStrapSpotifyWindowsTest(args *spotifyArgs) *spotify {
func TestSpotifyWindowsEnabledAndSpotifyNotRunning(t *testing.T) {
args := &spotifyArgs{
spotifyNotRunningError: errors.New(""),
runError: errors.New(""),
}
s := bootStrapSpotifyWindowsTest(args)
assert.Equal(t, false, s.enabled())
@ -35,7 +35,7 @@ func TestSpotifyWindowsEnabledAndSpotifyNotRunning(t *testing.T) {
func TestSpotifyWindowsEnabledAndSpotifyPlaying(t *testing.T) {
args := &spotifyArgs{
spotifyWindowsTitle: "Candlemass - Spellbreaker",
title: "Candlemass - Spellbreaker",
}
s := bootStrapSpotifyWindowsTest(args)
assert.Equal(t, true, s.enabled())
@ -44,7 +44,7 @@ func TestSpotifyWindowsEnabledAndSpotifyPlaying(t *testing.T) {
func TestSpotifyWindowsEnabledAndSpotifyStopped(t *testing.T) {
args := &spotifyArgs{
spotifyWindowsTitle: "Spotify premium",
title: "Spotify premium",
}
s := bootStrapSpotifyWindowsTest(args)
assert.Equal(t, true, s.enabled())

0
segment_terraform_test.go Executable file → Normal file
View file

0
segment_test.go Executable file → Normal file
View file

View file

@ -6,7 +6,7 @@ type text struct {
}
const (
//TextProperty represents text to write
// TextProperty represents text to write
TextProperty Property = "text"
)

View file

@ -10,7 +10,7 @@ type tempus struct {
}
const (
//TimeFormat uses the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format the current time
// TimeFormat uses the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format the current time
TimeFormat Property = "time_format"
)

18
settings.go Executable file → Normal file
View file

@ -6,31 +6,31 @@ import (
"os"
)
//Settings holds all the theme for rendering the prompt
// Settings holds all the theme for rendering the prompt
type Settings struct {
FinalSpace bool `json:"final_space"`
ConsoleTitle bool `json:"console_title"`
Blocks []*Block `json:"blocks"`
}
//BlockType type of block
// BlockType type of block
type BlockType string
//BlockAlignment aligment of a Block
// BlockAlignment aligment of a Block
type BlockAlignment string
const (
//Prompt writes one or more Segments
// Prompt writes one or more Segments
Prompt BlockType = "prompt"
//LineBreak creates a line break in the prompt
// LineBreak creates a line break in the prompt
LineBreak BlockType = "newline"
//Left aligns left
// Left aligns left
Left BlockAlignment = "left"
//Right aligns right
// Right aligns right
Right BlockAlignment = "right"
)
//Block defines a part of the prompt with optional segments
// Block defines a part of the prompt with optional segments
type Block struct {
Type BlockType `json:"type"`
Alignment BlockAlignment `json:"alignment"`
@ -39,7 +39,7 @@ type Block struct {
Segments []*Segment `json:"segments"`
}
//GetSettings returns the default configuration including possible user overrides
// GetSettings returns the default configuration including possible user overrides
func GetSettings(env environmentInfo) *Settings {
settings, err := loadUserConfiguration(env)
if err != nil {