/
format_tab_title.lua
120 lines (103 loc) · 3.37 KB
/
format_tab_title.lua
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
local wezterm = require("wezterm")
local module = {}
-- https://wezfurlong.org/wezterm/config/lua/window-events/format-tab-title.html
-- https://wezfurlong.org/wezterm/config/lua/PaneInformation.html
-- this one produces short hashes and they always seem to be the same shade of blue or red
local GHhash = function(str)
-- https://gist.github.com/scheler/26a942d34fb5576a68c111b05ac3fabe
-- also try https://github.com/lancelijade/qqwry.lua/blob/master/crc32.lua
-- https://wezfurlong.org/wezterm/config/files.html#making-your-own-lua-modules
local h = 5381
for c in str:gmatch(".") do
h = ((h << 5) + h) + string.byte(c)
end
return h
end
-- the following hash functions ported from https://www.partow.net/programming/hashfunctions/
-- this works pretty well, but nvim is now a dark somewhat hard to read purple
-- my favorite so far
local RSHash = function(str)
local b = 378551
local a = 63689
local hash = 0
for c in str:gmatch(".") do
hash = hash * a + string.byte(c)
a = a * b
end
return hash
end
-- this makes 'less` and 'nvim' look a very similar (army green)
local JSHash = function(str)
local hash = 1315423911
for c in str:gmatch(".") do
hash = hash ~ ((hash << 5) + string.byte(c) + (hash >> 2))
end
return hash
end
-- this is my favorite so far. zsh, nvim, and less look good
local ELFHash = function(str)
local hash = 0
local x = 0
for c in str:gmatch(".") do
hash = (hash << 4) + string.byte(c)
x = hash & 0xF0000000
if x ~= 0 then
hash = hash ~ (x >> 24)
end
hash = hash & ~x
end
return hash
end
-- let's assign a hash function here
local hash = ELFHash
local hash_to_color = function(hash_int)
local hex = string.format("%x", hash_int)
hex = hex .. hex -- make it long enough
local truncated = string.sub(hex, 1, 6)
-- https://wezfurlong.org/wezterm/config/lua/wezterm.color/parse.html
local color = wezterm.color.parse("#" .. truncated)
color = color:saturate(0.2)
color = color:lighten(0.2)
return color
end
-- Equivalent to POSIX basename(3)
-- Given "/foo/bar" returns "bar"
-- Given "c:\\foo\\bar" returns "bar"
local basename = function(s)
return string.gsub(s, "(.*[/\\])(.*)", "%2")
end
-- https://wezfurlong.org/wezterm/config/lua/window-events/format-tab-title.html
function module.format_tab_title(tab, tabs, panes, config, hover, max_width)
-- https://wezfurlong.org/wezterm/config/lua/pane/index.html
local pane = tab.active_pane
local process = basename(pane.foreground_process_name)
local cwd = pane.current_working_dir
-- cwd is nil if I pull up the Debug Overlay for example
if cwd == nil then
cwd = "<unknown>"
-- assume process is nil as well
process = "<unknown>"
else
-- cwd = string.gsub(cwd.file_path, wezterm.home_dir, '~')
cwd = basename(cwd.file_path)
end
local title = " " .. process .. " " .. cwd .. " "
-- TODO: use the whole args? https://wezfurlong.org/wezterm/config/lua/LocalProcessInfo.html
local color = hash_to_color(hash(process))
-- NOTE: my underline and intensity don't seem to be working...
local underline = "None"
local intensity = "Normal"
if tab.is_active then
underline = "Double"
intensity = "Half"
end
-- https://wezfurlong.org/wezterm/config/lua/wezterm/format.html
return {
{ Attribute = { Intensity = intensity } },
{ Attribute = { Underline = underline } },
{ Background = { Color = color } },
{ Foreground = { Color = "black" } },
{ Text = title },
}
end
return module