Skip to content

Commit

Permalink
Merge pull request #77 from autarch/autarch/alias-fix
Browse files Browse the repository at this point in the history
Fix path alias feature to always sort paths by reverse length
  • Loading branch information
justjanne authored Apr 12, 2018
2 parents 9368f54 + a31f670 commit 593a9f1
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions segment-cwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"sort"
"strings"
)

Expand All @@ -15,15 +16,33 @@ type pathSegment struct {
alias bool
}

type byRevLength []string

func (s byRevLength) Len() int {
return len(s)
}
func (s byRevLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byRevLength) Less(i, j int) bool {
return len(s[i]) > len(s[j])
}

func maybeAliasPathSegments(p *powerline, pathSegments []pathSegment) []pathSegment {
if p.pathAliases == nil {
return pathSegments
}

keys := make([]string, len(p.pathAliases))
for k := range p.pathAliases {
keys = append(keys, k)
}
sort.Sort(byRevLength(keys))

Aliases:
for p, alias := range p.pathAliases {
for _, k := range keys {
// This turns a string like "foo/bar/baz" into an array of strings.
path := strings.Split(p, "/")
path := strings.Split(k, "/")

// If the path has 3 elements, we know we should look at pathSegments
// in 3-element chunks.
Expand All @@ -34,6 +53,8 @@ Aliases:
continue Aliases
}

alias := p.pathAliases[k]

Segments:
// We want to see if that array of strings exists in pathSegments.
for i, _ := range pathSegments {
Expand Down

0 comments on commit 593a9f1

Please sign in to comment.