-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
184 lines (159 loc) · 4.42 KB
/
main.go
File metadata and controls
184 lines (159 loc) · 4.42 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"embed"
"fmt"
"hash/fnv"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/alecthomas/kong"
"github.com/lucasb-eyer/go-colorful"
"github.com/spf13/afero"
)
var appFS = afero.NewOsFs()
var cli struct {
Run struct{} `cmd default:"1"`
Hook struct {
Shell string `arg`
} `cmd help:"Install shell hook. Supported shells are fish and zsh"`
Sense struct {
Text string `arg help:"Custom text to sense a color from."`
} `cmd help:"Sense a color from custom text."`
BackgroundTint bool `default:"true" negatable:"" help:"Enable or disable background tinting for Ghostty and other terminals."`
}
func main() {
ktx := kong.Parse(&cli,
kong.Name("synesthesia"),
kong.Description("Change iTerm2 tab colour based on go module name or custom text."),
)
switch ktx.Command() {
case "hook <shell>":
echoHook(cli.Hook.Shell, cli.BackgroundTint)
case "sense <text>":
setTerminalColors(getColor(cli.Sense.Text), cli.BackgroundTint)
default:
synesthetize(cli.BackgroundTint)
}
}
func synesthetize(enableTint bool) {
cwd, _ := os.Getwd()
modname := readModule(findProjectRoot(cwd))
if modname == "" {
fmt.Print("\033]6;1;bg;*;default\a")
if enableTint {
fmt.Print("\033]111\a")
}
return
}
setTerminalColors(getColor(modname), enableTint)
}
func findProjectRoot(startDir string) string {
next := startDir
for {
current := next
isWorkspace := IsWorkspace(current)
// Current behaviour
if HasGoMod(current) && !isWorkspace {
return filepath.Join(current, "go.mod")
}
// Distinguish between workspace copies of the project
if isWorkspace {
return current
}
// Fallback if not a go project, but still a version controlled project
if IsRepositoryRoot(current) {
return current
}
parent := filepath.Dir(current)
if parent == current {
break
}
next = parent
}
return ""
}
func IsWorkspace(path string) bool {
git, _ := appFS.Stat(filepath.Join(path, ".git"))
if git != nil && !git.IsDir() {
return true
}
jj, _ := appFS.Stat(filepath.Join(path, ".jj"))
if jj != nil && jj.IsDir() {
// Standalone JJ has no local .git directory
gitDir, _ := appFS.Stat(filepath.Join(path, ".git"))
return gitDir == nil || !gitDir.IsDir()
}
return false
}
func IsRepositoryRoot(path string) bool {
git, _ := appFS.Stat(filepath.Join(path, ".git"))
if git != nil && git.IsDir() {
return true
}
jj, _ := appFS.Stat(filepath.Join(path, ".jj"))
return jj != nil && jj.IsDir()
}
func HasGoMod(path string) bool {
_, err := appFS.Stat(filepath.Join(path, "go.mod"))
return err == nil
}
func readModule(fpath string) string {
if fpath == "" {
return ""
}
// If it's a directory (found .jj or .git dir), return the directory name
if info, err := appFS.Stat(fpath); err == nil && info.IsDir() {
return filepath.Base(fpath)
}
bytes, err := afero.ReadFile(appFS, fpath)
if err != nil {
return ""
}
// Try to parse as go.mod
re := regexp.MustCompile(`(?m)^module\s(?P<modulename>.+)`)
result := re.FindStringSubmatch(string(bytes))
if len(result) > 0 {
return strings.TrimSpace(result[re.SubexpIndex("modulename")])
}
// Fallback to the name of the containing directory (e.g. for .git worktrees or extensionless go.mod)
return filepath.Base(filepath.Dir(fpath))
}
func getColor(name string) colorful.Color {
h := fnv.New64()
h.Write([]byte(name))
r := rand.New(rand.NewSource(int64(h.Sum64())))
return colorful.FastHappyColorWithRand(r)
}
func setTerminalColors(c colorful.Color, enableTint bool) {
r, g, b := c.RGB255()
// iTerm2: Tab color (Full intensity)
fmt.Printf("\033]6;1;bg;red;brightness;%d\a", r)
fmt.Printf("\033]6;1;bg;green;brightness;%d\a", g)
fmt.Printf("\033]6;1;bg;blue;brightness;%d\a", b)
if enableTint {
// Ghostty & others: Background color fallback (Lightly tinted)
// We blend the vibrant color with a dark background (#121212).
// 0.9 means 90% background, 10% our vibrant color.
bg, _ := colorful.Hex("#121212")
tinted := c.BlendLab(bg, 0.9)
fmt.Printf("\033]11;%s\a", tinted.Hex())
}
}
//go:embed hooks
var hooks embed.FS
func echoHook(shell string, enableTint bool) {
switch s := strings.ToLower(shell); s {
case "fish", "zsh":
bytes, _ := hooks.ReadFile(fmt.Sprintf("hooks/hook.%s", s))
script := string(bytes)
if !enableTint {
script = strings.ReplaceAll(script, "--background-tint", "--no-background-tint")
}
fmt.Print(script)
default:
fmt.Fprintf(os.Stderr, "Unsupported shell: %s\n", shell)
os.Exit(1)
}
}