From f119489907fb3e724e320feeeef1a6024c053fee Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Thu, 10 Aug 2023 18:28:42 +0800 Subject: [PATCH] feat: show current dir name in tab (#41) --- Cargo.lock | 1 + app/Cargo.toml | 1 + app/src/header/tabs.rs | 29 ++++++++++++++++++++++++++--- config/preset/theme.toml | 5 +++-- config/src/theme/theme.rs | 5 +++-- core/src/manager/tab.rs | 9 +++++++++ 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e99e9f20..4ca59c32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,6 +134,7 @@ dependencies = [ "tracing", "tracing-appender", "tracing-subscriber", + "unicode-width", ] [[package]] diff --git a/app/Cargo.toml b/app/Cargo.toml index 239afaed..099f2ce1 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -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" diff --git a/app/src/header/tabs.rs b/app/src/header/tabs.rs index 02f8ca44..414eef59 100644 --- a/app/src/header/tabs.rs +++ b/app/src/header/tabs.rs @@ -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; @@ -19,11 +22,31 @@ impl<'a> Widget for Tabs<'a> { tabs .iter() .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() { - Span::styled(format!(" {} ", i + 1), THEME.tab.active.get()) + Span::styled(format!(" {text} "), THEME.tab.active.get()) } else { - Span::styled(format!(" {} ", i + 1), THEME.tab.inactive.get()) + Span::styled(format!(" {text} "), THEME.tab.inactive.get()) } }) .collect::>(), diff --git a/config/preset/theme.toml b/config/preset/theme.toml index 8016374c..a4cb7061 100644 --- a/config/preset/theme.toml +++ b/config/preset/theme.toml @@ -1,6 +1,7 @@ [tab] -active = { fg = "#1E2031", bg = "#80AEFA" } -inactive = { fg = "#C8D3F8", bg = "#484D66" } +active = { fg = "#1E2031", bg = "#80AEFA" } +inactive = { fg = "#C8D3F8", bg = "#484D66" } +max_width = 1 [status] primary = { normal = "#80AEFA", select = "#CD9EFC", unset = "#FFA577" } diff --git a/config/src/theme/theme.rs b/config/src/theme/theme.rs index 34adc49e..d3f66548 100644 --- a/config/src/theme/theme.rs +++ b/config/src/theme/theme.rs @@ -8,8 +8,9 @@ use crate::MERGED_THEME; #[derive(Deserialize)] pub struct Tab { - pub active: Style, - pub inactive: Style, + pub active: Style, + pub inactive: Style, + pub max_width: usize, } #[derive(Deserialize)] diff --git a/core/src/manager/tab.rs b/core/src/manager/tab.rs index 9124c995..78706e25 100644 --- a/core/src/manager/tab.rs +++ b/core/src/manager/tab.rs @@ -270,6 +270,15 @@ impl Tab { }; 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 {