Guest User

(beginner) Pane Navigation made easy. wezterm.lua (claude generated) (check reddit)

a guest
May 14th, 2026
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.29 KB | Source Code | 0 0
  1. local wezterm = require 'wezterm'
  2. local act = wezterm.action
  3. local config = wezterm.config_builder()
  4.  
  5. config.enable_scroll_bar = true
  6. -- config.wrap_early = true        -- Active le wrap des lignes longues
  7.  
  8. config.set_environment_variables = {
  9.   TERM = "wezterm",   -- déjà par défaut, mais utile
  10. }
  11.  
  12. wezterm.on('format-tab-title', function(tab)
  13.   local mux_pane = wezterm.mux.get_pane(tab.active_pane.pane_id)
  14.   if mux_pane then
  15.     local cwd_uri = mux_pane:get_current_working_dir()
  16.     if cwd_uri then
  17.       local host, user, path
  18.  
  19.       if type(cwd_uri) == 'string' then
  20.         user, host, path = cwd_uri:match('^file://([^@/]*)@?([^/]*)(/.*)$')
  21.         path = path or ''
  22.       else
  23.         host = cwd_uri.host or ''
  24.         user = cwd_uri.username or ''
  25.         path = cwd_uri.file_path or ''
  26.       end
  27.  
  28.       if path ~= '/' then path = path:gsub('/$', '') end
  29.       host = host or ''
  30.  
  31.       if host ~= '' and host ~= 'localhost' then
  32.         -- SSH : le titre est déjà correctement formaté par le shell distant
  33.         return tab.active_pane.title
  34.       else
  35.         -- Local : ~/path ou /path
  36.         local home = os.getenv('HOME') or ''
  37.         if home ~= '' and path:sub(1, #home) == home then
  38.           path = '~' .. path:sub(#home + 1)
  39.         end
  40.         if path ~= '' then return path end
  41.       end
  42.     end
  43.   end
  44.   return tab.active_pane.title
  45. end)
  46.  
  47. local function pane_title(p)
  48.   local cwd_uri = p:get_current_working_dir()
  49.   if cwd_uri then
  50.     local host, path
  51.     if type(cwd_uri) == 'string' then
  52.       host, path = cwd_uri:match('^file://([^/]*)(/.*)$')
  53.       host = host or ''
  54.       path = path or ''
  55.     else
  56.       host = cwd_uri.host or ''
  57.       path = cwd_uri.file_path or ''
  58.     end
  59.     if path ~= '/' then path = path:gsub('/$', '') end
  60.     if host ~= '' and host ~= 'localhost' then
  61.       return p:get_title()
  62.     else
  63.       local home = os.getenv('HOME') or ''
  64.       if home ~= '' and path:sub(1, #home) == home then
  65.         path = '~' .. path:sub(#home + 1)
  66.       end
  67.       if path ~= '' then return path end
  68.     end
  69.   end
  70.   return p:get_title()
  71. end
  72.  
  73. wezterm.on('format-window-title', function(tab, pane, tabs)
  74.   local prefix = string.format('[%d/%d] ', tab.tab_index + 1, #tabs)
  75.   local mux_pane = wezterm.mux.get_pane(pane.pane_id)
  76.   if mux_pane then
  77.     return prefix .. pane_title(mux_pane)
  78.   end
  79.   return prefix .. pane.title
  80. end)
  81.  
  82. local function build_choices()
  83.   local choices = {}
  84.  
  85.   -- active tab par fenêtre (via GUI)
  86.   local active_tab_by_win = {}
  87.   local active_win_title_by_win = {}
  88.   for _, gui_win in ipairs(wezterm.gui.gui_windows()) do
  89.     local tab = gui_win:active_tab()
  90.     if tab then
  91.       local panes = tab:panes()
  92.       active_tab_by_win[gui_win:window_id()] = tab:tab_id()
  93.       active_win_title_by_win[gui_win:window_id()] = #panes > 0 and pane_title(panes[1]) or ''
  94.     end
  95.   end
  96.  
  97.   -- active pane par tab (via panes_with_info)
  98.   local active_pane_by_tab = {}
  99.   for _, mux_win in ipairs(wezterm.mux.all_windows()) do
  100.     for _, tab in ipairs(mux_win:tabs()) do
  101.       local ok, infos = pcall(function() return tab:panes_with_info() end)
  102.       if ok and infos then
  103.         for _, info in ipairs(infos) do
  104.           if info.is_active then
  105.             active_pane_by_tab[tab:tab_id()] = info.pane:pane_id()
  106.             break
  107.           end
  108.         end
  109.       end
  110.     end
  111.   end
  112.  
  113.   for _, mux_win in ipairs(wezterm.mux.all_windows()) do
  114.     local win_id = mux_win:window_id()
  115.     local tabs = mux_win:tabs()
  116.     local total = #tabs
  117.     local active_tab_id = active_tab_by_win[win_id]
  118.     local active_title = active_win_title_by_win[win_id] or ''
  119.  
  120.     local active_index = 1
  121.     for i, tab in ipairs(tabs) do
  122.       if tab:tab_id() == active_tab_id then
  123.         active_index = i
  124.         break
  125.       end
  126.     end
  127.  
  128.     table.insert(choices, {
  129.       label = string.format('Window #%d: [%d/%d] %s', win_id, active_index, total, active_title),
  130.       id = string.format('w:%d', win_id),
  131.     })
  132.  
  133.     for _, tab in ipairs(mux_win:tabs()) do
  134.       local tab_id = tab:tab_id()
  135.       local is_active_tab = tab_id == active_tab_id
  136.       local tab_marker = is_active_tab and '>' or ' '
  137.  
  138.       table.insert(choices, {
  139.         label = string.format('  %s Tab: %s', tab_marker, #tab:panes() > 0 and pane_title(tab:panes()[1]) or tab:get_title()),
  140.         id = string.format('t:%d:%d', win_id, tab_id),
  141.       })
  142.  
  143.       for _, p in ipairs(tab:panes()) do
  144.         local pane_id = p:pane_id()
  145.         local is_active_pane = pane_id == active_pane_by_tab[tab_id]
  146.         local pane_marker = is_active_pane and '>' or ' '
  147.         local dims = p:get_dimensions()
  148.         local size = dims and string.format('%dx%d', dims.cols or 0, dims.viewport_rows or dims.rows or 0) or '?x?'
  149.  
  150.         table.insert(choices, {
  151.           label = string.format('    %s Pane #%d [%s]: %s', pane_marker, pane_id, size, pane_title(p)),
  152.           id = string.format('p:%d:%d:%d', win_id, tab_id, pane_id),
  153.         })
  154.       end
  155.     end
  156.   end
  157.   return choices
  158. end
  159.  
  160. local function navigate(id)
  161.   if not id then return end
  162.  
  163.   local parts = {}
  164.   for part in id:gmatch('[^:]+') do
  165.     table.insert(parts, part)
  166.   end
  167.  
  168.   local kind = parts[1]
  169.  
  170.   if kind == 'p' then
  171.     local target_pane_id = tonumber(parts[4])
  172.     for _, w in ipairs(wezterm.mux.all_windows()) do
  173.       for _, t in ipairs(w:tabs()) do
  174.         for _, p in ipairs(t:panes()) do
  175.           if p:pane_id() == target_pane_id then
  176.             t:activate()
  177.             p:activate()
  178.             return
  179.           end
  180.         end
  181.       end
  182.     end
  183.  
  184.   elseif kind == 't' then
  185.     local target_tab_id = tonumber(parts[3])
  186.     for _, w in ipairs(wezterm.mux.all_windows()) do
  187.       for _, t in ipairs(w:tabs()) do
  188.         if t:tab_id() == target_tab_id then
  189.           t:activate()
  190.           return
  191.         end
  192.       end
  193.     end
  194.  
  195.   elseif kind == 'w' then
  196.     local target_win_id = tonumber(parts[2])
  197.     for _, w in ipairs(wezterm.mux.all_windows()) do
  198.       if w:window_id() == target_win_id then
  199.         local tabs = w:tabs()
  200.         if #tabs > 0 then
  201.           tabs[1]:activate()
  202.         end
  203.         return
  204.       end
  205.     end
  206.   end
  207. end
  208.  
  209. config.keys = {
  210.   {
  211.     key = 'D',
  212.     mods = 'CTRL|SHIFT',
  213.     action = wezterm.action_callback(function(win, pane)
  214.       pane:move_to_new_window()
  215.     end),
  216.   },
  217.  
  218.   {
  219.     key = '3',
  220.     mods = 'CTRL|SHIFT|ALT',
  221.     action = act.SplitVertical { domain = 'CurrentPaneDomain' },
  222.   },
  223.   {
  224.     key = 'I',
  225.     mods = 'CTRL|SHIFT',
  226.     action = wezterm.action_callback(function(window, pane)
  227.       local win_id   = window:window_id()
  228.       local pane_id  = pane:pane_id()
  229.       local tab      = window:active_tab()
  230.       local tab_name = tab and (#tab:panes() > 0 and pane_title(tab:panes()[1]) or '') or ''
  231.       local nav_title = string.format('PaneNav from Win %d %s Pane %d', win_id, tab_name, pane_id)
  232.  
  233.       local choices = build_choices()
  234.       table.insert(choices, 1, { label = nav_title, id = '' })
  235.  
  236.       window:perform_action(
  237.         act.InputSelector {
  238.           action = wezterm.action_callback(function(_, _, id, _)
  239.             navigate(id)
  240.           end),
  241.           title = nav_title,
  242.           choices = choices,
  243.           fuzzy = true,
  244.         },
  245.         pane
  246.       )
  247.     end),
  248.   },
  249.  
  250.  
  251. }
  252.  
  253. return config
  254.  
Advertisement
Add Comment
Please, Sign In to add comment