Skip to content

Commit

Permalink
feat: add counter component to the header for displaying currently ya…
Browse files Browse the repository at this point in the history
…nked/selected items (#646)
  • Loading branch information
Rolv-Apneseth authored Feb 15, 2024
1 parent 52d91c0 commit b82587e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 6 additions & 1 deletion yazi-config/preset/theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ marker_marked = { fg = "lightyellow", bg = "lightyellow" }
marker_selected = { fg = "lightblue", bg = "lightblue" }

# Tab
tab_active = { fg = "black", bg = "lightblue" }
tab_active = { fg = "black", bg = "white" }
tab_inactive = { fg = "white", bg = "darkgray" }
tab_width = 1

# Count
count_copied = { fg = "black", bg = "lightgreen" }
count_cut = { fg = "black", bg = "lightred" }
count_selected = { fg = "black", bg = "lightblue" }

# Border
border_symbol = ""
border_style = { fg = "gray" }
Expand Down
5 changes: 5 additions & 0 deletions yazi-config/src/theme/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub struct Manager {
#[validate(range(min = 1, message = "Must be greater than 0"))]
tab_width: u8,

// Selected counter
count_selected: Style,
count_copied: Style,
count_cut: Style,

// Border
pub border_symbol: String,
pub border_style: Style,
Expand Down
34 changes: 32 additions & 2 deletions yazi-plugin/preset/components/header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,39 @@ function Header:cwd()
return span:style(THEME.manager.cwd)
end

function Header:counter()
local yanked = #cx.yanked

local count, style
if yanked == 0 then
count = #cx.active.selected
style = THEME.manager.count_selected
elseif cx.yanked.is_cut then
count = yanked
style = THEME.manager.count_cut
else
count = yanked
style = THEME.manager.count_copied
end

if count == 0 then
return ui.Line {}
end

return ui.Line {
ui.Span(string.format(" %d ", count)):style(style),
ui.Span(" "),
}
end

function Header:tabs()
local tabs = #cx.tabs
if tabs == 1 then
return ui.Line {}
end

local spans = {}
for i = 1, #cx.tabs do
for i = 1, tabs do
local text = i
if THEME.manager.tab_width > 2 then
text = ya.truncate(text .. " " .. cx.tabs[i]:name(), THEME.manager.tab_width)
Expand All @@ -43,7 +73,7 @@ function Header:render(area)
local chunks = self:layout(area)

local left = ui.Line { self:cwd() }
local right = ui.Line { self:tabs() }
local right = ui.Line { self:counter(), self:tabs() }
return {
ui.Paragraph(chunks[1], { left }),
ui.Paragraph(chunks[2], { right }):align(ui.Paragraph.RIGHT),
Expand Down

0 comments on commit b82587e

Please sign in to comment.