Skip to content

Instantly share code, notes, and snippets.

@javipolo
Created May 16, 2022 19:43
Show Gist options
  • Save javipolo/28830acdbbe7f2f3c8cd332d7a0e73d4 to your computer and use it in GitHub Desktop.
Save javipolo/28830acdbbe7f2f3c8cd332d7a0e73d4 to your computer and use it in GitHub Desktop.
short_prompt_pwd similar to fish
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