feat(path): allow coloring the folder_separator_icon on cycle

This commit is contained in:
Jan De Dobbeleer 2023-04-25 20:35:27 +02:00 committed by Jan De Dobbeleer
parent 5ca932ae11
commit 66ca5ccf1f
4 changed files with 78 additions and 33 deletions

View file

@ -73,8 +73,10 @@ const (
MaxWidth properties.Property = "max_width"
// Hides the root location if it doesn't fit in max_depth. Used in Agnoster Short
HideRootLocation properties.Property = "hide_root_location"
// A foreground color cycle
// A color override cycle
Cycle properties.Property = "cycle"
// Color the path separators within the cycle
CycleFolderSeparator properties.Property = "cycle_folder_separator"
)
func (pt *Path) Template() string {
@ -576,28 +578,44 @@ func (pt *Path) replaceFolderSeparators(pwd string) string {
func (pt *Path) colorizePath(root string, elements []string) string {
cycle := pt.props.GetStringArray(Cycle, []string{})
skipColorize := len(cycle) == 0
folderSeparator := pt.getFolderSeparator()
colorSeparator := pt.props.GetBool(CycleFolderSeparator, false)
if len(cycle) != 0 {
colorize := "<%s>%s</>"
if len(root) != 0 {
root = fmt.Sprintf(colorize, cycle[0], root)
cycle = append(cycle[1:], cycle[0])
colorizeElement := func(element string) string {
if skipColorize || len(element) == 0 {
return element
}
for i, element := range elements {
if len(element) == 0 {
continue
}
colored := fmt.Sprintf(colorize, cycle[0], element)
elements[i] = colored
defer func() {
cycle = append(cycle[1:], cycle[0])
}()
return fmt.Sprintf("<%s>%s</>", cycle[0], element)
}
colorizeSeparator := func() string {
if skipColorize || !colorSeparator {
return folderSeparator
}
return fmt.Sprintf("<%s>%s</>", cycle[0], folderSeparator)
}
var builder strings.Builder
builder.WriteString(colorizeElement(root))
if root != pt.env.PathSeparator() && len(root) != 0 {
builder.WriteString(colorizeSeparator())
}
for i, element := range elements {
if len(element) == 0 {
continue
}
builder.WriteString(colorizeElement(element))
if i != len(elements)-1 {
builder.WriteString(colorizeSeparator())
}
}
if root == pt.env.PathSeparator() || len(root) == 0 {
return root + strings.Join(elements, folderSeparator)
}
return root + folderSeparator + strings.Join(elements, folderSeparator)
return builder.String()
}

View file

@ -950,13 +950,14 @@ func TestFolderPathCustomMappedLocations(t *testing.T) {
func TestAgnosterPath(t *testing.T) {
cases := []struct {
Case string
Expected string
Home string
PWD string
GOOS string
PathSeparator string
Cycle []string
Case string
Expected string
Home string
PWD string
GOOS string
PathSeparator string
Cycle []string
ColorSeparator bool
}{
{
Case: "Windows registry drive case sensitive",
@ -1082,6 +1083,15 @@ func TestAgnosterPath(t *testing.T) {
PathSeparator: "/",
Cycle: []string{"blue", "yellow"},
},
{
Case: "Unix, colorize with folder separator",
Expected: "<blue>mnt</><yellow> > </><yellow>f</><blue> > </><blue>location</>",
Home: homeDir,
PWD: "/mnt/folder/location",
PathSeparator: "/",
Cycle: []string{"blue", "yellow"},
ColorSeparator: true,
},
}
for _, tc := range cases {
@ -1098,11 +1108,12 @@ func TestAgnosterPath(t *testing.T) {
path := &Path{
env: env,
props: properties.Map{
properties.Style: Agnoster,
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
Cycle: tc.Cycle,
properties.Style: Agnoster,
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
Cycle: tc.Cycle,
CycleFolderSeparator: tc.ColorSeparator,
},
}
path.setPaths()

View file

@ -1774,6 +1774,19 @@
"title": "Hide the root location",
"description": "Hides the root location, when using agnoster_short style, if it doesn't fit in the last max_depth folders.",
"default": false
},
"cycle": {
"type": "array",
"title": "Color overrides to use to cycle through and color the path per folder",
"items": {
"type": "string"
}
},
"cycle_folder_separator": {
"type": "boolean",
"title": "Cycle the folder_separator_icon",
"description": "Colorize the folder_separator_icon as well when using a cycle.",
"default": false
}
}
}
@ -2771,7 +2784,9 @@
{
"if": {
"properties": {
"type": { "const": "unity" }
"type": {
"const": "unity"
}
}
},
"then": {

View file

@ -34,7 +34,7 @@ import Config from "@site/src/components/Config.js";
| --------------------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `folder_separator_icon` | `string` | the symbol to use as a separator between folders - defaults to platform path separator |
| `folder_separator_template` | `string` | the [template][templates] to use as a separator between folders - defaults to `` |
| `home_icon` | `string` | the icon to display when at `$HOME`, defaults to `~` |
| `home_icon` | `string` | the icon to display when at `$HOME` - defaults to `~` |
| `folder_icon` | `string` | the icon to use as a folder indication - defaults to `..` |
| `windows_registry_icon` | `string` | the icon to display when in the Windows registry - defaults to `\uE0B1` |
| `style` | `enum` | how to display the current path |
@ -42,7 +42,8 @@ import Config from "@site/src/components/Config.js";
| `max_depth` | `number` | maximum path depth to display before shortening when using `agnoster_short`, defaults to `1` |
| `max_width` | `number` | maximum path length to display when using `powerlevel`, defaults to `0` |
| `hide_root_location` | `boolean` | hides the root location if it doesn't fit in the last `max_depth` folders, when using `agnoster_short` - defaults to `false` |
| `cycle` | `[]string` | a list of foreground colors to cycle through to colorize the individual path folders |
| `cycle` | `[]string` | a list of color overrides to cycle through to colorize the individual path folders, e.g. `[ "#ffffff,#111111" ]` |
| `cycle_folder_separator` | `boolean` | colorize the `folder_separator_icon` as well when using a cycle - defaults to `false` |
## Mapped Locations