~fmac/corylus

1558e2016ceb11a8bdb5daf07657629fbf798178 — Francesco Camuffo 5 months ago 692061b
Add cwd.go
2 files changed, 38 insertions(+), 31 deletions(-)

A cwd.go
M misc.go
A cwd.go => cwd.go +38 -0
@@ 0,0 1,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
}

M misc.go => misc.go +0 -31
@@ 10,37 10,6 @@ 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 {