-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
79 lines (61 loc) · 1.46 KB
/
Copy pathutils.go
File metadata and controls
79 lines (61 loc) · 1.46 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
)
// formats cursor and output to STDOUT
func printInputIndicator() {
user := fmt.Sprintf("%s%s%s", GREEN, getCurrentUser(), Reset)
hostname := fmt.Sprintf("@%s%s%s", SOFT_RED, getCurrntHostname(), Reset)
folder := fmt.Sprintf("%s%s$%s", CYAN_BLUE, getCurrentFolder(), Reset)
git := fmt.Sprintf("%s%s%s", AMBER, getCurrentBranch(), Reset)
fmt.Fprintf(os.Stdout, "%s%s ~/%s %s > ", user, hostname, folder, git)
}
func getCurrentFolder() string {
dir, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
}
folder := filepath.Base(dir)
if len(folder) > 20 {
return fmt.Sprintf("%s..", folder[:20])
}
return folder
}
func getCurrntHostname() string {
hostname, err := os.Hostname()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
}
return hostname
}
func getCurrentUser() string {
currntUser, err := user.Current()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
}
return currntUser.Username
}
func getCurrentBranch() string {
args := []string{"rev-parse", "--abbrev-ref", "HEAD"}
out, err := exec.Command("git", args...).Output()
if err != nil {
return ""
}
branch := string(out)
branch = strings.TrimSuffix(branch, "\n")
return fmt.Sprintf("(%s)", branch)
}
func isSSHSession() bool {
if os.Getenv("SSH_CLIENT") != "" || os.Getenv("SSH_CONNECTION") != "" || os.Getenv("SSH_TTY") != "" {
return true
}
return false
}