~fmac/corylus

ref: e83d345cf7540c90f3bee891ea2e4171d9abfbee corylus/misc.go -rw-r--r-- 756 bytes
e83d345cFrancesco Camuffo Show two chars in CWD if first is '.' 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main

import (
	"os"
	"os/user"
	"strings"
)

func segHostname() (string, error) {
	return os.Hostname()
}

// CWD with contracted path
func segCwd() (string, error) {
	wd, err := os.Getwd()
	if err != nil {
		return "", err
	}
	h := os.Getenv("HOME")

	if strings.HasPrefix(wd, h) {
		wd = "~" + wd[len(h):]
	}
	ss := strings.Split(wd, "/")
	wd = ""
	for i, s := range ss {
		if i == len(ss) - 1 {
			wd += s
		} else {
			if len(s) == 0 {
				wd += "/"
				continue
			}
			if s[0] == '.' {
				wd += s[:2] + "/"
			} else {
				wd += string(s[0]) + "/"
			}
		}
	}
	return wd, nil
}

func segTail() (string, error) {
	u, err := user.Current()
	if err != nil {
		return "", err
	}
	if u.Uid == "0" {
		return "\n# ", nil
	}
	return "\n$ ", nil
}