Skip to content

Commit

Permalink
refactor(ui): display current channel in input bar border (#354)
Browse files Browse the repository at this point in the history
<img width="860" alt="Screenshot 2025-02-08 at 13 38 42"
src="https://github.com/user-attachments/assets/ab1558a9-75f6-4006-be2b-9a78dc56b44f"
/>
  • Loading branch information
alexpasmantier authored Feb 8, 2025
1 parent b706dcb commit 86c100e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion television/channels/cable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum PreviewKind {

#[allow(dead_code)]
pub struct Channel {
name: String,
pub name: String,
matcher: Matcher<String>,
entries_command: String,
preview_kind: PreviewKind,
Expand Down
8 changes: 8 additions & 0 deletions television/channels/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 7 additions & 9 deletions television/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,21 +24,21 @@ use crate::{

#[derive(Debug, Clone, PartialEq)]
pub struct ChannelState {
pub current_channel: UnitChannel,
pub current_channel_name: String,
pub selected_entries: FxHashSet<Entry>,
pub total_count: u32,
pub running: bool,
}

impl ChannelState {
pub fn new(
current_channel: UnitChannel,
current_channel_name: String,
selected_entries: FxHashSet<Entry>,
total_count: u32,
running: bool,
) -> Self {
Self {
current_channel,
current_channel_name,
selected_entries,
total_count,
running,
Expand All @@ -51,7 +48,7 @@ impl ChannelState {

impl Hash for ChannelState {
fn hash<H: std::hash::Hasher>(&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));
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)?;
Expand Down
17 changes: 10 additions & 7 deletions television/screen/help.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
) {
Expand All @@ -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);
}
Expand All @@ -79,7 +82,7 @@ fn draw_keymaps_block(
pub fn draw_help_bar(
f: &mut Frame,
layout: &Option<HelpBarLayout>,
current_channel: UnitChannel,
current_channel_name: &str,
keymap_table: Table,
mode: Mode,
app_metadata: &AppMetadata,
Expand All @@ -90,7 +93,7 @@ pub fn draw_help_bar(
f,
help_bar.left,
mode,
current_channel,
current_channel_name,
app_metadata,
colorscheme,
);
Expand Down
9 changes: 7 additions & 2 deletions television/screen/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,13 +21,19 @@ pub fn draw_input_box(
input_state: &Input,
results_picker_state: &ListState,
matcher_running: bool,
channel_name: &str,
spinner: &Spinner,
colorscheme: &Colorscheme,
) -> Result<()> {
let input_block = Block::default()
.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()),
Expand Down
5 changes: 2 additions & 3 deletions television/screen/metadata.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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> {
Expand Down Expand Up @@ -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),
)),
]);
Expand Down
2 changes: 1 addition & 1 deletion television/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 86c100e

Please sign in to comment.