~fmac/corylus

ref: 1558e2016ceb11a8bdb5daf07657629fbf798178 corylus/cwd.go -rw-r--r-- 532 bytes
1558e201 — Francesco Camuffo Add cwd.go 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
package main

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

// 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
}