~fmac/corylus

ref: e83d345cf7540c90f3bee891ea2e4171d9abfbee corylus/main.go -rw-r--r-- 671 bytes
e83d345cFrancesco Camuffo Show two chars in CWD if first is '.' 6 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
package main

import (
	"fmt"
	"os"
	"strconv"
)

func main() {
	segs := []string{}
	for _, arg := range os.Args[1:] {
		segs = append(segs, arg)
	}

	generators := map[string]func() (string, error) {
		"cwd": segCwd,
		"hostname": segHostname,
		"tail": segTail,
		"username": segUsername,
	}

	pr := ""
	for _, seg := range(segs) {
		g := generators[seg]
		if g == nil {
			// if we don't have a generator named `seg` then append as string
			s, _ := strconv.Unquote("\"" + seg + "\"")
			pr += s
		} else {
			str, err := g()
			if err != nil {
				fmt.Fprintf(os.Stderr, "++++++++\n%s\n++++++++\n", err)
			} else {
				pr += str
			}
		}
	}

	fmt.Printf("%s", pr)
}