Debian Codegraph @

lua/lualine/components/buffers/buffer.lua

raw
1 local Buffer = 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 buffer from opts
9 ---@param opts table
10 function Buffer:init(opts)
11 assert(opts.bufnr, 'Cannot create Buffer without bufnr')
12 self.bufnr = opts.bufnr
13 self.buf_index = opts.buf_index
14 self.options = opts.options
15 self.highlights = opts.highlights
16 self:get_props()
17 end
18
19 function Buffer:is_current()
20 return vim.api.nvim_get_current_buf() == self.bufnr
21 end
22
23 function Buffer:is_alternate()
24 return vim.fn.bufnr('#') == self.bufnr and not self:is_current()
25 end
26
27 ---setup icons, modified status for buffer
28 function Buffer:get_props()
29 self.file = modules.utils.stl_escape(vim.api.nvim_buf_get_name(self.bufnr))
30 self.buftype = vim.api.nvim_buf_get_option(self.bufnr, 'buftype')
31 self.filetype = vim.api.nvim_buf_get_option(self.bufnr, 'filetype')
32 local modified = self.options.show_modified_status and vim.api.nvim_buf_get_option(self.bufnr, 'modified')
33 self.modified_icon = modified and self.options.symbols.modified or ''
34 self.alternate_file_icon = self:is_alternate() and self.options.symbols.alternate_file or ''
35 self.icon = ''
36 if self.options.icons_enabled then
37 local dev
38 local status, _ = pcall(require, 'nvim-web-devicons')
39 if not status then
40 dev, _ = '', ''
41 elseif self.filetype == 'TelescopePrompt' then
42 dev, _ = require('nvim-web-devicons').get_icon('telescope')
43 elseif self.filetype == 'fugitive' then
44 dev, _ = require('nvim-web-devicons').get_icon('git')
45 elseif self.filetype == 'vimwiki' then
46 dev, _ = require('nvim-web-devicons').get_icon('markdown')
47 elseif self.buftype == 'terminal' then
48 dev, _ = require('nvim-web-devicons').get_icon('zsh')
49 elseif vim.fn.isdirectory(self.file) == 1 then
50 dev, _ = self.options.symbols.directory, nil
51 else
52 dev, _ = require('nvim-web-devicons').get_icon(self.file, vim.fn.expand('#' .. self.bufnr .. ':e'))
53 end
54 if dev then
55 self.icon = dev .. ' '
56 end
57 end
58 end
59
60 ---returns line configured for handling mouse click
61 ---@param name string
62 ---@return string
63 function Buffer:configure_mouse_click(name)
64 return string.format('%%%s@LualineSwitchBuffer@%s%%T', self.bufnr, name)
65 end
66
67 ---returns rendered buffer
68 ---@return string
69 function Buffer:render()
70 local name = self:name()
71 if self.options.fmt then
72 name = self.options.fmt(name or '', self)
73 end
74
75 if self.ellipse then -- show ellipsis
76 name = '...'
77 else
78 name = self:apply_mode(name)
79 end
80 name = Buffer.apply_padding(name, self.options.padding)
81 self.len = vim.fn.strchars(name)
82
83 -- setup for mouse clicks
84 local line = self:configure_mouse_click(name)
85 -- apply highlight
86 line = modules.highlight.component_format_highlight(self.highlights[(self.current and 'active' or 'inactive')])
87 .. line
88
89 -- apply separators
90 if self.options.self.section < 'x' and not self.first then
91 local sep_before = self:separator_before()
92 line = sep_before .. line
93 self.len = self.len + vim.fn.strchars(sep_before)
94 elseif self.options.self.section >= 'x' and not self.last then
95 local sep_after = self:separator_after()
96 line = line .. sep_after
97 self.len = self.len + vim.fn.strchars(sep_after)
98 end
99 return line
100 end
101
102 ---apply separator before current buffer
103 ---@return string
104 function Buffer:separator_before()
105 if self.current or self.aftercurrent then
106 return '%Z{' .. self.options.section_separators.left .. '}'
107 else
108 return self.options.component_separators.left
109 end
110 end
111
112 ---apply separator after current buffer
113 ---@return string
114 function Buffer:separator_after()
115 if self.current or self.beforecurrent then
116 return '%z{' .. self.options.section_separators.right .. '}'
117 else
118 return self.options.component_separators.right
119 end
120 end
121
122 ---returns name of current buffer after filtering special buffers
123 ---@return string
124 function Buffer:name()
125 if self.options.filetype_names[self.filetype] then
126 return self.options.filetype_names[self.filetype]
127 elseif self.buftype == 'help' then
128 return 'help:' .. vim.fn.fnamemodify(self.file, ':t:r')
129 elseif self.buftype == 'terminal' then
130 local match = string.match(vim.split(self.file, ' ')[1], 'term:.*:(%a+)')
131 return match ~= nil and match or vim.fn.fnamemodify(vim.env.SHELL, ':t')
132 elseif self.buftype == 'quickfix' then
133 local is_loclist = 0 ~= vim.fn.getloclist(0, { filewinid = 1 }).filewinid
134 return is_loclist and 'Location list' or 'Quickfix List'
135 elseif vim.fn.isdirectory(self.file) == 1 then
136 return vim.fn.fnamemodify(self.file, ':p:.')
137 elseif self.file == '' then
138 return '[No Name]'
139 end
140
141 local name
142 if self.options.show_filename_only then
143 name = vim.fn.fnamemodify(self.file, ':t')
144 else
145 name = vim.fn.pathshorten(vim.fn.fnamemodify(self.file, ':p:.'))
146 end
147 if self.options.hide_filename_extension then
148 name = vim.fn.fnamemodify(name, ':r')
149 end
150 return name
151 end
152
153 ---adds spaces to left and right
154 function Buffer.apply_padding(str, padding)
155 local l_padding, r_padding = 1, 1
156 if type(padding) == 'number' then
157 l_padding, r_padding = padding, padding
158 elseif type(padding) == 'table' then
159 l_padding, r_padding = padding.left or 0, padding.right or 0
160 end
161 return string.rep(' ', l_padding) .. str .. string.rep(' ', r_padding)
162 end
163
164 function Buffer:apply_mode(name)
165 if self.options.mode == 0 then
166 return string.format('%s%s%s%s', self.alternate_file_icon, self.icon, name, self.modified_icon)
167 end
168
169 if self.options.mode == 1 then
170 return string.format('%s%s %s%s', self.alternate_file_icon, self.buf_index or '', self.icon, self.modified_icon)
171 end
172
173 if self.options.mode == 2 then
174 return string.format(
175 '%s%s %s%s%s',
176 self.alternate_file_icon,
177 self.buf_index or '',
178 self.icon,
179 name,
180 self.modified_icon
181 )
182 end
183
184 if self.options.mode == 3 then
185 return string.format('%s%s %s%s', self.alternate_file_icon, self.bufnr or '', self.icon, self.modified_icon)
186 end
187
188 -- if self.options.mode == 4 then
189 return string.format('%s%s %s%s%s', self.alternate_file_icon, self.bufnr or '', self.icon, name, self.modified_icon)
190 end
191
192 return Buffer