main
  1local wezterm = require("wezterm")
  2local module = {}
  3
  4module.status_override = nil
  5
  6--- 获取当前是否为暗黑模式
  7--- @return boolean
  8local function is_dark_mode()
  9  if wezterm.gui then
 10    return wezterm.gui.get_appearance():find("Dark")
 11  end
 12  return true
 13end
 14
 15-- 获取标签页标题
 16local function tab_title(tab_info)
 17  local title = tab_info.tab_title
 18  -- 显式设置标签页标题时,返回设置的标题
 19  if title and #title > 0 then
 20    return title
 21  end
 22  return tab_info.active_pane.title
 23end
 24
 25-- 绘制右侧状态栏
 26local function draw_right_status(window)
 27  -- 默认显示主机名
 28  local center_text = wezterm.hostname()
 29  -- Leader 键按下时进行提示
 30  if module.status_override ~= nil then
 31    center_text = module.status_override
 32  elseif window:leader_is_active() then
 33    center_text = "Leader Key"
 34  end
 35
 36  -- 实心左箭头符号
 37  local SOLID_LEFT_ARROW = wezterm.nerdfonts.pl_right_hard_divider
 38
 39  -- 获取当前窗口的配置,并从中获取配色方案
 40  local color_scheme = window:effective_config().resolved_palette
 41  local bg = color_scheme.background
 42  local fg = color_scheme.foreground
 43
 44  -- 获取当前窗口顶栏配色
 45  local frame_scheme = window:effective_config().window_frame
 46  local tbar_inact_bg = frame_scheme.inactive_titlebar_bg
 47  local tbar_act_bg = frame_scheme.active_titlebar_bg
 48  local tbar_bg = ""
 49  if window:is_focused() then
 50    tbar_bg = tbar_act_bg
 51  else
 52    tbar_bg = tbar_inact_bg
 53  end
 54
 55  local right_status = {}
 56
 57  if wezterm.target_triple == "x86_64-pc-windows-msvc" then
 58    right_status = {
 59      { Background = { Color = "none" } },
 60      { Foreground = { Color = bg } },
 61      { Text = SOLID_LEFT_ARROW },
 62      { Background = { Color = bg } },
 63      { Foreground = { Color = fg } },
 64      { Text = " " .. center_text },
 65    }
 66    local gradient_step = 10
 67    local gradient_bgs =
 68      wezterm.color.gradient({ colors = { bg, tbar_bg } }, gradient_step)
 69    for i = 1, #gradient_bgs do
 70      table.insert(right_status, { Background = { Color = gradient_bgs[i] } })
 71      table.insert(right_status, { Foreground = { Color = "none" } })
 72      table.insert(right_status, { Text = " " })
 73    end
 74  else
 75    local tab_bar_scheme = color_scheme.tab_bar
 76    local act_tab_scheme = tab_bar_scheme.active_tab
 77    local tab_bar_bg = tab_bar_scheme.background
 78
 79    right_status = {
 80      { Background = { Color = tab_bar_bg } },
 81      { Foreground = { Color = act_tab_scheme.bg_color } },
 82      { Text = SOLID_LEFT_ARROW },
 83      { Background = { Color = act_tab_scheme.bg_color } },
 84      { Foreground = { Color = act_tab_scheme.fg_color } },
 85      { Text = " " .. center_text .. " " },
 86    }
 87  end
 88
 89  window:set_right_status(wezterm.format(right_status))
 90end
 91
 92local function draw_tab_bar(tab, tabs, panes, config, hover, max_width)
 93  local tab_bar_color_scheme = config.resolved_palette.tab_bar
 94  local bg = tab_bar_color_scheme.inactive_tab.bg_color
 95  local fg = tab_bar_color_scheme.inactive_tab.fg_color
 96  if tab.is_active then
 97    bg = tab_bar_color_scheme.active_tab.bg_color
 98    fg = tab_bar_color_scheme.active_tab.fg_color
 99  end
100  if hover then
101    bg = tab_bar_color_scheme.inactive_tab_hover.bg_color
102    fg = tab_bar_color_scheme.inactive_tab_hover.fg_color
103  end
104
105  local SOLID_RIGHT_ARROW = wezterm.nerdfonts.pl_left_hard_divider
106
107  local tab_left_text = SOLID_RIGHT_ARROW
108  local tab_right_text = SOLID_RIGHT_ARROW
109  local tab_aux_text_len = 6
110  if tab.tab_index == 0 then
111    tab_left_text = ""
112    tab_aux_text_len = tab_aux_text_len - 1
113  end
114  if tab.tab_index == #tabs - 1 then
115    tab_right_text = SOLID_RIGHT_ARROW .. " "
116    tab_aux_text_len = tab_aux_text_len + 1
117  end
118  local title = tab_title(tab)
119  if string.len(title) + tab_aux_text_len > max_width then
120    title = string.sub(title, 0, max_width - tab_aux_text_len - 1)
121    title = title .. ""
122  end
123  local format_items = {
124    { Background = { Color = bg } },
125    { Foreground = { Color = tab_bar_color_scheme.background } },
126    { Text = tab_left_text },
127    { Background = { Color = bg } },
128    { Foreground = { Color = fg } },
129    { Text = " " .. tab.tab_index + 1 .. ":" .. title .. " " },
130    { Background = { Color = tab_bar_color_scheme.background } },
131    { Foreground = { Color = bg } },
132    { Text = tab_right_text },
133  }
134  return format_items
135end
136
137function module.apply(config)
138  -- 设置字体
139  config.font = wezterm.font { family = "Maple Mono NF CN" }
140
141  -- 启用滚动条
142  config.enable_scroll_bar = true
143
144  -- 配色方案
145  if is_dark_mode() then
146    config.color_scheme = "Catppuccin Macchiato"
147  else
148    config.color_scheme = "Catppuccin Latte"
149  end
150
151  -- 初始窗口大小
152  config.initial_cols = 128
153  config.initial_rows = 32
154
155  -- 背景透明度
156  --  config.window_background_opacity = 0.4
157  -- config.win32_system_backdrop = 'Acrylic'
158  --  config.macos_window_background_blur = 20
159  --  config.kde_window_background_blur = true
160
161  -- 调整窗口边距
162  config.window_padding = {
163    left = "5pt",
164    right = "5pt",
165    top = "5pt",
166    bottom = "5pt",
167  }
168
169  if wezterm.target_triple == "x86_64-pc-windows-msvc" then
170    -- 改为嵌入式顶栏
171    config.window_decorations = "INTEGRATED_BUTTONS|RESIZE"
172  else
173    config.use_fancy_tab_bar = false
174    config.tab_max_width = 32
175    wezterm.on("format-tab-title", draw_tab_bar)
176  end
177
178  -- 定制顶栏
179  wezterm.on("update-status", draw_right_status)
180
181  config.window_frame = {
182    font = wezterm.font { family = "Noto Sans SC", weight = "Bold" },
183    font_size = 10.0,
184  }
185end
186
187return module