mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
refactor: rework width calculation
This commit is contained in:
parent
b27ef82ecd
commit
c0bf888247
4 changed files with 24 additions and 4 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -134,6 +134,7 @@ dependencies = [
|
|||
"tracing",
|
||||
"tracing-appender",
|
||||
"tracing-subscriber",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ libc = "^0"
|
|||
ratatui = "^0"
|
||||
signal-hook-tokio = { version = "^0", features = [ "futures-v0_3" ] }
|
||||
tokio = { version = "^1", features = [ "parking_lot" ] }
|
||||
unicode-width = "^0"
|
||||
|
||||
# Logging
|
||||
tracing = "^0"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
use std::ops::ControlFlow;
|
||||
|
||||
use config::THEME;
|
||||
use ratatui::{buffer::Buffer, layout::{Alignment, Rect}, text::{Line, Span}, widgets::{Paragraph, Widget}};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::Ctx;
|
||||
|
||||
|
|
@ -21,9 +24,24 @@ impl<'a> Widget for Tabs<'a> {
|
|||
.enumerate()
|
||||
.map(|(i, tab)| {
|
||||
let mut text = format!(" {} ", i + 1);
|
||||
if THEME.tab.max_width > 0 {
|
||||
text
|
||||
.push_str(&tab.current_name().chars().take(THEME.tab.max_width).collect::<String>());
|
||||
if let Some(dir_name) = tab.current_name() {
|
||||
text.push_str(dir_name);
|
||||
}
|
||||
|
||||
let threshold = THEME.tab.max_width.max(3) - 1;
|
||||
let truncated = text.chars().try_fold(String::with_capacity(threshold), |mut text, c| {
|
||||
if text.width() > threshold {
|
||||
ControlFlow::Break(text)
|
||||
} else {
|
||||
text.push(c);
|
||||
ControlFlow::Continue(text)
|
||||
}
|
||||
});
|
||||
let mut text = match truncated {
|
||||
ControlFlow::Break(text) => text,
|
||||
ControlFlow::Continue(text) => text,
|
||||
};
|
||||
if !text.ends_with(' ') {
|
||||
text.push(' ');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
[tab]
|
||||
active = { fg = "#1E2031", bg = "#80AEFA" }
|
||||
inactive = { fg = "#C8D3F8", bg = "#484D66" }
|
||||
max_width = 0
|
||||
max_width = 3
|
||||
|
||||
[status]
|
||||
primary = { normal = "#80AEFA", select = "#CD9EFC", unset = "#FFA577" }
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue