Repository navigation
Expand file tree
/
Copy pathtext.go
More file actions
72 lines (65 loc) · 1.62 KB
/
Copy pathtext.go
File metadata and controls
72 lines (65 loc) · 1.62 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
package git
import (
"fmt"
"strings"
)
// combinedOutput joins normal output and error output into one message.
func combinedOutput(stdout string, stderr string) string {
stdout = strings.TrimSpace(stdout)
stderr = strings.TrimSpace(stderr)
switch {
case stdout == "":
return stderr
case stderr == "":
return stdout
default:
return stdout + "\n" + stderr
}
}
// joinOutput joins non-empty command output parts with newlines.
func joinOutput(outputs ...string) string {
var parts []string
for _, output := range outputs {
output = strings.TrimSpace(output)
if output != "" {
parts = append(parts, output)
}
}
return strings.Join(parts, "\n")
}
// nonEmptyLines returns trimmed lines and drops blank separators.
func nonEmptyLines(value string) []string {
lines := strings.Split(value, "\n")
result := make([]string, 0, len(lines))
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
result = append(result, line)
}
}
return result
}
// contains checks small string slices used for branch and remote lists.
func contains(values []string, target string) bool {
for _, value := range values {
if value == target {
return true
}
}
return false
}
// numbered formats file content with a header and stable line numbers.
func numbered(path string, content string) string {
lines := strings.Split(content, "\n")
width := len(fmt.Sprintf("%d", len(lines)))
var b strings.Builder
b.WriteString(path)
b.WriteString("\n\n")
for i, line := range lines {
if i == len(lines)-1 && line == "" {
continue
}
fmt.Fprintf(&b, "%*d %s\n", width, i+1, line)
}
return b.String()
}