Skip to content

Commit 7570c79

Browse files
committed
feat(ui): add show_modified option
1 parent 32d74d5 commit 7570c79

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

doc/bufferline.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ The available configuration are:
164164
sort_by = 'insert_after_current' |'insert_at_end' | 'id' | 'extension' | 'relative_directory' | 'directory' | 'tabs' | function(buffer_a, buffer_b)
165165
-- add custom logic
166166
return buffer_a.modified > buffer_b.modified
167-
end
167+
end,
168+
show_modified = true | false | "active" | "inactive" | function(context): boolean
168169
}
169170
}
170171
<

lua/bufferline/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ local function get_defaults()
651651
groups = { items = {}, options = { toggle_hidden_on_enter = true } },
652652
hover = { enabled = false, reveal = {}, delay = 200 },
653653
debug = { logging = false },
654+
show_modified = true,
654655
}
655656
return { options = opts, highlights = derive_colors(opts.style_preset) }
656657
end

lua/bufferline/ui.lua

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,26 @@ local function add_icon(context)
313313
end
314314
end
315315

316+
--- Determine if the modified icon should be shown for the current element.
317+
--- @param context bufferline.RenderContext
318+
--- @return boolean
319+
local function show_modified(context)
320+
local show_modified = config.options.show_modified
321+
local fn
322+
if type(show_modified) == "boolean" then
323+
fn = function(context) return show_modified end
324+
elseif type(show_modified) == "string" then
325+
local t = {
326+
["active"] = function(context) return context.tab:current() end,
327+
["inactive"] = function(context) return not context.tab:current() end,
328+
}
329+
fn = t[show_modified]
330+
elseif type(show_modified) == "function" then
331+
fn = show_modified
332+
end
333+
return fn == nil or fn(context) -- default to true
334+
end
335+
316336
--- The suffix can be either the modified icon, space to replace the icon if
317337
--- a user has turned them off or the close icon if the element is not currently modified
318338
--- @param context bufferline.RenderContext
@@ -329,7 +349,7 @@ local function add_suffix(context)
329349
highlight = element.modified and hl.modified or nil,
330350
}
331351
local close = get_close_icon(element.id, context)
332-
return not element.modified and close or modified
352+
return (not show_modified(context) or not element.modified) and close or modified
333353
end
334354

335355
--- TODO: We increment the buffer length by the separator although the final

tests/ui_spec.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,50 @@ describe("UI Tests", function()
6969
})
7070
assert.is_equal(segment.text, "a_very_very_very_…")
7171
end)
72+
73+
it("should render an icon if show_modified is true", function()
74+
config.setup({ options = { show_modified = true } })
75+
config.apply()
76+
local buf = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true })
77+
local el = ui.element({}, buf)
78+
local segment = ui.to_tabline_str(el:component(1))
79+
assert.is_truthy(segment:match(config.options.modified_icon))
80+
end)
81+
82+
it("should not render an icon if show_modified is false", function()
83+
config.setup({ options = { show_modified = false } })
84+
config.apply()
85+
local buf = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true })
86+
local el = ui.element({}, buf)
87+
local segment = ui.to_tabline_str(el:component(1))
88+
assert.is_falsy(segment:match(config.options.modified_icon))
89+
end)
90+
91+
it("should render an icon if show_modified is active", function()
92+
config.setup({ options = { show_modified = "active" } })
93+
config.apply()
94+
local buf1 = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true, _is_current = true })
95+
local buf2 = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true, _is_current = false })
96+
local el1 = ui.element({}, buf1)
97+
local el2 = ui.element({}, buf2)
98+
local segment1 = ui.to_tabline_str(el1:component(1))
99+
local segment2 = ui.to_tabline_str(el2:component(1))
100+
assert.is_truthy(segment1:match(config.options.modified_icon))
101+
assert.is_falsy(segment2:match(config.options.modified_icon))
102+
end)
103+
104+
it("should render an icon if show_modified is inactive", function()
105+
config.setup({ options = { show_modified = "inactive" } })
106+
config.apply()
107+
local buf1 = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true, _is_current = false })
108+
local buf2 = MockBuffer:new({ id = 1, name = "file.txt", length = 10, modified = true, _is_current = true })
109+
local el1 = ui.element({}, buf1)
110+
local el2 = ui.element({}, buf2)
111+
local segment1 = ui.to_tabline_str(el1:component(1))
112+
local segment2 = ui.to_tabline_str(el2:component(1))
113+
assert.is_truthy(segment1:match(config.options.modified_icon))
114+
assert.is_falsy(segment2:match(config.options.modified_icon))
115+
end)
72116
end)
73117

74118
describe("Hover events - ", function()

0 commit comments

Comments
 (0)