fn.go (2651B)
1 package main 2 3 import ( 4 "os" 5 "os/user" 6 "fmt" 7 "strings" 8 9 "hhvn.uk/prompt/tput" 10 ) 11 12 type Fn func(ch chan string) 13 14 func FnExitStatus(ch chan string) { 15 defer close(ch) 16 17 status := os.Args[1:] 18 if len(status) == 0 { 19 panic("where are my exit codes?? *grumble*") 20 } 21 22 if cnf.Smallpipe { 23 allzero := true 24 for _, v := range status { 25 if v != "0" { 26 allzero = false 27 break 28 } 29 } 30 31 if allzero { 32 status = status[:1] 33 } 34 } 35 36 for i, v := range status { 37 if v == "0" { 38 ch <- tput.Fg(col.Success) 39 } else { 40 ch <- tput.Fg(col.Fail) 41 } 42 43 ch <- v 44 45 if i < len(status) - 1 { 46 ch <- tput.Fg(col.Pipe) 47 ch <- "|" 48 } 49 } 50 } 51 52 func FnUsername(ch chan string) { 53 defer close(ch) 54 55 ch <- tput.Fg(col.Username) 56 57 user, err := user.Current() 58 if err != nil { 59 Err(err) 60 return 61 } 62 63 usr := user.Username 64 65 if cnf.Smalluser { 66 ch <- Smalltxt(usr) 67 } else { 68 ch <- usr 69 } 70 } 71 72 // Git branch/HEAD 73 func FnBranch(ch chan string) { 74 defer close(ch) 75 76 ch <- tput.Fg(col.Branch) 77 78 brch, err := GitHEAD() 79 if err != nil { 80 Err(Ignore(err, GitErrIgns...)) 81 return 82 } 83 84 ch <- brch 85 } 86 87 func FnDiff(ch chan string) { 88 defer close(ch) 89 90 m, a, d, s, err := GitDiff() 91 if err != nil { 92 Err(Ignore(err, GitErrIgns...)) 93 return 94 } 95 96 total := m + a + d + s 97 98 if total == 0 { 99 return 100 } 101 102 if cnf.Smalldiff { 103 if m + a + d == 0 && s > 0 { 104 ch <- tput.Fg(col.Staged) 105 } else { 106 ch <- tput.Fg(col.Unstaged) 107 } 108 109 ch <- fmt.Sprintf("%d", total) 110 111 if m > 0 { ch <- "m" } 112 if a > 0 { ch <- "+" } 113 if d > 0 { ch <- "-" } 114 if s > 0 { ch <- "s" } 115 } else { 116 fn := func(i int, s string) { 117 if i == 0 { 118 return 119 } 120 121 ch <- fmt.Sprintf("%d%s", i, s) 122 } 123 124 if m + a + d > 0 { 125 ch <- tput.Fg(col.Unstaged) 126 } 127 128 fn(m, "m") 129 fn(a, "+") 130 fn(d, "-") 131 132 if s > 0 { 133 ch <- tput.Fg(col.Staged) 134 } 135 136 fn(s, "s") 137 } 138 } 139 140 func FnUnpushed(ch chan string) { 141 defer close(ch) 142 143 n, err := GitUnpushed() 144 if err != nil { 145 Err(Ignore(err, GitErrIgns...)) 146 return 147 } 148 149 if n == 0 { 150 return 151 } 152 153 ch <- tput.Fg(col.Unpushed) 154 ch <- fmt.Sprintf("%d^", n) 155 } 156 157 func FnDir(ch chan string) { 158 defer close(ch) 159 160 ch <- tput.Fg(col.Dir) 161 162 wd, err := os.Getwd() 163 if err != nil { 164 Err(err) 165 return 166 } 167 168 // TODO: hilight the dir that the repo belongs to? 169 // _, err = gitPrefix() 170 // if err != nil { 171 // Err(err) 172 // return 173 // } 174 175 home := os.Getenv("HOME") 176 177 if strings.HasPrefix(wd, home) { 178 bwd := []byte(wd) 179 bh := []byte(home) 180 l := len(bh) - 1 181 182 bwd[l] = '~' 183 ch <- string(bwd[l:]) 184 } else { 185 ch <- wd 186 } 187 } 188 189 func FnDollar(ch chan string) { 190 defer close(ch) 191 192 ch <- tput.Fg(col.Dollar) 193 194 if os.Getuid() == 0 { 195 ch <- "#" 196 } else { 197 ch <- "$" 198 } 199 }