~fmac/corylus

ref: 0a6019d236e40f5e872b340b4862175c1e542a74 corylus/cwd.go -rw-r--r-- 725 bytes
0a6019d2 — Francesco Camuffo Add seg cwd-count 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
package main

import (
	"fmt"
	"os"
	"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
}

func segCwdCount() (string, error) {
	wd, err := os.Getwd()
	if err != nil {
		return "", err
	}
	ff, err := os.ReadDir(wd)
	if err != nil {
		return "", err
	}
	return fmt.Sprint(len(ff)), nil
}