-
Notifications
You must be signed in to change notification settings - Fork 15
/
title.go
43 lines (39 loc) · 1.11 KB
/
title.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
package main
import (
"path/filepath"
"strings"
"github.com/xyproto/env/v2"
"github.com/xyproto/termtitle"
)
// SetTitle sets an approperiate terminal emulator title, unless NO_COLOR is set
func (fnord *FilenameOrData) SetTitle() {
if envNoColor {
return
}
title := "?"
if fnord.stdin {
title = "stdin"
if len(fnord.data) > 512 {
fields := strings.Fields(string(fnord.data[:512]))
firstWord := fields[0]
if len(firstWord) >= 2 && strings.Contains(firstWord, "(") && strings.Contains(firstWord, ")") {
// Probably a man page, create a nicely formatted lowercase title
// "LS(1)" becomes "man ls"
fields = strings.Split(firstWord, "(")
title = "man " + strings.ToLower(fields[0])
}
}
} else if fnord.filename != "" {
title = termtitle.GenerateTitle(fnord.filename)
}
termtitle.Set(sanitizeFilename(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)
}