Debian Codegraph @

lua/lualine/components/filename.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3 local M = require('lualine.component'):extend()
4
5 local modules = require('lualine_require').lazy_require {
6 utils = 'lualine.utils.utils',
7 }
8
9 local default_options = {
10 symbols = {
11 modified = '[+]',
12 readonly = '[-]',
13 unnamed = '[No Name]',
14 newfile = '[New]',
15 },
16 file_status = true,
17 newfile_status = false,
18 path = 0,
19 shorting_target = 40,
20 }
21
22 local function is_new_file()
23 local filename = vim.fn.expand('%')
24 return filename ~= ''
25 and filename:match('^%a+://') == nil
26 and vim.bo.buftype == ''
27 and vim.fn.filereadable(filename) == 0
28 end
29
30 ---shortens path by turning apple/orange -> a/orange
31 ---@param path string
32 ---@param sep string path separator
33 ---@param max_len integer maximum length of the full filename string
34 ---@return string
35 local function shorten_path(path, sep, max_len)
36 local len = #path
37 if len <= max_len then
38 return path
39 end
40
41 local segments = vim.split(path, sep)
42 for idx = 1, #segments - 1 do
43 if len <= max_len then
44 break
45 end
46
47 local segment = segments[idx]
48 local shortened = segment:sub(1, vim.startswith(segment, '.') and 2 or 1)
49 segments[idx] = shortened
50 len = len - (#segment - #shortened)
51 end
52
53 return table.concat(segments, sep)
54 end
55
56 local function filename_and_parent(path, sep)
57 local segments = vim.split(path, sep)
58 if #segments == 0 then
59 return path
60 elseif #segments == 1 then
61 return segments[#segments]
62 else
63 return table.concat({ segments[#segments - 1], segments[#segments] }, sep)
64 end
65 end
66
67 M.init = function(self, options)
68 M.super.init(self, options)
69 self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
70 end
71
72 M.update_status = function(self)
73 local path_separator = package.config:sub(1, 1)
74 local data
75 if self.options.path == 1 then
76 -- relative path
77 data = vim.fn.expand('%:~:.')
78 elseif self.options.path == 2 then
79 -- absolute path
80 data = vim.fn.expand('%:p')
81 elseif self.options.path == 3 then
82 -- absolute path, with tilde
83 data = vim.fn.expand('%:p:~')
84 elseif self.options.path == 4 then
85 -- filename and immediate parent
86 data = filename_and_parent(vim.fn.expand('%:p:~'), path_separator)
87 else
88 -- just filename
89 data = vim.fn.expand('%:t')
90 end
91
92 if data == '' then
93 data = self.options.symbols.unnamed
94 end
95
96 local shorting_target = self.options.shorting_target
97 if type(shorting_target) == 'function' then
98 shorting_target = shorting_target()
99 end
100
101 if shorting_target ~= 0 then
102 local windwidth = self.options.globalstatus and vim.go.columns or vim.fn.winwidth(0)
103 local estimated_space_available = windwidth - shorting_target
104
105 data = shorten_path(data, path_separator, estimated_space_available)
106 end
107
108 data = modules.utils.stl_escape(data)
109
110 local symbols = {}
111 if self.options.file_status then
112 if vim.bo.modified then
113 table.insert(symbols, self.options.symbols.modified)
114 end
115 if vim.bo.modifiable == false or vim.bo.readonly == true then
116 table.insert(symbols, self.options.symbols.readonly)
117 end
118 end
119
120 if self.options.newfile_status and is_new_file() then
121 table.insert(symbols, self.options.symbols.newfile)
122 end
123
124 return data .. (#symbols > 0 and ' ' .. table.concat(symbols, '') or '')
125 end
126
127 return M