Debian Codegraph @

lua/lualine/components/tabs/tab.lua

raw
1 local Tab = require('lualine.utils.class'):extend()
2
3 local modules = require('lualine_require').lazy_require {
4 highlight = 'lualine.highlight',
5 utils = 'lualine.utils.utils',
6 }
7
8 ---initialize a new tab from opts
9 ---@param opts table
10 function Tab:init(opts)
11 assert(opts.tabnr, 'Cannot create Tab without tabnr')
12 self.tabnr = opts.tabnr
13 self.tabId = opts.tabId
14 self.options = opts.options
15 self.highlights = opts.highlights
16 self.modified_icon = ''
17 self:get_props()
18 end
19
20 function Tab:get_props()
21 local buflist = vim.fn.tabpagebuflist(self.tabnr)
22 local winnr = vim.fn.tabpagewinnr(self.tabnr)
23 local bufnr = buflist[winnr]
24 self.file = modules.utils.stl_escape(vim.api.nvim_buf_get_name(bufnr))
25 self.filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
26 self.buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype')
27
28 if self.options.show_modified_status then
29 for _, b in ipairs(buflist) do
30 if vim.api.nvim_buf_get_option(b, 'modified') then
31 self.modified_icon = self.options.symbols.modified or ''
32 break
33 end
34 end
35 end
36 end
37
38 ---returns name for tab. Tabs name is the name of buffer in last active window
39 --- of the tab.
40 ---@return string
41 function Tab:label()
42 local ok, custom_tabname = pcall(vim.api.nvim_tabpage_get_var, self.tabId, 'tabname')
43 if not ok then
44 custom_tabname = nil
45 end
46 if custom_tabname and custom_tabname ~= '' then
47 return modules.utils.stl_escape(custom_tabname)
48 end
49 if self.filetype == 'fugitive' then
50 return 'fugitive: ' .. vim.fn.fnamemodify(self.file, ':h:h:t')
51 elseif self.buftype == 'help' then
52 return 'help:' .. vim.fn.fnamemodify(self.file, ':t:r')
53 elseif self.buftype == 'terminal' then
54 local match = string.match(vim.split(self.file, ' ')[1], 'term:.*:(%a+)')
55 return match ~= nil and match or vim.fn.fnamemodify(vim.env.SHELL, ':t')
56 elseif self.file == '' then
57 return '[No Name]'
58 end
59 if self.options.path == 1 then
60 return vim.fn.fnamemodify(self.file, ':~:.')
61 elseif self.options.path == 2 then
62 return vim.fn.fnamemodify(self.file, ':p')
63 elseif self.options.path == 3 then
64 return vim.fn.fnamemodify(self.file, ':p:~')
65 else
66 return vim.fn.fnamemodify(self.file, ':t')
67 end
68 end
69
70 ---shortens path by turning apple/orange -> a/orange
71 ---@param path string
72 ---@param sep string path separator
73 ---@param max_len integer maximum length of the full filename string
74 ---@return string
75 local function shorten_path(path, sep, max_len)
76 local len = #path
77 if len <= max_len then
78 return path
79 end
80
81 local segments = vim.split(path, sep)
82 for idx = 1, #segments - 1 do
83 if len <= max_len then
84 break
85 end
86
87 local segment = segments[idx]
88 local shortened = segment:sub(1, vim.startswith(segment, '.') and 2 or 1)
89 segments[idx] = shortened
90 len = len - (#segment - #shortened)
91 end
92
93 return table.concat(segments, sep)
94 end
95
96 ---returns rendered tab
97 ---@return string
98 function Tab:render()
99 local name = self:label()
100 if self.options.tab_max_length ~= 0 then
101 local path_separator = package.config:sub(1, 1)
102 name = shorten_path(name, path_separator, self.options.tab_max_length)
103 end
104 if self.options.fmt then
105 name = self.options.fmt(name or '', self)
106 end
107 if self.ellipse then -- show ellipsis
108 name = '...'
109 else
110 -- different formats for different modes
111 if self.options.mode == 0 then
112 name = tostring(self.tabnr)
113 if self.modified_icon ~= '' then
114 name = string.format('%s%s', name, self.modified_icon)
115 end
116 elseif self.options.mode == 1 then
117 if self.modified_icon ~= '' then
118 name = string.format('%s %s', self.modified_icon, name)
119 end
120 else
121 name = string.format('%s%s %s', tostring(self.tabnr), self.modified_icon, name)
122 end
123 end
124
125 name = Tab.apply_padding(name, self.options.padding)
126 self.len = vim.fn.strchars(name)
127
128 -- setup for mouse clicks
129 local line = string.format('%%%s@LualineSwitchTab@%s%%T', self.tabnr, name)
130 -- apply highlight
131 line = modules.highlight.component_format_highlight(self.highlights[(self.current and 'active' or 'inactive')])
132 .. line
133
134 -- apply separators
135 if self.options.self.section < 'x' and not self.first then
136 local sep_before = self:separator_before()
137 line = sep_before .. line
138 self.len = self.len + vim.fn.strchars(sep_before)
139 elseif self.options.self.section >= 'x' and not self.last then
140 local sep_after = self:separator_after()
141 line = line .. sep_after
142 self.len = self.len + vim.fn.strchars(sep_after)
143 end
144 return line
145 end
146
147 ---apply separator before current tab
148 ---@return string
149 function Tab:separator_before()
150 if self.current or self.aftercurrent then
151 return '%Z{' .. self.options.section_separators.left .. '}'
152 else
153 return self.options.component_separators.left
154 end
155 end
156
157 ---apply separator after current tab
158 ---@return string
159 function Tab:separator_after()
160 if self.current or self.beforecurrent then
161 return '%z{' .. self.options.section_separators.right .. '}'
162 else
163 return self.options.component_separators.right
164 end
165 end
166
167 ---adds spaces to left and right
168 function Tab.apply_padding(str, padding)
169 local l_padding, r_padding = 1, 1
170 if type(padding) == 'number' then
171 l_padding, r_padding = padding, padding
172 elseif type(padding) == 'table' then
173 l_padding, r_padding = padding.left or 0, padding.right or 0
174 end
175 return string.rep(' ', l_padding) .. str .. string.rep(' ', r_padding)
176 end
177
178 return Tab