Created
May 16, 2022 19:43
-
-
Save javipolo/28830acdbbe7f2f3c8cd332d7a0e73d4 to your computer and use it in GitHub Desktop.
short_prompt_pwd similar to fish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
) | |
func getdir(str string) (string, string) { | |
index := strings.Index(str, "/") | |
if index < 0 { | |
return str, "" | |
} | |
if index == len(str) { | |
return str, "" | |
} | |
if index == 0 { | |
return "/", str[index+1:] | |
} | |
return str[0:1] + "/", str[index+1:] | |
} | |
func main() { | |
dirname, err := os.Getwd() | |
home := os.Getenv("HOME") | |
if strings.HasPrefix(dirname, home) { | |
dirname = strings.Replace(dirname, home, "~", 1) | |
} | |
if err != nil { | |
log.Println(err) | |
} | |
cur, rest := getdir(dirname) | |
output := cur | |
for rest != "" { | |
cur, rest = getdir(rest) | |
output = output + cur | |
} | |
fmt.Println(output) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment