From 0aacc1887133928a42f3bca7cab34be6d03ea056 Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier Date: Sat, 8 Feb 2025 13:37:11 +0100 Subject: [PATCH] refactor(ui): display current channel in input bar border --- television/channels/cable.rs | 2 +- television/channels/mod.rs | 8 ++++++++ television/draw.rs | 16 +++++++--------- television/screen/help.rs | 17 ++++++++++------- television/screen/input.rs | 9 +++++++-- television/screen/metadata.rs | 5 ++--- television/television.rs | 2 +- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/television/channels/cable.rs b/television/channels/cable.rs index 33012cc..14c7162 100644 --- a/television/channels/cable.rs +++ b/television/channels/cable.rs @@ -29,7 +29,7 @@ enum PreviewKind { #[allow(dead_code)] pub struct Channel { - name: String, + pub name: String, matcher: Matcher, entries_command: String, preview_kind: PreviewKind, diff --git a/television/channels/mod.rs b/television/channels/mod.rs index 0579ebf..12b21a5 100644 --- a/television/channels/mod.rs +++ b/television/channels/mod.rs @@ -174,6 +174,14 @@ impl TelevisionChannel { _ => unreachable!(), } } + + pub fn name(&self) -> String { + match self { + TelevisionChannel::Cable(channel) => channel.name.clone(), + TelevisionChannel::Stdin(_) => String::from("Stdin"), + _ => UnitChannel::from(self).to_string(), + } + } } macro_rules! variant_to_module { diff --git a/television/draw.rs b/television/draw.rs index ca5f5bd..3846791 100644 --- a/television/draw.rs +++ b/television/draw.rs @@ -7,10 +7,7 @@ use tokio::sync::mpsc::Sender; use crate::{ action::Action, - channels::{ - entry::{Entry, PreviewType, ENTRY_PLACEHOLDER}, - UnitChannel, - }, + channels::entry::{Entry, PreviewType, ENTRY_PLACEHOLDER}, config::Config, picker::Picker, preview::PreviewState, @@ -27,7 +24,7 @@ use crate::{ #[derive(Debug, Clone, PartialEq)] pub struct ChannelState { - pub current_channel: UnitChannel, + pub current_channel_name: String, pub selected_entries: FxHashSet, pub total_count: u32, pub running: bool, @@ -35,13 +32,13 @@ pub struct ChannelState { impl ChannelState { pub fn new( - current_channel: UnitChannel, + current_channel_name: String, selected_entries: FxHashSet, total_count: u32, running: bool, ) -> Self { Self { - current_channel, + current_channel_name, selected_entries, total_count, running, @@ -51,7 +48,7 @@ impl ChannelState { impl Hash for ChannelState { fn hash(&self, state: &mut H) { - self.current_channel.hash(state); + self.current_channel_name.hash(state); self.selected_entries .iter() .for_each(|entry| entry.hash(state)); @@ -177,7 +174,7 @@ pub fn draw(ctx: &Ctx, f: &mut Frame<'_>, area: Rect) -> Result<()> { draw_help_bar( f, &layout.help_bar, - ctx.tv_state.channel_state.current_channel, + &ctx.tv_state.channel_state.current_channel_name, build_keybindings_table( &ctx.config.keybindings.to_displayable(), ctx.tv_state.mode, @@ -233,6 +230,7 @@ pub fn draw(ctx: &Ctx, f: &mut Frame<'_>, area: Rect) -> Result<()> { &ctx.tv_state.results_picker.input, &ctx.tv_state.results_picker.state, ctx.tv_state.channel_state.running, + &ctx.tv_state.channel_state.current_channel_name, &ctx.tv_state.spinner, &ctx.colorscheme, )?; diff --git a/television/screen/help.rs b/television/screen/help.rs index 3216097..e9e3f91 100644 --- a/television/screen/help.rs +++ b/television/screen/help.rs @@ -1,5 +1,4 @@ use super::layout::HelpBarLayout; -use crate::channels::UnitChannel; use crate::screen::colors::{Colorscheme, GeneralColorscheme}; use crate::screen::logo::build_logo_paragraph; use crate::screen::metadata::build_metadata_table; @@ -37,7 +36,7 @@ fn draw_metadata_block( f: &mut Frame, area: Rect, mode: Mode, - current_channel: UnitChannel, + current_channel_name: &str, app_metadata: &AppMetadata, colorscheme: &Colorscheme, ) { @@ -51,9 +50,13 @@ fn draw_metadata_block( .bg(colorscheme.general.background.unwrap_or_default()), ); - let metadata_table = - build_metadata_table(mode, current_channel, app_metadata, colorscheme) - .block(metadata_block); + let metadata_table = build_metadata_table( + mode, + current_channel_name, + app_metadata, + colorscheme, + ) + .block(metadata_block); f.render_widget(metadata_table, area); } @@ -79,7 +82,7 @@ fn draw_keymaps_block( pub fn draw_help_bar( f: &mut Frame, layout: &Option, - current_channel: UnitChannel, + current_channel_name: &str, keymap_table: Table, mode: Mode, app_metadata: &AppMetadata, @@ -90,7 +93,7 @@ pub fn draw_help_bar( f, help_bar.left, mode, - current_channel, + current_channel_name, app_metadata, colorscheme, ); diff --git a/television/screen/input.rs b/television/screen/input.rs index 5c10f99..5896a6a 100644 --- a/television/screen/input.rs +++ b/television/screen/input.rs @@ -5,14 +5,13 @@ use ratatui::{ Alignment, Constraint, Direction, Layout as RatatuiLayout, Rect, }, style::{Style, Stylize}, - text::Span, + text::{Line, Span}, widgets::{Block, BorderType, Borders, ListState, Paragraph}, Frame, }; use crate::screen::{colors::Colorscheme, spinner::Spinner}; -// TODO: refactor arguments (e.g. use a struct for the spinner+state, same #[allow(clippy::too_many_arguments)] pub fn draw_input_box( f: &mut Frame, @@ -22,6 +21,7 @@ pub fn draw_input_box( input_state: &Input, results_picker_state: &ListState, matcher_running: bool, + channel_name: &str, spinner: &Spinner, colorscheme: &Colorscheme, ) -> Result<()> { @@ -29,6 +29,11 @@ pub fn draw_input_box( .borders(Borders::ALL) .border_type(BorderType::Rounded) .border_style(Style::default().fg(colorscheme.general.border_fg)) + .title_top( + Line::from(String::from(" ") + channel_name + " ") + .style(Style::default().fg(colorscheme.mode.channel).bold()) + .centered(), + ) .style( Style::default() .bg(colorscheme.general.background.unwrap_or_default()), diff --git a/television/screen/metadata.rs b/television/screen/metadata.rs index 8426df5..2ca1beb 100644 --- a/television/screen/metadata.rs +++ b/television/screen/metadata.rs @@ -1,6 +1,5 @@ use std::fmt::Display; -use crate::channels::UnitChannel; use crate::screen::{colors::Colorscheme, mode::mode_color}; use crate::television::Mode; use crate::utils::metadata::AppMetadata; @@ -23,7 +22,7 @@ impl Display for Mode { pub fn build_metadata_table<'a>( mode: Mode, - current_channel: UnitChannel, + current_channel_name: &'a str, app_metadata: &'a AppMetadata, colorscheme: &'a Colorscheme, ) -> Table<'a> { @@ -58,7 +57,7 @@ pub fn build_metadata_table<'a>( Style::default().fg(colorscheme.help.metadata_field_name_fg), )), Cell::from(Span::styled( - current_channel.to_string(), + current_channel_name, Style::default().fg(colorscheme.help.metadata_field_value_fg), )), ]); diff --git a/television/television.rs b/television/television.rs index ac5404f..c5acc44 100644 --- a/television/television.rs +++ b/television/television.rs @@ -127,7 +127,7 @@ impl Television { pub fn dump_context(&self) -> Ctx { let channel_state = ChannelState::new( - self.current_channel(), + self.channel.name(), self.channel.selected_entries().clone(), self.channel.total_count(), self.channel.running(),