-
Notifications
You must be signed in to change notification settings - Fork 16
/
fnord.go
59 lines (51 loc) · 1.38 KB
/
fnord.go
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
package main
import (
"path/filepath"
"strings"
"github.com/xyproto/env/v2"
"github.com/xyproto/termtitle"
)
// FilenameOrData represents either a filename, or data read in from stdin
type FilenameOrData struct {
filename string
data []byte
length uint64
}
// ExpandUser will expand the filename if it starts with "~"
// fnord is short for "filename or data"
func (fnord *FilenameOrData) ExpandUser() {
// If the filename starts with "~", then expand it
if strings.HasPrefix(fnord.filename, "~") {
fnord.filename = env.ExpandUser(fnord.filename)
}
}
// Empty checks if data has been loaded
func (fnord *FilenameOrData) Empty() bool {
return fnord.length == 0
}
// String returns the contents as a string
func (fnord *FilenameOrData) String() string {
return string(fnord.data)
}
// SetTitle sets an approperiate terminal emulator title, unless NO_COLOR is set
func (fnord *FilenameOrData) SetTitle() {
if envNoColor {
return
}
title := "?"
if fnord.filename == "-" {
title = "stdin"
} else if fnord.filename != "" {
title = fnord.filename
}
termtitle.Set(termtitle.GenerateTitle(title))
}
// NoTitle will remove the filename title by setting the shell name as the title,
// if NO_COLOR is not set and the terminal emulator supports it.
func NoTitle() {
if envNoColor {
return
}
shellName := filepath.Base(env.Str("SHELL", "/bin/sh"))
termtitle.Set(shellName)
}