mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor: remove deprecated functionality
This commit is contained in:
parent
f5178e8c8c
commit
0987cafad0
|
@ -34,10 +34,7 @@ Display the currently active .NET SDK version.
|
|||
- `always`: the segment is always displayed
|
||||
- `files`: the segment is only displayed when `*.cs`, `*.vb`, `*.fs`, `*.fsx`, `*.sln`, `*.csproj`, `*.vbproj`,
|
||||
or `*.fsproj` files are present (default)
|
||||
- unsupported_version_icon: `string` - text/icon that is displayed when the active .NET SDK version (e.g., one specified
|
||||
by `global.json`) is not installed/supported - defaults to `\uf071` (X in a rectangle box)
|
||||
- version_url_template: `string` - A go [text/template][go-text-template] template extended
|
||||
with [sprig][sprig] utilizing the properties below. Defaults does nothing(backward compatibility).
|
||||
- version_url_template: `string` - A go text/template [template][templates] that creates the changelog URL
|
||||
|
||||
## [Template][templates] Properties
|
||||
|
||||
|
|
|
@ -209,10 +209,6 @@ func getDefaultConfig(info string) *Config {
|
|||
Background: "#f36943",
|
||||
Foreground: "#193549",
|
||||
Properties: properties{
|
||||
ColorBackground: true,
|
||||
ChargedColor: "#4caf50",
|
||||
ChargingColor: "#40c4ff",
|
||||
DischargingColor: "#ff5722",
|
||||
Postfix: "\uF295 ",
|
||||
},
|
||||
},
|
||||
|
@ -262,10 +258,7 @@ func getDefaultConfig(info string) *Config {
|
|||
LeadingDiamond: "<transparent,#2e9599>\uE0B0</>",
|
||||
TrailingDiamond: "\uE0B4",
|
||||
Properties: properties{
|
||||
DisplayExitCode: false,
|
||||
AlwaysEnabled: true,
|
||||
ErrorColor: "#f1184c",
|
||||
ColorBackground: true,
|
||||
Prefix: " \uE23A",
|
||||
},
|
||||
},
|
||||
|
|
|
@ -15,8 +15,6 @@ const (
|
|||
Prefix Property = "prefix"
|
||||
// Postfix adds a text postfix to the segment
|
||||
Postfix Property = "postfix"
|
||||
// ColorBackground color the background or foreground when a specific color is set
|
||||
ColorBackground Property = "color_background"
|
||||
// IncludeFolders folders to be included for the segment logic
|
||||
IncludeFolders Property = "include_folders"
|
||||
// ExcludeFolders folders to be excluded for the segment logic
|
||||
|
|
|
@ -9,91 +9,93 @@ import (
|
|||
const (
|
||||
expected = "expected"
|
||||
expectedColor = "#768954"
|
||||
|
||||
Foo Property = "color"
|
||||
)
|
||||
|
||||
func TestGetString(t *testing.T) {
|
||||
var properties properties = properties{TextProperty: expected}
|
||||
value := properties.getString(TextProperty, "err")
|
||||
var properties properties = properties{Foo: expected}
|
||||
value := properties.getString(Foo, "err")
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestGetStringNoEntry(t *testing.T) {
|
||||
var properties properties = properties{}
|
||||
value := properties.getString(TextProperty, expected)
|
||||
value := properties.getString(Foo, expected)
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestGetStringNoTextEntry(t *testing.T) {
|
||||
var properties properties = properties{TextProperty: true}
|
||||
value := properties.getString(TextProperty, expected)
|
||||
var properties properties = properties{Foo: true}
|
||||
value := properties.getString(Foo, expected)
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestGetHexColor(t *testing.T) {
|
||||
expected := expectedColor
|
||||
var properties properties = properties{UserColor: expected}
|
||||
value := properties.getColor(UserColor, "#789123")
|
||||
var properties properties = properties{Foo: expected}
|
||||
value := properties.getColor(Foo, "#789123")
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestGetColor(t *testing.T) {
|
||||
expected := "yellow"
|
||||
var properties properties = properties{UserColor: expected}
|
||||
value := properties.getColor(UserColor, "#789123")
|
||||
var properties properties = properties{Foo: expected}
|
||||
value := properties.getColor(Foo, "#789123")
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestDefaultColorWithInvalidColorCode(t *testing.T) {
|
||||
expected := expectedColor
|
||||
var properties properties = properties{UserColor: "invalid"}
|
||||
value := properties.getColor(UserColor, expected)
|
||||
var properties properties = properties{Foo: "invalid"}
|
||||
value := properties.getColor(Foo, expected)
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestDefaultColorWithUnavailableProperty(t *testing.T) {
|
||||
expected := expectedColor
|
||||
var properties properties = properties{}
|
||||
value := properties.getColor(UserColor, expected)
|
||||
value := properties.getColor(Foo, expected)
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestGetPaletteColor(t *testing.T) {
|
||||
expected := "p:red"
|
||||
var properties properties = properties{Background: expected}
|
||||
value := properties.getColor(Background, "white")
|
||||
var properties properties = properties{Foo: expected}
|
||||
value := properties.getColor(Foo, "white")
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestGetBool(t *testing.T) {
|
||||
expected := true
|
||||
var properties properties = properties{DisplayHost: expected}
|
||||
value := properties.getBool(DisplayHost, false)
|
||||
var properties properties = properties{Foo: expected}
|
||||
value := properties.getBool(Foo, false)
|
||||
assert.True(t, value)
|
||||
}
|
||||
|
||||
func TestGetBoolPropertyNotInMap(t *testing.T) {
|
||||
var properties properties = properties{}
|
||||
value := properties.getBool(DisplayHost, false)
|
||||
value := properties.getBool(Foo, false)
|
||||
assert.False(t, value)
|
||||
}
|
||||
|
||||
func TestGetBoolInvalidProperty(t *testing.T) {
|
||||
var properties properties = properties{DisplayHost: "borked"}
|
||||
value := properties.getBool(DisplayHost, false)
|
||||
var properties properties = properties{Foo: "borked"}
|
||||
value := properties.getBool(Foo, false)
|
||||
assert.False(t, value)
|
||||
}
|
||||
|
||||
func TestGetFloat64(t *testing.T) {
|
||||
expected := float64(1337)
|
||||
var properties properties = properties{"myfloat": expected}
|
||||
value := properties.getFloat64("myfloat", 9001)
|
||||
var properties properties = properties{Foo: expected}
|
||||
value := properties.getFloat64(Foo, 9001)
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
func TestGetFloat64PropertyNotInMap(t *testing.T) {
|
||||
expected := float64(1337)
|
||||
var properties properties = properties{}
|
||||
value := properties.getFloat64(ThresholdProperty, expected)
|
||||
value := properties.getFloat64(Foo, expected)
|
||||
assert.Equal(t, expected, value)
|
||||
}
|
||||
|
||||
|
|
|
@ -45,11 +45,6 @@ type Properties interface {
|
|||
getInt(property Property, defaultValue int) int
|
||||
getKeyValueMap(property Property, defaultValue map[string]string) map[string]string
|
||||
getStringArray(property Property, defaultValue []string) []string
|
||||
// for legacy purposes
|
||||
getOneOfBool(property, legacyProperty Property, defaultValue bool) bool
|
||||
getOneOfString(property, legacyProperty Property, defaultValue string) string
|
||||
hasOneOf(properties ...Property) bool
|
||||
set(property Property, value interface{})
|
||||
}
|
||||
|
||||
// SegmentWriter is the interface used to define what and if to write to the prompt
|
||||
|
@ -96,8 +91,6 @@ const (
|
|||
Node SegmentType = "node"
|
||||
// Os write os specific icon
|
||||
Os SegmentType = "os"
|
||||
// EnvVar writes the content of an environment variable
|
||||
EnvVar SegmentType = "envvar"
|
||||
// Az writes the Azure subscription info we're currently in
|
||||
Az SegmentType = "az"
|
||||
// Kubectl writes the Kubernetes context we're currently in
|
||||
|
@ -239,13 +232,11 @@ func (segment *Segment) shouldInvokeWithTip(tip string) bool {
|
|||
}
|
||||
|
||||
func (segment *Segment) foreground() string {
|
||||
color := segment.Properties.getColor(ForegroundOverride, segment.Foreground)
|
||||
return segment.getColor(segment.ForegroundTemplates, color)
|
||||
return segment.getColor(segment.ForegroundTemplates, segment.Foreground)
|
||||
}
|
||||
|
||||
func (segment *Segment) background() string {
|
||||
color := segment.Properties.getColor(BackgroundOverride, segment.Background)
|
||||
return segment.getColor(segment.BackgroundTemplates, color)
|
||||
return segment.getColor(segment.BackgroundTemplates, segment.Background)
|
||||
}
|
||||
|
||||
func (segment *Segment) mapSegmentWithWriter(env Environment) error {
|
||||
|
@ -267,7 +258,6 @@ func (segment *Segment) mapSegmentWithWriter(env Environment) error {
|
|||
ShellInfo: &shell{},
|
||||
Node: &node{},
|
||||
Os: &osInfo{},
|
||||
EnvVar: &envvar{},
|
||||
Az: &az{},
|
||||
Kubectl: &kubectl{},
|
||||
Dotnet: &dotnet{},
|
||||
|
|
|
@ -10,11 +10,8 @@ type angular struct {
|
|||
}
|
||||
|
||||
func (a *angular) string() string {
|
||||
segmentTemplate := a.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return a.language.string()
|
||||
}
|
||||
return a.language.renderTemplate(segmentTemplate, a)
|
||||
segmentTemplate := a.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return a.language.string(segmentTemplate, a)
|
||||
}
|
||||
|
||||
func (a *angular) init(props Properties, env Environment) {
|
||||
|
|
|
@ -5,11 +5,8 @@ type azfunc struct {
|
|||
}
|
||||
|
||||
func (az *azfunc) string() string {
|
||||
segmentTemplate := az.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return az.language.string()
|
||||
}
|
||||
return az.language.renderTemplate(segmentTemplate, az)
|
||||
segmentTemplate := az.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return az.language.string(segmentTemplate, az)
|
||||
}
|
||||
|
||||
func (az *azfunc) init(props Properties, env Environment) {
|
||||
|
|
|
@ -45,10 +45,6 @@ func (b *batt) enabled() bool {
|
|||
batteryPercentage := b.Battery.Current / b.Battery.Full * 100
|
||||
b.Percentage = int(math.Min(100, batteryPercentage))
|
||||
|
||||
if !b.shouldDisplay() {
|
||||
return false
|
||||
}
|
||||
|
||||
switch b.Battery.State {
|
||||
case battery.Discharging, battery.NotCharging:
|
||||
b.Icon = b.props.getString(DischargingIcon, "")
|
||||
|
@ -59,7 +55,6 @@ func (b *batt) enabled() bool {
|
|||
case battery.Empty, battery.Unknown:
|
||||
return true
|
||||
}
|
||||
b.colorSegment()
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -7,12 +7,6 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
chargingColor = "#123456"
|
||||
dischargingColor = "#765432"
|
||||
chargedColor = "#248644"
|
||||
)
|
||||
|
||||
func TestGetBatteryColors(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
|
|
|
@ -5,11 +5,8 @@ type crystal struct {
|
|||
}
|
||||
|
||||
func (c *crystal) string() string {
|
||||
segmentTemplate := c.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return c.language.string()
|
||||
}
|
||||
return c.language.renderTemplate(segmentTemplate, c)
|
||||
segmentTemplate := c.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return c.language.string(segmentTemplate, c)
|
||||
}
|
||||
|
||||
func (c *crystal) init(props Properties, env Environment) {
|
||||
|
|
|
@ -5,11 +5,8 @@ type dart struct {
|
|||
}
|
||||
|
||||
func (d *dart) string() string {
|
||||
segmentTemplate := d.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return d.language.string()
|
||||
}
|
||||
return d.language.renderTemplate(segmentTemplate, d)
|
||||
segmentTemplate := d.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return d.language.string(segmentTemplate, d)
|
||||
}
|
||||
|
||||
func (d *dart) init(props Properties, env Environment) {
|
||||
|
|
|
@ -1,442 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/distatus/battery"
|
||||
)
|
||||
|
||||
// Segment
|
||||
|
||||
const (
|
||||
BackgroundOverride Property = "background"
|
||||
ForegroundOverride Property = "foreground"
|
||||
)
|
||||
|
||||
// Properties
|
||||
|
||||
func (p properties) getOneOfBool(property, legacyProperty Property, defaultValue bool) bool {
|
||||
_, found := p[legacyProperty]
|
||||
if found {
|
||||
return p.getBool(legacyProperty, defaultValue)
|
||||
}
|
||||
return p.getBool(property, defaultValue)
|
||||
}
|
||||
|
||||
func (p properties) getOneOfString(property, legacyProperty Property, defaultValue string) string {
|
||||
_, found := p[legacyProperty]
|
||||
if found {
|
||||
return p.getString(legacyProperty, defaultValue)
|
||||
}
|
||||
return p.getString(property, defaultValue)
|
||||
}
|
||||
|
||||
func (p properties) hasOneOf(properties ...Property) bool {
|
||||
for _, property := range properties {
|
||||
if _, found := p[property]; found {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p properties) set(property Property, value interface{}) {
|
||||
p[property] = value
|
||||
}
|
||||
|
||||
// GIT Segement
|
||||
|
||||
const (
|
||||
// DisplayStatus shows the status of the repository
|
||||
DisplayStatus Property = "display_status"
|
||||
// DisplayStashCount show stash count or not
|
||||
DisplayStashCount Property = "display_stash_count"
|
||||
// DisplayWorktreeCount show worktree count or not
|
||||
DisplayWorktreeCount Property = "display_worktree_count"
|
||||
// DisplayUpstreamIcon show or hide the upstream icon
|
||||
DisplayUpstreamIcon Property = "display_upstream_icon"
|
||||
// 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 Property = "local_staged_icon"
|
||||
// DisplayStatusDetail shows the detailed status of the repository
|
||||
DisplayStatusDetail Property = "display_status_detail"
|
||||
// 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 Property = "staging_color"
|
||||
// StatusColorsEnabled enables status colors
|
||||
StatusColorsEnabled Property = "status_colors_enabled"
|
||||
// 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 Property = "ahead_and_behind_color"
|
||||
// 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 Property = "ahead_color"
|
||||
// WorktreeCountIcon shows before the worktree context
|
||||
WorktreeCountIcon Property = "worktree_count_icon"
|
||||
// StashCountIcon shows before the stash context
|
||||
StashCountIcon Property = "stash_count_icon"
|
||||
// StatusSeparatorIcon shows between staging and working area
|
||||
StatusSeparatorIcon Property = "status_separator_icon"
|
||||
)
|
||||
|
||||
func (g *git) deprecatedString(statusColorsEnabled bool) string {
|
||||
if statusColorsEnabled {
|
||||
g.SetStatusColor()
|
||||
}
|
||||
buffer := new(bytes.Buffer)
|
||||
// remote (if available)
|
||||
if len(g.UpstreamIcon) != 0 {
|
||||
fmt.Fprintf(buffer, "%s", g.UpstreamIcon)
|
||||
}
|
||||
// branchName
|
||||
fmt.Fprintf(buffer, "%s", g.HEAD)
|
||||
if len(g.BranchStatus) > 0 {
|
||||
buffer.WriteString(g.BranchStatus)
|
||||
}
|
||||
|
||||
// status
|
||||
if g.Staging.Changed() {
|
||||
fmt.Fprint(buffer, g.getStatusDetailString(g.Staging, StagingColor, LocalStagingIcon, " \uF046"))
|
||||
}
|
||||
if g.Staging.Changed() && g.Working.Changed() {
|
||||
fmt.Fprint(buffer, g.props.getString(StatusSeparatorIcon, " |"))
|
||||
}
|
||||
if g.Working.Changed() {
|
||||
fmt.Fprint(buffer, g.getStatusDetailString(g.Working, WorkingColor, LocalWorkingIcon, " \uF044"))
|
||||
}
|
||||
if g.StashCount != 0 {
|
||||
fmt.Fprintf(buffer, " %s%d", g.props.getString(StashCountIcon, "\uF692 "), g.StashCount)
|
||||
}
|
||||
if g.WorktreeCount != 0 {
|
||||
fmt.Fprintf(buffer, " %s%d", g.props.getString(WorktreeCountIcon, "\uf1bb "), g.WorktreeCount)
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
func (g *git) SetStatusColor() {
|
||||
if g.props.getBool(ColorBackground, true) {
|
||||
g.props.set(BackgroundOverride, g.getStatusColor(g.props.getColor(BackgroundOverride, "")))
|
||||
} else {
|
||||
g.props.set(ForegroundOverride, g.getStatusColor(g.props.getColor(ForegroundOverride, "")))
|
||||
}
|
||||
}
|
||||
|
||||
func (g *git) getStatusColor(defaultValue string) string {
|
||||
if g.Staging.Changed() || g.Working.Changed() {
|
||||
return g.props.getColor(LocalChangesColor, defaultValue)
|
||||
} else if g.Ahead > 0 && g.Behind > 0 {
|
||||
return g.props.getColor(AheadAndBehindColor, defaultValue)
|
||||
} else if g.Ahead > 0 {
|
||||
return g.props.getColor(AheadColor, defaultValue)
|
||||
} else if g.Behind > 0 {
|
||||
return g.props.getColor(BehindColor, defaultValue)
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
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.getColor(ForegroundOverride, ""))
|
||||
detail := ""
|
||||
if g.props.getBool(DisplayStatusDetail, true) {
|
||||
detail = status.String()
|
||||
}
|
||||
statusStr := g.colorStatusString(prefix, detail, foregroundColor)
|
||||
return strings.TrimSpace(statusStr)
|
||||
}
|
||||
|
||||
func (g *git) colorStatusString(prefix, status, color string) string {
|
||||
if len(color) == 0 {
|
||||
return fmt.Sprintf("%s %s", prefix, status)
|
||||
}
|
||||
if len(status) == 0 {
|
||||
return fmt.Sprintf("<%s>%s</>", color, prefix)
|
||||
}
|
||||
if strings.Contains(prefix, "</>") {
|
||||
return fmt.Sprintf("%s <%s>%s</>", prefix, color, status)
|
||||
}
|
||||
return fmt.Sprintf("<%s>%s %s</>", color, prefix, status)
|
||||
}
|
||||
|
||||
// EXIT Segment
|
||||
|
||||
const (
|
||||
// DisplayExitCode shows or hides the error code
|
||||
DisplayExitCode Property = "display_exit_code"
|
||||
// ErrorColor specify a different foreground color for the error text when using always_show = true
|
||||
ErrorColor Property = "error_color"
|
||||
// AlwaysNumeric shows error codes as numbers
|
||||
AlwaysNumeric Property = "always_numeric"
|
||||
// SuccessIcon displays when there's no error and AlwaysEnabled = true
|
||||
SuccessIcon Property = "success_icon"
|
||||
// ErrorIcon displays when there's an error
|
||||
ErrorIcon Property = "error_icon"
|
||||
)
|
||||
|
||||
func (e *exit) deprecatedString() string {
|
||||
colorBackground := e.props.getBool(ColorBackground, false)
|
||||
code := e.env.lastErrorCode()
|
||||
if code != 0 && !colorBackground {
|
||||
e.props.set(ForegroundOverride, e.props.getColor(ErrorColor, e.props.getColor(ForegroundOverride, "")))
|
||||
}
|
||||
if code != 0 && colorBackground {
|
||||
e.props.set(BackgroundOverride, e.props.getColor(ErrorColor, e.props.getColor(BackgroundOverride, "")))
|
||||
}
|
||||
if code == 0 {
|
||||
return e.props.getString(SuccessIcon, "")
|
||||
}
|
||||
errorIcon := e.props.getString(ErrorIcon, "")
|
||||
if !e.props.getBool(DisplayExitCode, true) {
|
||||
return errorIcon
|
||||
}
|
||||
if e.props.getBool(AlwaysNumeric, false) {
|
||||
return fmt.Sprintf("%s%d", errorIcon, code)
|
||||
}
|
||||
return fmt.Sprintf("%s%s", errorIcon, e.Text)
|
||||
}
|
||||
|
||||
// Battery segment
|
||||
|
||||
const (
|
||||
// ChargedColor to display when fully charged
|
||||
ChargedColor Property = "charged_color"
|
||||
// ChargingColor to display when charging
|
||||
ChargingColor Property = "charging_color"
|
||||
// DischargingColor to display when discharging
|
||||
DischargingColor Property = "discharging_color"
|
||||
// DisplayCharging Hide the battery icon while it's charging
|
||||
DisplayCharging Property = "display_charging"
|
||||
// DisplayCharged Hide the battery icon when it's charged
|
||||
DisplayCharged Property = "display_charged"
|
||||
)
|
||||
|
||||
func (b *batt) colorSegment() {
|
||||
if !b.props.hasOneOf(ChargedColor, ChargingColor, DischargingColor) {
|
||||
return
|
||||
}
|
||||
var colorProperty Property
|
||||
switch b.Battery.State {
|
||||
case battery.Discharging, battery.NotCharging:
|
||||
colorProperty = DischargingColor
|
||||
case battery.Charging:
|
||||
colorProperty = ChargingColor
|
||||
case battery.Full:
|
||||
colorProperty = ChargedColor
|
||||
case battery.Empty, battery.Unknown:
|
||||
return
|
||||
}
|
||||
colorBackground := b.props.getBool(ColorBackground, false)
|
||||
if colorBackground {
|
||||
b.props.set(BackgroundOverride, b.props.getColor(colorProperty, b.props.getColor(BackgroundOverride, "")))
|
||||
} else {
|
||||
b.props.set(ForegroundOverride, b.props.getColor(colorProperty, b.props.getColor(ForegroundOverride, "")))
|
||||
}
|
||||
}
|
||||
|
||||
func (b *batt) shouldDisplay() bool {
|
||||
if !b.props.hasOneOf(DisplayCharged, DisplayCharging) {
|
||||
return true
|
||||
}
|
||||
displayCharged := b.props.getBool(DisplayCharged, true)
|
||||
if !displayCharged && (b.Battery.State == battery.Full) {
|
||||
return false
|
||||
}
|
||||
displayCharging := b.props.getBool(DisplayCharging, true)
|
||||
if !displayCharging && (b.Battery.State == battery.Charging) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Session
|
||||
|
||||
const (
|
||||
// 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 Property = "user_color"
|
||||
// HostColor if set, is used to color the computer name
|
||||
HostColor Property = "host_color"
|
||||
// DisplayHost hides or show the computer name
|
||||
DisplayHost Property = "display_host"
|
||||
// DisplayUser hides or shows the user name
|
||||
DisplayUser Property = "display_user"
|
||||
// DefaultUserName holds the default user of the platform
|
||||
DefaultUserName Property = "default_user_name"
|
||||
// SSHIcon is the icon used for SSH sessions
|
||||
SSHIcon Property = "ssh_icon"
|
||||
|
||||
defaultUserEnvVar = "POSH_SESSION_DEFAULT_USER"
|
||||
)
|
||||
|
||||
func (s *session) getDefaultUser() string {
|
||||
user := s.env.getenv(defaultUserEnvVar)
|
||||
if len(user) == 0 {
|
||||
user = s.props.getString(DefaultUserName, "")
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
func (s *session) legacyEnabled() bool {
|
||||
if s.props.getBool(DisplayUser, true) {
|
||||
s.userName = s.getUserName()
|
||||
}
|
||||
if s.props.getBool(DisplayHost, true) {
|
||||
s.hostName = s.getComputerName()
|
||||
}
|
||||
s.DefaultUserName = s.getDefaultUser()
|
||||
showDefaultUser := s.props.getBool(DisplayDefault, true)
|
||||
if !showDefaultUser && s.DefaultUserName == s.userName {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *session) legacyString() string {
|
||||
separator := ""
|
||||
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
|
||||
separator = s.props.getString(UserInfoSeparator, "@")
|
||||
}
|
||||
var sshIcon string
|
||||
if s.SSHSession {
|
||||
sshIcon = s.props.getString(SSHIcon, "\uF817 ")
|
||||
}
|
||||
defaulColor := s.props.getColor(ForegroundOverride, "")
|
||||
userColor := s.props.getColor(UserColor, defaulColor)
|
||||
hostColor := s.props.getColor(HostColor, defaulColor)
|
||||
if len(userColor) > 0 && len(hostColor) > 0 {
|
||||
return fmt.Sprintf("%s<%s>%s</>%s<%s>%s</>", sshIcon, userColor, s.userName, separator, hostColor, s.hostName)
|
||||
}
|
||||
if len(userColor) > 0 {
|
||||
return fmt.Sprintf("%s<%s>%s</>%s%s", sshIcon, userColor, s.userName, separator, s.hostName)
|
||||
}
|
||||
if len(hostColor) > 0 {
|
||||
return fmt.Sprintf("%s%s%s<%s>%s</>", sshIcon, s.userName, separator, hostColor, s.hostName)
|
||||
}
|
||||
return fmt.Sprintf("%s%s%s%s", sshIcon, s.userName, separator, s.hostName)
|
||||
}
|
||||
|
||||
// Language
|
||||
|
||||
const (
|
||||
// DisplayVersion show the version number or not
|
||||
DisplayVersion Property = "display_version"
|
||||
// VersionMismatchColor displays empty string by default
|
||||
VersionMismatchColor Property = "version_mismatch_color"
|
||||
// EnableVersionMismatch displays empty string by default
|
||||
EnableVersionMismatch Property = "enable_version_mismatch"
|
||||
)
|
||||
|
||||
func (l *language) string() string {
|
||||
if !l.props.getOneOfBool(FetchVersion, DisplayVersion, true) {
|
||||
return ""
|
||||
}
|
||||
displayError := l.props.getBool(DisplayError, true)
|
||||
if len(l.Error) != 0 && displayError {
|
||||
return l.Error
|
||||
}
|
||||
|
||||
segmentTemplate := l.props.getString(SegmentTemplate, "{{ .Full }}")
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: l.version,
|
||||
Env: l.env,
|
||||
}
|
||||
text, err := template.render()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
if l.props.getBool(EnableVersionMismatch, false) {
|
||||
l.setVersionFileMismatch()
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (l *language) colorMismatch() {
|
||||
if l.props.getBool(ColorBackground, false) {
|
||||
l.props.set(BackgroundOverride, l.props.getColor(VersionMismatchColor, l.props.getColor(BackgroundOverride, "")))
|
||||
return
|
||||
}
|
||||
l.props.set(ForegroundOverride, l.props.getColor(VersionMismatchColor, l.props.getColor(ForegroundOverride, "")))
|
||||
}
|
||||
|
||||
// Python
|
||||
|
||||
const (
|
||||
// DisplayVirtualEnv shows or hides the virtual env
|
||||
DisplayVirtualEnv Property = "display_virtual_env"
|
||||
)
|
||||
|
||||
func (p *python) legacyString() string {
|
||||
if p.Venv == "" {
|
||||
return p.language.string()
|
||||
}
|
||||
version := p.language.string()
|
||||
if version == "" {
|
||||
return p.Venv
|
||||
}
|
||||
return fmt.Sprintf("%s %s", p.Venv, version)
|
||||
}
|
||||
|
||||
// Node
|
||||
|
||||
const (
|
||||
// DisplayPackageManager shows if NPM or Yarn is used
|
||||
DisplayPackageManager Property = "display_package_manager"
|
||||
)
|
||||
|
||||
// Environment Variable
|
||||
|
||||
type envvar struct {
|
||||
props Properties
|
||||
env Environment
|
||||
Value string
|
||||
}
|
||||
|
||||
const (
|
||||
// VarName name of the variable
|
||||
VarName Property = "var_name"
|
||||
)
|
||||
|
||||
func (e *envvar) enabled() bool {
|
||||
name := e.props.getString(VarName, "")
|
||||
e.Value = e.env.getenv(name)
|
||||
return e.Value != ""
|
||||
}
|
||||
|
||||
func (e *envvar) string() string {
|
||||
segmentTemplate := e.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return e.Value
|
||||
}
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: e,
|
||||
Env: e.env,
|
||||
}
|
||||
text, err := template.render()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (e *envvar) init(props Properties, env Environment) {
|
||||
e.props = props
|
||||
e.env = env
|
||||
}
|
||||
|
||||
// Dotnet
|
||||
|
||||
const (
|
||||
// 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"
|
||||
)
|
|
@ -1,813 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/distatus/battery"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// GIT Segment
|
||||
|
||||
func TestGetStatusDetailStringDefault(t *testing.T) {
|
||||
expected := "icon +1"
|
||||
status := &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Added: 1,
|
||||
},
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: properties{},
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
|
||||
}
|
||||
|
||||
func TestGetStatusDetailStringDefaultColorOverride(t *testing.T) {
|
||||
expected := "<#123456>icon +1</>"
|
||||
status := &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Added: 1,
|
||||
},
|
||||
}
|
||||
props := properties{
|
||||
WorkingColor: "#123456",
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
|
||||
}
|
||||
|
||||
func TestGetStatusDetailStringDefaultColorOverrideAndIconColorOverride(t *testing.T) {
|
||||
expected := "<#789123>work</> <#123456>+1</>"
|
||||
status := &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Added: 1,
|
||||
},
|
||||
}
|
||||
props := properties{
|
||||
WorkingColor: "#123456",
|
||||
LocalWorkingIcon: "<#789123>work</>",
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
|
||||
}
|
||||
|
||||
func TestGetStatusDetailStringDefaultColorOverrideNoIconColorOverride(t *testing.T) {
|
||||
expected := "<#123456>work +1</>"
|
||||
status := &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Added: 1,
|
||||
},
|
||||
}
|
||||
props := properties{
|
||||
WorkingColor: "#123456",
|
||||
LocalWorkingIcon: "work",
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
|
||||
}
|
||||
|
||||
func TestGetStatusDetailStringNoStatus(t *testing.T) {
|
||||
expected := "icon"
|
||||
status := &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Added: 1,
|
||||
},
|
||||
}
|
||||
props := properties{
|
||||
DisplayStatusDetail: false,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
|
||||
}
|
||||
|
||||
func TestGetStatusDetailStringNoStatusColorOverride(t *testing.T) {
|
||||
expected := "<#123456>icon</>"
|
||||
status := &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Added: 1,
|
||||
},
|
||||
}
|
||||
props := properties{
|
||||
DisplayStatusDetail: false,
|
||||
WorkingColor: "#123456",
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
|
||||
}
|
||||
|
||||
func TestGetStatusColorLocalChangesStaging(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
LocalChangesColor: expected,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Modified: 1,
|
||||
},
|
||||
},
|
||||
Working: &GitStatus{},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
|
||||
}
|
||||
|
||||
func TestGetStatusColorLocalChangesWorking(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
LocalChangesColor: expected,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{},
|
||||
Working: &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Modified: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
|
||||
}
|
||||
|
||||
func TestGetStatusColorAheadAndBehind(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
AheadAndBehindColor: expected,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{},
|
||||
Working: &GitStatus{},
|
||||
Ahead: 1,
|
||||
Behind: 3,
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
|
||||
}
|
||||
|
||||
func TestGetStatusColorAhead(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
AheadColor: expected,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{},
|
||||
Working: &GitStatus{},
|
||||
Ahead: 1,
|
||||
Behind: 0,
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
|
||||
}
|
||||
|
||||
func TestGetStatusColorBehind(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
BehindColor: expected,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{},
|
||||
Working: &GitStatus{},
|
||||
Ahead: 0,
|
||||
Behind: 5,
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusColor("#fg1111"))
|
||||
}
|
||||
|
||||
func TestGetStatusColorDefault(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
BehindColor: changesColor,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{},
|
||||
Working: &GitStatus{},
|
||||
Ahead: 0,
|
||||
Behind: 0,
|
||||
}
|
||||
assert.Equal(t, expected, g.getStatusColor(expected))
|
||||
}
|
||||
|
||||
func TestSetStatusColorForeground(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
LocalChangesColor: changesColor,
|
||||
ColorBackground: false,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Added: 1,
|
||||
},
|
||||
},
|
||||
Working: &GitStatus{},
|
||||
}
|
||||
g.SetStatusColor()
|
||||
assert.Equal(t, expected, g.props.getColor(ForegroundOverride, ""))
|
||||
}
|
||||
|
||||
func TestSetStatusColorBackground(t *testing.T) {
|
||||
expected := changesColor
|
||||
props := properties{
|
||||
LocalChangesColor: changesColor,
|
||||
ColorBackground: true,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
props: props,
|
||||
},
|
||||
Staging: &GitStatus{},
|
||||
Working: &GitStatus{
|
||||
ScmStatus: ScmStatus{
|
||||
Modified: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
g.SetStatusColor()
|
||||
assert.Equal(t, expected, g.props.getColor(BackgroundOverride, ""))
|
||||
}
|
||||
|
||||
func TestStatusColorsWithoutDisplayStatus(t *testing.T) {
|
||||
expected := changesColor
|
||||
status := "## main...origin/main [ahead 33]\n M myfile"
|
||||
env := new(MockedEnvironment)
|
||||
env.On("isWsl").Return(false)
|
||||
env.On("inWSLSharedDrive").Return(false)
|
||||
env.On("getRuntimeGOOS").Return("unix")
|
||||
env.On("hasCommand", "git").Return(true)
|
||||
fileInfo := &fileInfo{
|
||||
path: "/dir/hello",
|
||||
parentFolder: "/dir",
|
||||
isDir: true,
|
||||
}
|
||||
env.On("hasParentFilePath", ".git").Return(fileInfo, nil)
|
||||
env.On("getFileContent", fmt.Sprintf("%s/HEAD", fileInfo.path)).Return("")
|
||||
env.mockGitCommand(fileInfo.path, "", "describe", "--tags", "--exact-match")
|
||||
env.mockGitCommand(fileInfo.path, status, "status", "-unormal", "--branch", "--porcelain=2")
|
||||
env.On("hasFolder", fmt.Sprintf("%s/rebase-merge", fileInfo.path)).Return(false)
|
||||
env.On("hasFolder", fmt.Sprintf("%s/rebase-apply", fileInfo.path)).Return(false)
|
||||
env.On("hasFilesInDir", fileInfo.path, "CHERRY_PICK_HEAD").Return(false)
|
||||
env.On("hasFilesInDir", fileInfo.path, "REVERT_HEAD").Return(false)
|
||||
env.On("hasFilesInDir", fileInfo.path, "MERGE_MSG").Return(false)
|
||||
env.On("hasFilesInDir", fileInfo.path, "MERGE_HEAD").Return(false)
|
||||
env.On("hasFilesInDir", fileInfo.path, "sequencer/todo").Return(false)
|
||||
|
||||
props := properties{
|
||||
DisplayStatus: false,
|
||||
StatusColorsEnabled: true,
|
||||
LocalChangesColor: expected,
|
||||
}
|
||||
g := &git{
|
||||
scm: scm{
|
||||
env: env,
|
||||
props: props,
|
||||
},
|
||||
gitWorkingFolder: "",
|
||||
}
|
||||
g.Working = &GitStatus{}
|
||||
g.Staging = &GitStatus{}
|
||||
_ = g.enabled()
|
||||
g.string()
|
||||
assert.Equal(t, expected, g.props.getColor(BackgroundOverride, ""))
|
||||
}
|
||||
|
||||
// EXIT Segement
|
||||
|
||||
func TestExitWriterDeprecatedString(t *testing.T) {
|
||||
cases := []struct {
|
||||
ExitCode int
|
||||
Expected string
|
||||
SuccessIcon string
|
||||
ErrorIcon string
|
||||
DisplayExitCode bool
|
||||
AlwaysNumeric bool
|
||||
}{
|
||||
{ExitCode: 129, Expected: "SIGHUP", DisplayExitCode: true},
|
||||
{ExitCode: 5001, Expected: "5001", DisplayExitCode: true},
|
||||
{ExitCode: 147, Expected: "SIGSTOP", DisplayExitCode: true},
|
||||
{ExitCode: 147, Expected: "", DisplayExitCode: false},
|
||||
{ExitCode: 147, Expected: "147", DisplayExitCode: true, AlwaysNumeric: true},
|
||||
{ExitCode: 0, Expected: "wooopie", SuccessIcon: "wooopie"},
|
||||
{ExitCode: 129, Expected: "err SIGHUP", ErrorIcon: "err ", DisplayExitCode: true},
|
||||
{ExitCode: 129, Expected: "err", ErrorIcon: "err", DisplayExitCode: false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(MockedEnvironment)
|
||||
env.On("lastErrorCode").Return(tc.ExitCode)
|
||||
props := properties{
|
||||
SuccessIcon: tc.SuccessIcon,
|
||||
ErrorIcon: tc.ErrorIcon,
|
||||
DisplayExitCode: tc.DisplayExitCode,
|
||||
AlwaysNumeric: tc.AlwaysNumeric,
|
||||
}
|
||||
e := &exit{
|
||||
env: env,
|
||||
props: props,
|
||||
}
|
||||
_ = e.enabled()
|
||||
assert.Equal(t, tc.Expected, e.deprecatedString())
|
||||
}
|
||||
}
|
||||
|
||||
// Battery Segment
|
||||
|
||||
func TestBatterySegmentSingle(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
Batteries []*battery.Battery
|
||||
ExpectedString string
|
||||
ExpectedEnabled bool
|
||||
ExpectedColor string
|
||||
ColorBackground bool
|
||||
DisplayError bool
|
||||
Error error
|
||||
DisableCharging bool
|
||||
DisableCharged bool
|
||||
}{
|
||||
{Case: "80% charging", Batteries: []*battery.Battery{{Full: 100, State: battery.Charging, Current: 80}}, ExpectedString: "charging 80", ExpectedEnabled: true},
|
||||
{Case: "battery full", Batteries: []*battery.Battery{{Full: 100, State: battery.Full, Current: 100}}, ExpectedString: "charged 100", ExpectedEnabled: true},
|
||||
{Case: "70% discharging", Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}}, ExpectedString: "going down 70", ExpectedEnabled: true},
|
||||
{
|
||||
Case: "discharging background color",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}},
|
||||
ExpectedString: "going down 70",
|
||||
ExpectedEnabled: true,
|
||||
ColorBackground: true,
|
||||
ExpectedColor: dischargingColor,
|
||||
},
|
||||
{
|
||||
Case: "charging background color",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Charging, Current: 70}},
|
||||
ExpectedString: "charging 70",
|
||||
ExpectedEnabled: true,
|
||||
ColorBackground: true,
|
||||
ExpectedColor: chargingColor,
|
||||
},
|
||||
{
|
||||
Case: "charged background color",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Full, Current: 70}},
|
||||
ExpectedString: "charged 70",
|
||||
ExpectedEnabled: true,
|
||||
ColorBackground: true,
|
||||
ExpectedColor: chargedColor,
|
||||
},
|
||||
{
|
||||
Case: "discharging foreground color",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}},
|
||||
ExpectedString: "going down 70",
|
||||
ExpectedEnabled: true,
|
||||
ExpectedColor: dischargingColor,
|
||||
},
|
||||
{
|
||||
Case: "charging foreground color",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Charging, Current: 70}},
|
||||
ExpectedString: "charging 70",
|
||||
ExpectedEnabled: true,
|
||||
ExpectedColor: chargingColor,
|
||||
},
|
||||
{
|
||||
Case: "charged foreground color",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Full, Current: 70}},
|
||||
ExpectedString: "charged 70",
|
||||
ExpectedEnabled: true,
|
||||
ExpectedColor: chargedColor,
|
||||
},
|
||||
{Case: "battery error", DisplayError: true, Error: errors.New("oh snap"), ExpectedString: "oh snap", ExpectedEnabled: true},
|
||||
{Case: "battery error disabled", Error: errors.New("oh snap")},
|
||||
{Case: "no batteries", DisplayError: true, Error: &noBatteryError{}},
|
||||
{Case: "no batteries without error"},
|
||||
{Case: "display charging disabled: charging", Batteries: []*battery.Battery{{Full: 100, State: battery.Charging}}, DisableCharging: true},
|
||||
{Case: "display charged disabled: charged", Batteries: []*battery.Battery{{Full: 100, State: battery.Full}}, DisableCharged: true},
|
||||
{
|
||||
Case: "display charging disabled/display charged enabled: charging",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Charging}},
|
||||
DisableCharging: true,
|
||||
DisableCharged: false},
|
||||
{
|
||||
Case: "display charged disabled/display charging enabled: charged",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Full}},
|
||||
DisableCharged: true,
|
||||
DisableCharging: false},
|
||||
{
|
||||
Case: "display charging disabled: discharging",
|
||||
Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}},
|
||||
ExpectedString: "going down 70",
|
||||
ExpectedEnabled: true,
|
||||
DisableCharging: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &MockedEnvironment{}
|
||||
props := properties{
|
||||
ChargingIcon: "charging ",
|
||||
ChargedIcon: "charged ",
|
||||
DischargingIcon: "going down ",
|
||||
DischargingColor: dischargingColor,
|
||||
ChargedColor: chargedColor,
|
||||
ChargingColor: chargingColor,
|
||||
ColorBackground: tc.ColorBackground,
|
||||
DisplayError: tc.DisplayError,
|
||||
}
|
||||
// default values
|
||||
if tc.DisableCharging {
|
||||
props[DisplayCharging] = false
|
||||
}
|
||||
if tc.DisableCharged {
|
||||
props[DisplayCharged] = false
|
||||
}
|
||||
env.On("getBatteryInfo").Return(tc.Batteries, tc.Error)
|
||||
env.onTemplate()
|
||||
b := &batt{
|
||||
props: props,
|
||||
env: env,
|
||||
}
|
||||
enabled := b.enabled()
|
||||
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
|
||||
if !enabled {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, tc.ExpectedString, b.string(), tc.Case)
|
||||
if len(tc.ExpectedColor) == 0 {
|
||||
continue
|
||||
}
|
||||
actualColor := b.props.getColor(ForegroundOverride, "")
|
||||
if tc.ColorBackground {
|
||||
actualColor = b.props.getColor(BackgroundOverride, "")
|
||||
}
|
||||
assert.Equal(t, tc.ExpectedColor, actualColor, tc.Case)
|
||||
}
|
||||
}
|
||||
|
||||
// Session
|
||||
|
||||
func TestPropertySessionSegment(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
ExpectedEnabled bool
|
||||
ExpectedString string
|
||||
UserName string
|
||||
Host string
|
||||
DefaultUserName string
|
||||
DefaultUserNameEnv string
|
||||
SSHSession bool
|
||||
SSHClient bool
|
||||
Root bool
|
||||
DisplayUser bool
|
||||
DisplayHost bool
|
||||
DisplayDefault bool
|
||||
HostColor string
|
||||
UserColor string
|
||||
GOOS string
|
||||
HostError bool
|
||||
}{
|
||||
{
|
||||
Case: "user and computer",
|
||||
ExpectedString: "john at company-laptop",
|
||||
Host: "company-laptop",
|
||||
DisplayUser: true,
|
||||
DisplayHost: true,
|
||||
UserName: "john",
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "user and computer with host color",
|
||||
ExpectedString: "john at <yellow>company-laptop</>",
|
||||
Host: "company-laptop",
|
||||
DisplayUser: true,
|
||||
DisplayHost: true,
|
||||
UserName: "john",
|
||||
HostColor: "yellow",
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "user and computer with user color",
|
||||
ExpectedString: "<yellow>john</> at company-laptop",
|
||||
Host: "company-laptop",
|
||||
DisplayUser: true,
|
||||
DisplayHost: true,
|
||||
UserName: "john",
|
||||
UserColor: "yellow",
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "user and computer with both colors",
|
||||
ExpectedString: "<yellow>john</> at <green>company-laptop</>",
|
||||
Host: "company-laptop",
|
||||
DisplayUser: true,
|
||||
DisplayHost: true,
|
||||
UserName: "john",
|
||||
UserColor: "yellow",
|
||||
HostColor: "green",
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "SSH Session",
|
||||
ExpectedString: "ssh john at company-laptop",
|
||||
Host: "company-laptop",
|
||||
DisplayUser: true,
|
||||
DisplayHost: true,
|
||||
UserName: "john",
|
||||
SSHSession: true,
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "SSH Client",
|
||||
ExpectedString: "ssh john at company-laptop",
|
||||
Host: "company-laptop",
|
||||
DisplayUser: true,
|
||||
DisplayHost: true,
|
||||
UserName: "john",
|
||||
SSHClient: true,
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "SSH Client",
|
||||
ExpectedString: "ssh john at company-laptop",
|
||||
Host: "company-laptop",
|
||||
DisplayUser: true,
|
||||
DisplayHost: true,
|
||||
UserName: "john",
|
||||
SSHClient: true,
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "only user name",
|
||||
ExpectedString: "john",
|
||||
Host: "company-laptop",
|
||||
UserName: "john",
|
||||
DisplayUser: true,
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "windows user name",
|
||||
ExpectedString: "john at company-laptop",
|
||||
Host: "company-laptop",
|
||||
UserName: "surface\\john",
|
||||
DisplayHost: true,
|
||||
DisplayUser: true,
|
||||
ExpectedEnabled: true,
|
||||
GOOS: string(Windows),
|
||||
},
|
||||
{
|
||||
Case: "only host name",
|
||||
ExpectedString: "company-laptop",
|
||||
Host: "company-laptop",
|
||||
UserName: "john",
|
||||
DisplayDefault: true,
|
||||
DisplayHost: true,
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
{
|
||||
Case: "display default - hidden",
|
||||
Host: "company-laptop",
|
||||
UserName: "john",
|
||||
DefaultUserName: "john",
|
||||
DisplayDefault: false,
|
||||
DisplayHost: true,
|
||||
DisplayUser: true,
|
||||
ExpectedEnabled: false,
|
||||
},
|
||||
{
|
||||
Case: "display default with env var - hidden",
|
||||
Host: "company-laptop",
|
||||
UserName: "john",
|
||||
DefaultUserNameEnv: "john",
|
||||
DefaultUserName: "jake",
|
||||
DisplayDefault: false,
|
||||
DisplayHost: true,
|
||||
DisplayUser: true,
|
||||
ExpectedEnabled: false,
|
||||
},
|
||||
{
|
||||
Case: "host error",
|
||||
ExpectedString: "john at unknown",
|
||||
Host: "company-laptop",
|
||||
HostError: true,
|
||||
UserName: "john",
|
||||
DisplayHost: true,
|
||||
DisplayUser: true,
|
||||
ExpectedEnabled: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(MockedEnvironment)
|
||||
env.On("getCurrentUser").Return(tc.UserName)
|
||||
env.On("getRuntimeGOOS").Return(tc.GOOS)
|
||||
if tc.HostError {
|
||||
env.On("getHostName").Return(tc.Host, errors.New("oh snap"))
|
||||
} else {
|
||||
env.On("getHostName").Return(tc.Host, nil)
|
||||
}
|
||||
var SSHSession string
|
||||
if tc.SSHSession {
|
||||
SSHSession = "zezzion"
|
||||
}
|
||||
var SSHClient string
|
||||
if tc.SSHClient {
|
||||
SSHClient = "clientz"
|
||||
}
|
||||
env.On("getenv", "SSH_CONNECTION").Return(SSHSession)
|
||||
env.On("getenv", "SSH_CLIENT").Return(SSHClient)
|
||||
env.On("getenv", "SSH_CLIENT").Return(SSHSession)
|
||||
env.On("getenv", defaultUserEnvVar).Return(tc.DefaultUserNameEnv)
|
||||
env.On("isRunningAsRoot").Return(tc.Root)
|
||||
props := properties{
|
||||
UserInfoSeparator: " at ",
|
||||
SSHIcon: "ssh ",
|
||||
DefaultUserName: tc.DefaultUserName,
|
||||
DisplayDefault: tc.DisplayDefault,
|
||||
DisplayUser: tc.DisplayUser,
|
||||
DisplayHost: tc.DisplayHost,
|
||||
HostColor: tc.HostColor,
|
||||
UserColor: tc.UserColor,
|
||||
}
|
||||
session := &session{
|
||||
env: env,
|
||||
props: props,
|
||||
}
|
||||
assert.Equal(t, tc.ExpectedEnabled, session.enabled(), tc.Case)
|
||||
if tc.ExpectedEnabled {
|
||||
assert.Equal(t, tc.ExpectedString, session.string(), tc.Case)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Language
|
||||
|
||||
func TestLanguageVersionMismatch(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
Enabled bool
|
||||
Mismatch bool
|
||||
ExpectedColor string
|
||||
ColorBackground bool
|
||||
}{
|
||||
{Case: "Mismatch - Foreground color", Enabled: true, Mismatch: true, ExpectedColor: "#566777"},
|
||||
{Case: "Mismatch - Background color", Enabled: true, Mismatch: true, ExpectedColor: "#566777", ColorBackground: true},
|
||||
{Case: "Disabled", Enabled: false},
|
||||
{Case: "No mismatch", Enabled: true, Mismatch: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
props := properties{
|
||||
EnableVersionMismatch: tc.Enabled,
|
||||
VersionMismatchColor: tc.ExpectedColor,
|
||||
ColorBackground: tc.ColorBackground,
|
||||
}
|
||||
var matchesVersionFile func() bool
|
||||
switch tc.Mismatch {
|
||||
case true:
|
||||
matchesVersionFile = func() bool {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
matchesVersionFile = func() bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
args := &languageArgs{
|
||||
commands: []*cmd{
|
||||
{
|
||||
executable: "unicorn",
|
||||
args: []string{"--version"},
|
||||
regex: "(?P<version>.*)",
|
||||
},
|
||||
},
|
||||
extensions: []string{uni, corn},
|
||||
enabledExtensions: []string{uni, corn},
|
||||
enabledCommands: []string{"unicorn"},
|
||||
version: universion,
|
||||
properties: props,
|
||||
matchesVersionFile: matchesVersionFile,
|
||||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled(), tc.Case)
|
||||
assert.Equal(t, universion, lang.string(), tc.Case)
|
||||
if tc.ColorBackground {
|
||||
assert.Equal(t, tc.ExpectedColor, lang.props.getColor(BackgroundOverride, ""), tc.Case)
|
||||
return
|
||||
}
|
||||
assert.Equal(t, tc.ExpectedColor, lang.props.getColor(ForegroundOverride, ""), tc.Case)
|
||||
}
|
||||
}
|
||||
|
||||
// Python
|
||||
|
||||
func TestPythonVirtualEnv(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
Expected string
|
||||
ExpectedDisabled bool
|
||||
VirtualEnvName string
|
||||
CondaEnvName string
|
||||
CondaDefaultEnvName string
|
||||
PyEnvName string
|
||||
FetchVersion bool
|
||||
DisplayDefault bool
|
||||
}{
|
||||
{Case: "VENV", Expected: "VENV", VirtualEnvName: "VENV"},
|
||||
{Case: "CONDA", Expected: "CONDA", CondaEnvName: "CONDA"},
|
||||
{Case: "CONDA default", Expected: "CONDA", CondaDefaultEnvName: "CONDA"},
|
||||
{Case: "Display Base", Expected: "base", CondaDefaultEnvName: "base", DisplayDefault: true},
|
||||
{Case: "Hide base", Expected: "", CondaDefaultEnvName: "base", ExpectedDisabled: true},
|
||||
{Case: "PYENV", Expected: "PYENV", PyEnvName: "PYENV"},
|
||||
{Case: "PYENV Version", Expected: "PYENV 3.8.4", PyEnvName: "PYENV", FetchVersion: true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(MockedEnvironment)
|
||||
env.On("hasCommand", "python").Return(true)
|
||||
env.On("runCommand", "python", []string{"--version"}).Return("Python 3.8.4", nil)
|
||||
env.On("hasFiles", "*.py").Return(true)
|
||||
env.On("getenv", "VIRTUAL_ENV").Return(tc.VirtualEnvName)
|
||||
env.On("getenv", "CONDA_ENV_PATH").Return(tc.CondaEnvName)
|
||||
env.On("getenv", "CONDA_DEFAULT_ENV").Return(tc.CondaDefaultEnvName)
|
||||
env.On("getenv", "PYENV_VERSION").Return(tc.PyEnvName)
|
||||
env.On("getPathSeperator").Return("")
|
||||
env.On("pwd").Return("/usr/home/project")
|
||||
env.On("homeDir").Return("/usr/home")
|
||||
env.onTemplate()
|
||||
props := properties{
|
||||
FetchVersion: tc.FetchVersion,
|
||||
DisplayVirtualEnv: true,
|
||||
DisplayDefault: tc.DisplayDefault,
|
||||
}
|
||||
python := &python{}
|
||||
python.init(props, env)
|
||||
assert.Equal(t, !tc.ExpectedDisabled, python.enabled(), tc.Case)
|
||||
assert.Equal(t, tc.Expected, python.string(), tc.Case)
|
||||
}
|
||||
}
|
||||
|
||||
// Environment Variable
|
||||
|
||||
func TestEnvvarAvailable(t *testing.T) {
|
||||
name := "HERP"
|
||||
expected := "derp"
|
||||
env := new(MockedEnvironment)
|
||||
env.On("getenv", name).Return(expected)
|
||||
e := &envvar{
|
||||
env: env,
|
||||
props: properties{
|
||||
VarName: name,
|
||||
},
|
||||
}
|
||||
assert.True(t, e.enabled())
|
||||
assert.Equal(t, expected, e.string())
|
||||
}
|
||||
|
||||
func TestEnvvarNotAvailable(t *testing.T) {
|
||||
name := "HERP"
|
||||
expected := ""
|
||||
env := new(MockedEnvironment)
|
||||
env.On("getenv", name).Return(expected)
|
||||
e := &envvar{
|
||||
env: env,
|
||||
props: properties{
|
||||
VarName: name,
|
||||
},
|
||||
}
|
||||
assert.False(t, e.enabled())
|
||||
}
|
|
@ -7,14 +7,8 @@ type dotnet struct {
|
|||
}
|
||||
|
||||
func (d *dotnet) string() string {
|
||||
segmentTemplate := d.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) != 0 {
|
||||
return d.language.renderTemplate(segmentTemplate, d)
|
||||
}
|
||||
if d.Unsupported {
|
||||
return d.language.props.getString(UnsupportedDotnetVersionIcon, "\uf071 ")
|
||||
}
|
||||
return d.language.string()
|
||||
segmentTemplate := d.language.props.getString(SegmentTemplate, "{{ if .Unsupported }}\uf071{{ else }}{{ .Full }}{{ end }}")
|
||||
return d.language.string(segmentTemplate, d)
|
||||
}
|
||||
|
||||
func (d *dotnet) init(props Properties, env Environment) {
|
||||
|
|
|
@ -31,7 +31,6 @@ func bootStrapDotnetTest(args *dotnetArgs) *dotnet {
|
|||
env.onTemplate()
|
||||
props := properties{
|
||||
FetchVersion: args.displayVersion,
|
||||
UnsupportedDotnetVersionIcon: args.unsupportedIcon,
|
||||
}
|
||||
dotnet := &dotnet{}
|
||||
dotnet.init(props, env)
|
||||
|
@ -78,5 +77,5 @@ func TestDotnetVersionUnsupported(t *testing.T) {
|
|||
}
|
||||
dotnet := bootStrapDotnetTest(args)
|
||||
assert.True(t, dotnet.enabled())
|
||||
assert.Equal(t, expected, dotnet.string())
|
||||
assert.Equal(t, "\uf071", dotnet.string())
|
||||
}
|
||||
|
|
|
@ -18,10 +18,7 @@ func (e *exit) enabled() bool {
|
|||
}
|
||||
|
||||
func (e *exit) string() string {
|
||||
segmentTemplate := e.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return e.deprecatedString()
|
||||
}
|
||||
segmentTemplate := e.props.getString(SegmentTemplate, "{{ .Text }}")
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: e,
|
||||
|
|
|
@ -108,26 +108,23 @@ func (g *git) enabled() bool {
|
|||
if !g.shouldDisplay() {
|
||||
return false
|
||||
}
|
||||
statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false)
|
||||
displayStatus := g.props.getOneOfBool(FetchStatus, DisplayStatus, false)
|
||||
if !displayStatus {
|
||||
g.setPrettyHEADName()
|
||||
}
|
||||
if displayStatus || statusColorsEnabled {
|
||||
displayStatus := g.props.getBool(FetchStatus, false)
|
||||
if displayStatus {
|
||||
g.setGitStatus()
|
||||
g.setGitHEADContext()
|
||||
g.setBranchStatus()
|
||||
} else {
|
||||
g.setPrettyHEADName()
|
||||
g.Working = &GitStatus{}
|
||||
g.Staging = &GitStatus{}
|
||||
}
|
||||
if g.Upstream != "" && g.props.getOneOfBool(FetchUpstreamIcon, DisplayUpstreamIcon, false) {
|
||||
if g.Upstream != "" && g.props.getBool(FetchUpstreamIcon, false) {
|
||||
g.UpstreamIcon = g.getUpstreamIcon()
|
||||
}
|
||||
if g.props.getOneOfBool(FetchStashCount, DisplayStashCount, false) {
|
||||
if g.props.getBool(FetchStashCount, false) {
|
||||
g.StashCount = g.getStashContext()
|
||||
}
|
||||
if g.props.getOneOfBool(FetchWorktreeCount, DisplayWorktreeCount, false) {
|
||||
if g.props.getBool(FetchWorktreeCount, false) {
|
||||
g.WorktreeCount = g.getWorktreeContext()
|
||||
}
|
||||
return true
|
||||
|
@ -191,13 +188,8 @@ func (g *git) shouldDisplay() bool {
|
|||
}
|
||||
|
||||
func (g *git) string() string {
|
||||
// use template if available
|
||||
segmentTemplate := g.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) > 0 {
|
||||
segmentTemplate := g.props.getString(SegmentTemplate, "{{ .HEAD }} {{ .BranchStatus }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}") // nolint: lll
|
||||
return g.templateString(segmentTemplate)
|
||||
}
|
||||
// legacy render string if no template
|
||||
return g.deprecatedString(g.props.getBool(StatusColorsEnabled, false))
|
||||
}
|
||||
|
||||
func (g *git) templateString(segmentTemplate string) string {
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
changesColor = "#BD8BDE"
|
||||
branchName = "main"
|
||||
)
|
||||
|
||||
|
|
|
@ -13,11 +13,8 @@ const (
|
|||
)
|
||||
|
||||
func (g *golang) string() string {
|
||||
segmentTemplate := g.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return g.language.string()
|
||||
}
|
||||
return g.language.renderTemplate(segmentTemplate, g)
|
||||
segmentTemplate := g.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return g.language.string(segmentTemplate, g)
|
||||
}
|
||||
|
||||
func (g *golang) init(props Properties, env Environment) {
|
||||
|
|
|
@ -7,11 +7,8 @@ type java struct {
|
|||
}
|
||||
|
||||
func (j *java) string() string {
|
||||
segmentTemplate := j.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return j.language.string()
|
||||
}
|
||||
return j.language.renderTemplate(segmentTemplate, j)
|
||||
segmentTemplate := j.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return j.language.string(segmentTemplate, j)
|
||||
}
|
||||
|
||||
func (j *java) init(props Properties, env Environment) {
|
||||
|
|
|
@ -5,11 +5,8 @@ type julia struct {
|
|||
}
|
||||
|
||||
func (j *julia) string() string {
|
||||
segmentTemplate := j.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return j.language.string()
|
||||
}
|
||||
return j.language.renderTemplate(segmentTemplate, j)
|
||||
segmentTemplate := j.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return j.language.string(segmentTemplate, j)
|
||||
}
|
||||
|
||||
func (j *julia) init(props Properties, env Environment) {
|
||||
|
|
|
@ -83,7 +83,7 @@ const (
|
|||
LanguageExtensions Property = "extensions"
|
||||
)
|
||||
|
||||
func (l *language) renderTemplate(segmentTemplate string, context SegmentWriter) string {
|
||||
func (l *language) string(segmentTemplate string, context SegmentWriter) string {
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: context,
|
||||
|
@ -125,7 +125,7 @@ func (l *language) enabled() bool {
|
|||
enabled = l.hasLanguageFiles() || l.inLanguageContext()
|
||||
}
|
||||
}
|
||||
if !enabled || !l.props.getOneOfBool(FetchVersion, DisplayVersion, true) {
|
||||
if !enabled || !l.props.getBool(FetchVersion, true) {
|
||||
return enabled
|
||||
}
|
||||
err := l.setVersion()
|
||||
|
@ -197,17 +197,6 @@ func (l *language) inLanguageContext() bool {
|
|||
return l.inContext()
|
||||
}
|
||||
|
||||
func (l *language) setVersionFileMismatch() {
|
||||
if l.matchesVersionFile == nil {
|
||||
return
|
||||
}
|
||||
l.Mismatch = !l.matchesVersionFile()
|
||||
if !l.Mismatch {
|
||||
return
|
||||
}
|
||||
l.colorMismatch()
|
||||
}
|
||||
|
||||
func (l *language) buildVersionURL() {
|
||||
versionURLTemplate := l.props.getString(VersionURLTemplate, l.versionURLTemplate)
|
||||
if len(versionURLTemplate) == 0 {
|
||||
|
|
|
@ -78,7 +78,7 @@ func TestLanguageFilesFoundButNoCommandAndVersionAndDisplayVersion(t *testing.T)
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, "", lang.string(), "unicorn is not available")
|
||||
assert.Equal(t, "", lang.Error, "unicorn is not available")
|
||||
}
|
||||
|
||||
func TestLanguageFilesFoundButNoCommandAndVersionAndDontDisplayVersion(t *testing.T) {
|
||||
|
@ -147,7 +147,7 @@ func TestLanguageEnabledOneExtensionFound(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, universion, lang.string(), "unicorn is available and uni files are found")
|
||||
assert.Equal(t, universion, lang.Full, "unicorn is available and uni files are found")
|
||||
}
|
||||
|
||||
func TestLanguageDisabledInHome(t *testing.T) {
|
||||
|
@ -185,7 +185,7 @@ func TestLanguageEnabledSecondExtensionFound(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, universion, lang.string(), "unicorn is available and corn files are found")
|
||||
assert.Equal(t, universion, lang.Full, "unicorn is available and corn files are found")
|
||||
}
|
||||
|
||||
func TestLanguageEnabledSecondCommand(t *testing.T) {
|
||||
|
@ -209,7 +209,7 @@ func TestLanguageEnabledSecondCommand(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, universion, lang.string(), "unicorn is available and corn files are found")
|
||||
assert.Equal(t, universion, lang.Full, "unicorn is available and corn files are found")
|
||||
}
|
||||
|
||||
func TestLanguageEnabledAllExtensionsFound(t *testing.T) {
|
||||
|
@ -228,7 +228,7 @@ func TestLanguageEnabledAllExtensionsFound(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, universion, lang.string(), "unicorn is available and uni and corn files are found")
|
||||
assert.Equal(t, universion, lang.Full, "unicorn is available and uni and corn files are found")
|
||||
}
|
||||
|
||||
func TestLanguageEnabledNoVersion(t *testing.T) {
|
||||
|
@ -251,7 +251,7 @@ func TestLanguageEnabledNoVersion(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, "", lang.string(), "unicorn is available and uni and corn files are found")
|
||||
assert.Equal(t, "", lang.Full, "unicorn is available and uni and corn files are found")
|
||||
}
|
||||
|
||||
func TestLanguageEnabledMissingCommand(t *testing.T) {
|
||||
|
@ -268,7 +268,7 @@ func TestLanguageEnabledMissingCommand(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, "", lang.string(), "unicorn is available and uni and corn files are found")
|
||||
assert.Equal(t, "", lang.Full, "unicorn is unavailable and uni and corn files are found")
|
||||
}
|
||||
|
||||
func TestLanguageEnabledNoVersionData(t *testing.T) {
|
||||
|
@ -291,7 +291,7 @@ func TestLanguageEnabledNoVersionData(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, "", lang.string())
|
||||
assert.Equal(t, "", lang.Full)
|
||||
}
|
||||
|
||||
func TestLanguageEnabledMissingCommandCustomText(t *testing.T) {
|
||||
|
@ -309,7 +309,7 @@ func TestLanguageEnabledMissingCommandCustomText(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, expected, lang.string(), "unicorn is available and uni and corn files are found")
|
||||
assert.Equal(t, expected, lang.Error, "unicorn is available and uni and corn files are found")
|
||||
}
|
||||
|
||||
func TestLanguageEnabledMissingCommandCustomTextHideError(t *testing.T) {
|
||||
|
@ -327,7 +327,7 @@ func TestLanguageEnabledMissingCommandCustomTextHideError(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, "", lang.string())
|
||||
assert.Equal(t, "", lang.Full)
|
||||
}
|
||||
|
||||
func TestLanguageEnabledCommandExitCode(t *testing.T) {
|
||||
|
@ -348,7 +348,7 @@ func TestLanguageEnabledCommandExitCode(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, "err executing uni with [--version]", lang.string())
|
||||
assert.Equal(t, "err executing uni with [--version]", lang.Error)
|
||||
assert.Equal(t, expected, lang.exitCode)
|
||||
}
|
||||
|
||||
|
@ -401,7 +401,7 @@ func TestLanguageHyperlinkEnabledWrongRegex(t *testing.T) {
|
|||
}
|
||||
lang := bootStrapLanguageTest(args)
|
||||
assert.True(t, lang.enabled())
|
||||
assert.Equal(t, "err parsing info from corn with 1.3.307", lang.string())
|
||||
assert.Equal(t, "err parsing info from corn with 1.3.307", lang.Error)
|
||||
}
|
||||
|
||||
func TestLanguageEnabledInHome(t *testing.T) {
|
||||
|
|
|
@ -18,12 +18,8 @@ const (
|
|||
)
|
||||
|
||||
func (n *node) string() string {
|
||||
segmentTemplate := n.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
version := n.language.string()
|
||||
return fmt.Sprintf("%s%s", version, n.PackageManagerIcon)
|
||||
}
|
||||
return n.language.renderTemplate(segmentTemplate, n)
|
||||
segmentTemplate := n.language.props.getString(SegmentTemplate, "{{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }}")
|
||||
return n.language.string(segmentTemplate, n)
|
||||
}
|
||||
|
||||
func (n *node) init(props Properties, env Environment) {
|
||||
|
@ -49,7 +45,7 @@ func (n *node) enabled() bool {
|
|||
}
|
||||
|
||||
func (n *node) loadContext() {
|
||||
if !n.language.props.getOneOfBool(FetchPackageManager, DisplayPackageManager, false) {
|
||||
if !n.language.props.getBool(FetchPackageManager, false) {
|
||||
return
|
||||
}
|
||||
if n.language.env.hasFiles("yarn.lock") {
|
||||
|
|
|
@ -70,7 +70,7 @@ func TestNodeInContext(t *testing.T) {
|
|||
props: properties{
|
||||
YarnIcon: "yarn",
|
||||
NPMIcon: "npm",
|
||||
DisplayPackageManager: tc.PkgMgrEnabled,
|
||||
FetchPackageManager: tc.PkgMgrEnabled,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -5,11 +5,8 @@ type php struct {
|
|||
}
|
||||
|
||||
func (p *php) string() string {
|
||||
segmentTemplate := p.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return p.language.string()
|
||||
}
|
||||
return p.language.renderTemplate(segmentTemplate, p)
|
||||
segmentTemplate := p.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return p.language.string(segmentTemplate, p)
|
||||
}
|
||||
|
||||
func (p *php) init(props Properties, env Environment) {
|
||||
|
|
|
@ -54,7 +54,7 @@ func (p *plastic) enabled() bool {
|
|||
return false
|
||||
}
|
||||
p.plasticWorkspaceFolder = wkdir.parentFolder
|
||||
displayStatus := p.props.getOneOfBool(FetchStatus, DisplayStatus, false)
|
||||
displayStatus := p.props.getBool(FetchStatus, false)
|
||||
p.setSelector()
|
||||
if displayStatus {
|
||||
p.setPlasticStatus()
|
||||
|
|
|
@ -12,11 +12,8 @@ const (
|
|||
)
|
||||
|
||||
func (p *python) string() string {
|
||||
segmentTemplate := p.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return p.legacyString()
|
||||
}
|
||||
return p.language.renderTemplate(segmentTemplate, p)
|
||||
segmentTemplate := p.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return p.language.string(segmentTemplate, p)
|
||||
}
|
||||
|
||||
func (p *python) init(props Properties, env Environment) {
|
||||
|
@ -49,7 +46,7 @@ func (p *python) enabled() bool {
|
|||
}
|
||||
|
||||
func (p *python) loadContext() {
|
||||
if !p.language.props.getOneOfBool(DisplayVirtualEnv, FetchVirtualEnv, true) {
|
||||
if !p.language.props.getBool(FetchVirtualEnv, true) {
|
||||
return
|
||||
}
|
||||
venvVars := []string{
|
||||
|
|
|
@ -5,16 +5,8 @@ type ruby struct {
|
|||
}
|
||||
|
||||
func (r *ruby) string() string {
|
||||
segmentTemplate := r.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
version := r.language.string()
|
||||
// asdf default non-set version
|
||||
if version == "______" {
|
||||
return ""
|
||||
}
|
||||
return version
|
||||
}
|
||||
return r.language.renderTemplate(segmentTemplate, r)
|
||||
segmentTemplate := r.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return r.language.string(segmentTemplate, r)
|
||||
}
|
||||
|
||||
func (r *ruby) init(props Properties, env Environment) {
|
||||
|
@ -53,5 +45,10 @@ func (r *ruby) init(props Properties, env Environment) {
|
|||
}
|
||||
|
||||
func (r *ruby) enabled() bool {
|
||||
return r.language.enabled()
|
||||
enabled := r.language.enabled()
|
||||
// this happens when no version is set
|
||||
if r.Full == "______" {
|
||||
r.Full = ""
|
||||
}
|
||||
return enabled
|
||||
}
|
||||
|
|
|
@ -5,11 +5,8 @@ type rust struct {
|
|||
}
|
||||
|
||||
func (r *rust) string() string {
|
||||
segmentTemplate := r.language.props.getString(SegmentTemplate, "")
|
||||
if len(segmentTemplate) == 0 {
|
||||
return r.language.string()
|
||||
}
|
||||
return r.language.renderTemplate(segmentTemplate, r)
|
||||
segmentTemplate := r.language.props.getString(SegmentTemplate, "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}")
|
||||
return r.language.string(segmentTemplate, r)
|
||||
}
|
||||
|
||||
func (r *rust) init(props Properties, env Environment) {
|
||||
|
|
|
@ -1,14 +1,10 @@
|
|||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
type session struct {
|
||||
props Properties
|
||||
env Environment
|
||||
// text string
|
||||
|
||||
userName string
|
||||
hostName string
|
||||
SSHSession bool
|
||||
|
||||
// Deprecated
|
||||
|
@ -17,18 +13,11 @@ type session struct {
|
|||
|
||||
func (s *session) enabled() bool {
|
||||
s.SSHSession = s.activeSSHSession()
|
||||
segmentTemplate := s.props.getString(SegmentTemplate, "")
|
||||
if segmentTemplate == "" {
|
||||
return s.legacyEnabled()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *session) string() string {
|
||||
segmentTemplate := s.props.getString(SegmentTemplate, "")
|
||||
if segmentTemplate == "" {
|
||||
return s.legacyString()
|
||||
}
|
||||
segmentTemplate := s.props.getString(SegmentTemplate, "{{ .UserName}}@{{ .HostName }}")
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: s,
|
||||
|
@ -46,23 +35,6 @@ func (s *session) init(props Properties, env Environment) {
|
|||
s.env = env
|
||||
}
|
||||
|
||||
func (s *session) getUserName() string {
|
||||
user := s.env.getCurrentUser()
|
||||
username := strings.TrimSpace(user)
|
||||
if s.env.getRuntimeGOOS() == "windows" && strings.Contains(username, "\\") {
|
||||
username = strings.Split(username, "\\")[1]
|
||||
}
|
||||
return username
|
||||
}
|
||||
|
||||
func (s *session) getComputerName() string {
|
||||
computername, err := s.env.getHostName()
|
||||
if err != nil {
|
||||
computername = "unknown"
|
||||
}
|
||||
return strings.TrimSpace(computername)
|
||||
}
|
||||
|
||||
func (s *session) activeSSHSession() bool {
|
||||
keys := []string{
|
||||
"SSH_CONNECTION",
|
||||
|
|
|
@ -57,7 +57,7 @@ func TestSessionSegmentTemplate(t *testing.T) {
|
|||
},
|
||||
{
|
||||
Case: "no template",
|
||||
ExpectedString: "\uf817 john@remote",
|
||||
ExpectedString: "",
|
||||
UserName: "john",
|
||||
SSHSession: true,
|
||||
ComputerName: "remote",
|
||||
|
@ -96,14 +96,13 @@ func TestSessionSegmentTemplate(t *testing.T) {
|
|||
}
|
||||
env.On("getenv", "SSH_CONNECTION").Return(SSHSession)
|
||||
env.On("getenv", "SSH_CLIENT").Return(SSHSession)
|
||||
env.On("getenv", defaultUserEnvVar).Return(tc.DefaultUserName)
|
||||
env.On("templateCache").Return(&templateCache{
|
||||
UserName: tc.UserName,
|
||||
HostName: tc.ComputerName,
|
||||
Env: map[string]string{
|
||||
"SSH_CONNECTION": SSHSession,
|
||||
"SSH_CLIENT": SSHSession,
|
||||
defaultUserEnvVar: tc.DefaultUserName,
|
||||
"POSH_SESSION_DEFAULT_USER": tc.DefaultUserName,
|
||||
},
|
||||
Root: tc.Root,
|
||||
})
|
||||
|
|
|
@ -16,9 +16,24 @@ func TestSysInfo(t *testing.T) {
|
|||
Precision int
|
||||
Template string
|
||||
}{
|
||||
{Case: "physical mem", ExpectedString: "50", SysInfo: sysinfo{PhysicalPercentUsed: 50}},
|
||||
{Case: "physical mem 2 digits", ExpectedString: "60.51", SysInfo: sysinfo{Precision: 2, PhysicalPercentUsed: 60.51}},
|
||||
{Case: "physical meme rounded", ExpectedString: "61", SysInfo: sysinfo{Precision: 0, PhysicalPercentUsed: 61}},
|
||||
{
|
||||
Case: "physical mem",
|
||||
ExpectedString: "50",
|
||||
SysInfo: sysinfo{PhysicalPercentUsed: 50},
|
||||
Template: "{{ round .PhysicalPercentUsed .Precision }}",
|
||||
},
|
||||
{
|
||||
Case: "physical mem 2 digits",
|
||||
ExpectedString: "60.51",
|
||||
SysInfo: sysinfo{Precision: 2, PhysicalPercentUsed: 60.51},
|
||||
Template: "{{ round .PhysicalPercentUsed .Precision }}",
|
||||
},
|
||||
{
|
||||
Case: "physical meme rounded",
|
||||
ExpectedString: "61",
|
||||
SysInfo: sysinfo{Precision: 0, PhysicalPercentUsed: 61},
|
||||
Template: "{{ round .PhysicalPercentUsed .Precision }}",
|
||||
},
|
||||
{
|
||||
Case: "load",
|
||||
ExpectedString: "0.22 0.12 0",
|
||||
|
@ -39,12 +54,11 @@ func TestSysInfo(t *testing.T) {
|
|||
tc.SysInfo.env = env
|
||||
tc.SysInfo.props = properties{
|
||||
Precision: tc.Precision,
|
||||
SegmentTemplate: tc.Template,
|
||||
}
|
||||
if tc.Template != "" {
|
||||
tc.SysInfo.props.set(SegmentTemplate, tc.Template)
|
||||
}
|
||||
enabled := tc.SysInfo.enabled()
|
||||
if tc.ExpectDisabled {
|
||||
assert.Equal(t, false, tc.SysInfo.enabled(), tc.Case)
|
||||
assert.Equal(t, false, enabled, tc.Case)
|
||||
} else {
|
||||
assert.Equal(t, tc.ExpectedString, tc.SysInfo.string(), tc.Case)
|
||||
}
|
||||
|
|
|
@ -13,9 +13,9 @@ const (
|
|||
)
|
||||
|
||||
func (t *text) enabled() bool {
|
||||
textProperty := t.props.getOneOfString(TextProperty, SegmentTemplate, "!!text property not defined!!")
|
||||
segmentTemplate := t.props.getString(SegmentTemplate, "{{ .Text }}")
|
||||
template := &textTemplate{
|
||||
Template: textProperty,
|
||||
Template: segmentTemplate,
|
||||
Context: t,
|
||||
Env: t.env,
|
||||
}
|
||||
|
@ -27,17 +27,7 @@ func (t *text) enabled() bool {
|
|||
}
|
||||
|
||||
func (t *text) string() string {
|
||||
segmentTemplate := t.props.getString(SegmentTemplate, "{{.Text}}")
|
||||
template := &textTemplate{
|
||||
Template: segmentTemplate,
|
||||
Context: t,
|
||||
Env: t.env,
|
||||
}
|
||||
text, err := template.render()
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return text
|
||||
return t.Text
|
||||
}
|
||||
|
||||
func (t *text) init(props Properties, env Environment) {
|
||||
|
|
|
@ -39,7 +39,7 @@ func TestTextSegment(t *testing.T) {
|
|||
txt := &text{
|
||||
env: env,
|
||||
props: properties{
|
||||
TextProperty: tc.Text,
|
||||
SegmentTemplate: tc.Text,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, tc.ExpectedDisabled, !txt.enabled(), tc.Case)
|
||||
|
|
|
@ -124,7 +124,7 @@
|
|||
"prefix": " \uE235 ",
|
||||
"display_mode": "files",
|
||||
"fetch_virtual_env": false,
|
||||
"template": "{{ .Full }}"
|
||||
"template": "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -93,7 +93,7 @@
|
|||
"prefix": " \uE235 ",
|
||||
"display_mode": "files",
|
||||
"fetch_virtual_env": false,
|
||||
"template": "{{ .Full }}"
|
||||
"template": "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -94,7 +94,7 @@
|
|||
"prefix": " \uE235 ",
|
||||
"display_mode": "files",
|
||||
"fetch_virtual_env": false,
|
||||
"template": "{{ .Full }}"
|
||||
"template": "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -97,7 +97,7 @@
|
|||
"postfix": " \uE235 ",
|
||||
"display_mode": "files",
|
||||
"fetch_virtual_env": false,
|
||||
"template": "{{ .Full }}"
|
||||
"template": "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -387,12 +387,6 @@
|
|||
"properties": {
|
||||
"properties": {
|
||||
"properties": {
|
||||
"unsupported_version_icon": {
|
||||
"type": "string",
|
||||
"title": "Unsupported Version Icon",
|
||||
"description": "Text/icon that is displayed when the active .NET SDK version (e.g., one specified by global.json) is not installed/supported",
|
||||
"default": " \uE77F "
|
||||
},
|
||||
"fetch_version": {
|
||||
"$ref": "#/definitions/fetch_version"
|
||||
},
|
||||
|
|
|
@ -80,7 +80,7 @@
|
|||
"fetch_virtual_env": false,
|
||||
"postfix": "",
|
||||
"display_mode": "context",
|
||||
"template": "{{ .Full }}"
|
||||
"template": "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue