-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.lua
150 lines (137 loc) · 4.26 KB
/
util.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
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
local wezterm = require("wezterm")
local theme = require("theme")
local act = wezterm.action
local module = {}
local function base_path_name(str) return string.gsub(str, "(.*[/\\])(.*)", "%2") end
function module.file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
function module.file_lines(file)
if not module.file_exists(file) then return {} end
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
function module.format_tab_title(tab, tabs, panes, config, hover, max_width)
local label = {
{ Foreground = { Color = theme.colors.hl_1 } },
{ Text = " " .. tab.tab_index + 1 },
{ Foreground = { Color = theme.colors.hl_2 } },
{ Text = ": " },
}
local pane = tab.active_pane
if pane.is_zoomed then module.tconcat(label, {
{ Text = "🔍 " },
}) end
local title = tab.active_pane.title
if tab.tab_title ~= "" then title = tab.tab_title end
if tab.is_active then
module.tconcat(label, {
{ Foreground = { Color = theme.colors.active_fg } },
{ Text = title .. " " },
})
else
module.tconcat(label, {
{ Foreground = { Color = theme.colors.inactive_fg } },
{ Text = title .. " " },
})
end
module.tconcat(label, {
-- { Background = { Color=C_BG } },
{ Background = { Color = "none" } },
{ Foreground = { Color = theme.colors.hl_1 } },
{ Text = "|" },
})
return label
end
function module.launch(args)
return wezterm.action_callback(function(window, _)
local _, _, _ = window:mux_window():spawn_tab({
args = { os.getenv("SHELL"), "-c", module.titled_cmd(args) },
})
end)
end
--- Launches new split pane but with spawn command.
--- Yes, this seems backward, but Wezterm refers to the direction splitting
--- rather than the layout of the newly launched pane.
function module.launch_split(args)
return act.SplitVertical({
args = { os.getenv("SHELL"), "-c", table.unpack(args) },
domain = "CurrentPaneDomain",
})
end
--- Launches new vertical but with spawn command.
--- Yes, this seems backward, but Wezterm refers to the direction splitting
--- rather than the layout of the newly launched pane.
function module.launch_vertical(args)
return act.SplitHorizontal({
args = { os.getenv("SHELL"), "-c", table.unpack(args) },
domain = "CurrentPaneDomain",
})
end
function module.tconcat(t1, t2)
for _, v in ipairs(t2) do
table.insert(t1, v)
end
end
function module.titled_cmd(args)
local title = args[1]
return string.format("wezterm cli set-tab-title %s && %s", title, table.concat(args, " "))
end
function module.update_right_status(window, pane)
local text = {}
module.tconcat(text, {
{ Attribute = { Italic = true } },
{ Foreground = { Color = theme.colors.hl_1 } },
{ Text = " " },
{ Foreground = { Color = theme.colors.hl_2 } },
{ Text = base_path_name(window:active_workspace()) },
})
if window:active_key_table() then
module.tconcat(text, {
{ Foreground = { Color = theme.colors.hl_1 } },
{ Text = " | ⌨ " },
{ Foreground = { Color = theme.colors.hl_2 } },
{ Text = window:active_key_table() },
})
end
local tab = pane:tab()
if tab == nil then return end
for _, p in ipairs(tab:panes_with_info()) do
-- wezterm.log_info("zoomed: " .. tostring(p.is_zoomed))
if p.is_zoomed then module.tconcat(text, {
{ Text = " 🔍" },
}) end
end
module.tconcat(text, {
{ Text = " " },
})
window:set_right_status(wezterm.format(text))
end
function module.user_var_changed(window, pane, name, value)
local overrides = window:get_config_overrides() or {}
if name == "ZEN_MODE" then
local incremental = value:find("+")
local number_value = tonumber(value)
if incremental ~= nil then
while number_value > 0 do
window:perform_action(wezterm.action.IncreaseFontSize, pane)
number_value = number_value - 1
end
overrides.enable_tab_bar = false
elseif number_value < 0 then
window:perform_action(wezterm.action.ResetFontSize, pane)
overrides.font_size = nil
overrides.enable_tab_bar = true
else
overrides.font_size = number_value
overrides.enable_tab_bar = false
end
end
window:set_config_overrides(overrides)
end
return module