feat: show current dir name in tab (#41)

This commit is contained in:
Pig Fang 2023-08-10 18:28:42 +08:00 committed by GitHub
parent 4823c0abc4
commit f119489907
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 7 deletions

1
Cargo.lock generated
View file

@ -134,6 +134,7 @@ dependencies = [
"tracing", "tracing",
"tracing-appender", "tracing-appender",
"tracing-subscriber", "tracing-subscriber",
"unicode-width",
] ]
[[package]] [[package]]

View file

@ -18,6 +18,7 @@ libc = "^0"
ratatui = "^0" ratatui = "^0"
signal-hook-tokio = { version = "^0", features = [ "futures-v0_3" ] } signal-hook-tokio = { version = "^0", features = [ "futures-v0_3" ] }
tokio = { version = "^1", features = [ "parking_lot" ] } tokio = { version = "^1", features = [ "parking_lot" ] }
unicode-width = "^0"
# Logging # Logging
tracing = "^0" tracing = "^0"

View file

@ -1,5 +1,8 @@
use std::ops::ControlFlow;
use config::THEME; use config::THEME;
use ratatui::{buffer::Buffer, layout::{Alignment, Rect}, text::{Line, Span}, widgets::{Paragraph, Widget}}; use ratatui::{buffer::Buffer, layout::{Alignment, Rect}, text::{Line, Span}, widgets::{Paragraph, Widget}};
use unicode_width::UnicodeWidthStr;
use crate::Ctx; use crate::Ctx;
@ -19,11 +22,31 @@ impl<'a> Widget for Tabs<'a> {
tabs tabs
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, _)| { .map(|(i, tab)| {
let mut text = format!("{}", i + 1);
if let Some(dir_name) = tab.current_name() {
text.push(' ');
text.push_str(dir_name);
}
let threshold = THEME.tab.max_width.max(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 text = match truncated {
ControlFlow::Break(text) => text,
ControlFlow::Continue(text) => text,
};
if i == tabs.idx() { if i == tabs.idx() {
Span::styled(format!(" {} ", i + 1), THEME.tab.active.get()) Span::styled(format!(" {text} "), THEME.tab.active.get())
} else { } else {
Span::styled(format!(" {} ", i + 1), THEME.tab.inactive.get()) Span::styled(format!(" {text} "), THEME.tab.inactive.get())
} }
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),

View file

@ -1,6 +1,7 @@
[tab] [tab]
active = { fg = "#1E2031", bg = "#80AEFA" } active = { fg = "#1E2031", bg = "#80AEFA" }
inactive = { fg = "#C8D3F8", bg = "#484D66" } inactive = { fg = "#C8D3F8", bg = "#484D66" }
max_width = 1
[status] [status]
primary = { normal = "#80AEFA", select = "#CD9EFC", unset = "#FFA577" } primary = { normal = "#80AEFA", select = "#CD9EFC", unset = "#FFA577" }

View file

@ -8,8 +8,9 @@ use crate::MERGED_THEME;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Tab { pub struct Tab {
pub active: Style, pub active: Style,
pub inactive: Style, pub inactive: Style,
pub max_width: usize,
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -270,6 +270,15 @@ impl Tab {
}; };
true true
} }
pub fn current_name(&self) -> Option<&str> {
self
.current
.cwd
.file_name()
.and_then(|name| name.to_str())
.or_else(|| self.current.cwd.to_str())
}
} }
impl Tab { impl Tab {