@@ -5,6 +5,30 @@ local map = require("veil").map
55local builtin = {
66 sections = {},
77 headers = {},
8+ highlight = {
9+ normal = function ()
10+ local v = require (" veil" ).api .get_buf ()
11+ local b = vim .api .nvim_get_current_buf ()
12+ if v == b then
13+ local norm = vim .fn .hlID (" Normal" )
14+ return { fg = vim .fn .synIDattr (norm , " fg" ), bg = vim .fn .synIDattr (norm , " bg" ) }
15+ else
16+ local nc = vim .fn .hlID (" NormalNC" )
17+ return { fg = vim .fn .synIDattr (nc , " fg" ), bg = vim .fn .synIDattr (nc , " bg" ) }
18+ end
19+ end ,
20+ visual = function ()
21+ local v = require (" veil" ).api .get_buf ()
22+ local b = vim .api .nvim_get_current_buf ()
23+ local norm = vim .api .nvim_get_hl_by_name (" Visual" , true )
24+ local nc = vim .api .nvim_get_hl_by_name (" NormalNC" , true )
25+ if v == b then
26+ return { fg = norm .guifg or norm .fg or norm .foreground , bg = norm .guibg or norm .bg or norm .background }
27+ else
28+ return { fg = nc .guifg or nc .fg or nc .foreground , bg = norm .guibg or norm .bg or norm .background }
29+ end
30+ end ,
31+ },
832}
933
1034function builtin .sections .animated (frames , opts )
@@ -30,6 +54,68 @@ function builtin.sections.animated(frames, opts)
3054 })
3155end
3256
57+ function builtin .sections .oldfiles (options )
58+ local opts = options or {}
59+ local has_icons , icons = pcall (require , " nvim-web-devicons" )
60+ return Section :new ({
61+ header_size = 2 ,
62+ state = {
63+ files = vim .v .oldfiles ,
64+ line_nrs = {},
65+ max = opts .max or 5 ,
66+ home = vim .loop .os_homedir (),
67+ icons = has_icons and icons or nil ,
68+ },
69+ contents = function (self )
70+ local lines = {}
71+ local align = opts .align or " center"
72+ self .files = vim .v .oldfiles
73+ local maxwidth = 0
74+ for i , file in ipairs (self .files ) do
75+ if string.match (file , self .home ) ~= nil then
76+ local f , e = vim .loop .fs_stat (vim .fn .fnamemodify (file , " :p:s?" .. self .home .. " ?" ))
77+ if f ~= nil and e == nil then
78+ local s = vim .fn .fnamemodify (file , " :~:." )
79+ if self .icons then
80+ local icon = self .icons .get_icon (s , vim .fn .fnamemodify (s , " :e" ), { default = true })
81+ s = icon .. " " .. s
82+ end
83+ table.insert (lines , s )
84+ table.insert (self .line_nrs , i )
85+ maxwidth = math.max (maxwidth , # s )
86+ if # lines >= self .max then
87+ break
88+ end
89+ end
90+ end
91+ end
92+ table.insert (lines , 1 , " Recent files" )
93+ table.insert (lines , 2 , " " )
94+ for i , line in ipairs (lines ) do
95+ if align == " center" and line ~= " " then
96+ lines [i ] = string.rep (" " , math.floor ((maxwidth - # line ) / 2 )) .. line
97+ elseif align == " right" and line ~= " " then
98+ lines [i ] = string.rep (" " , maxwidth - # line ) .. line
99+ end
100+ end
101+
102+ return lines
103+ end ,
104+ hl = builtin .highlight .normal ,
105+ focused_hl = opts .focused_hl or builtin .highlight .visual ,
106+ interactive = true ,
107+ on_interact = function (self , line , _col )
108+ if line <= 2 then
109+ return
110+ end
111+ -- open the file
112+ vim .cmd (string.format (" edit %s" , self .files [self .line_nrs [line - 2 ]]))
113+ -- go to last place in file
114+ vim .api .nvim_feedkeys (" '." , " n" , false )
115+ end ,
116+ })
117+ end
118+
33119function builtin .sections .buttons (buttons , options )
34120 local opts = options or {}
35121 for _ , button in ipairs (buttons ) do
@@ -46,6 +132,8 @@ function builtin.sections.buttons(buttons, options)
46132 end ,
47133 contents = function (self )
48134 local lines = {}
135+ local align = opts .align or " center"
136+ local maxwidth = 0
49137 for _ , button in ipairs (self .buttons ) do
50138 local s = string.format (
51139 " %s%s %s" ,
@@ -56,12 +144,23 @@ function builtin.sections.buttons(buttons, options)
56144 if # s % 2 ~= 0 then
57145 s = s .. " "
58146 end
147+ maxwidth = math.max (maxwidth , # s )
59148 table.insert (lines , s )
60149 end
150+ for i , line in ipairs (lines ) do
151+ if align == " center" then
152+ -- lines[i] = string.rep(" ", math.floor((maxwidth - #line) / 2)) .. line
153+ if # lines [i ] < maxwidth then
154+ lines [i ] = lines [i ] .. string.rep (" " , maxwidth - # lines [i ])
155+ end
156+ elseif align == " right" then
157+ lines [i ] = string.rep (" " , maxwidth - # line ) .. line
158+ end
159+ end
61160 return lines
62161 end ,
63- hl = opts .hl or " Normal " ,
64- focused_hl = opts .focused_hl or " Visual " ,
162+ hl = opts .hl or builtin . highlight . normal ,
163+ focused_hl = opts .focused_hl or builtin . highlight . visual ,
65164 interactive = true ,
66165 })
67166end
0 commit comments