Skip to content

Commit

Permalink
added key bindings bar
Browse files Browse the repository at this point in the history
  • Loading branch information
gilcu3 committed Mar 21, 2024
1 parent d517572 commit 8122f85
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 20 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- [ ] Syncing with gpodder api
- [ ] Queue
- [ ] Internal Player
- [x] Show key bindings in a bat on the bottom
80 changes: 80 additions & 0 deletions src/ui/keybindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::io;
use std::rc::Rc;

use crossterm::{cursor, queue, style, style::Stylize};

use crate::keymap::{Keybindings, UserAction};

use super::AppColors;

#[derive(Debug)]
pub struct KeybindingsWin<'a> {
keymap: &'a Keybindings,
colors: Rc<AppColors>,
start_y: u16,
total_rows: u16,
total_cols: u16,
}

impl<'a> KeybindingsWin<'a> {
pub fn new(
keymap: &'a Keybindings, colors: Rc<AppColors>, start_y: u16, total_rows: u16,
total_cols: u16,
) -> Self {
Self {
keymap,
colors,
start_y,
total_rows,
total_cols,
}
}

pub fn redraw(&self) {
let actions = vec![
(UserAction::Quit, "Quit"),
(UserAction::Help, "Help"),
(UserAction::Play, "Play"),
(UserAction::SyncAll, "Sync all"),
(UserAction::MarkPlayed, "Mark as played"),
(UserAction::MarkAllPlayed, "Mark all as played"),
(UserAction::Download, "Download"),
(UserAction::Delete, "Delete file"),
(UserAction::AddFeed, "Add feed"),
];
let mut key_strs = Vec::new();
for (action, action_str) in actions {
let keys = self.keymap.keys_for_action(action);
// longest prefix is 21 chars long
let key_str = match keys.len() {
0 => format!(":{}", action_str),
_ => format!("{}:{}", &keys[0], action_str,),
};
key_strs.push(key_str);
}
let message0 = key_strs.join(" | ");
let m0len = if self.total_cols as usize >= message0.len() {
self.total_cols as usize - message0.len()
} else {
0
};
let message = message0 + &" ".repeat(m0len);
queue!(
io::stdout(),
cursor::MoveTo(0, self.start_y),
style::PrintStyledContent(
style::style(&message)
.with(self.colors.normal.0)
.on(self.colors.normal.1)
)
)
.unwrap();
}

pub fn resize(&mut self, total_rows: u16, total_cols: u16) {
self.total_rows = total_rows;
self.total_cols = total_cols;

self.redraw();
}
}
6 changes: 4 additions & 2 deletions src/ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,15 @@ mod tests {

fn create_menu(n_row: u16, n_col: u16, top_row: u16, selected: u16) -> Menu<Episode> {
let colors = Rc::new(crate::ui::AppColors::default());
let titles = ["A Very Cool Episode",
let titles = [
"A Very Cool Episode",
"This is a very long episode title but we'll get through it together",
"An episode with le Unicodé",
"How does an episode with emoji sound? 😉",
"Here's another title",
"Un titre, c'est moi!",
"One more just for good measure"];
"One more just for good measure",
];
let mut items = Vec::new();
for (i, t) in titles.iter().enumerate() {
let played = i % 2 == 0;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/mock_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ impl Panel {
self.buffer.push(String::new());
}
}
Ordering::Less => self.buffer.truncate(new_len),
Ordering::Equal => {},
Ordering::Less => self.buffer.truncate(new_len),
Ordering::Equal => {}
}
}

Expand Down
32 changes: 21 additions & 11 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ mod panel;

pub mod colors;
mod details_panel;
mod keybindings;
mod menu;
mod notification;
mod popup;

use self::colors::AppColors;
use self::details_panel::{Details, DetailsPanel};
use self::keybindings::KeybindingsWin;
use self::menu::Menu;
use self::notification::NotifWin;
use self::panel::Panel;
Expand Down Expand Up @@ -104,6 +106,7 @@ pub struct Ui<'a> {
active_panel: ActivePanel,
notif_win: NotifWin,
popup_win: PopupWin<'a>,
keybindings_win: KeybindingsWin<'a>,
}

impl<'a> Ui<'a> {
Expand Down Expand Up @@ -188,7 +191,7 @@ impl<'a> Ui<'a> {
"Podcasts".to_string(),
0,
colors.clone(),
n_row - 1,
n_row - 2,
pod_col,
0,
(0, 0, 0, 0),
Expand All @@ -199,7 +202,7 @@ impl<'a> Ui<'a> {
"Episodes".to_string(),
1,
colors.clone(),
n_row - 1,
n_row - 2,
ep_col,
pod_col - 1,
(0, 0, 0, 0),
Expand All @@ -212,7 +215,7 @@ impl<'a> Ui<'a> {
"Details".to_string(),
2,
colors.clone(),
n_row - 1,
n_row - 2,
det_col,
pod_col + ep_col - 2,
(0, 1, 0, 1),
Expand All @@ -221,8 +224,11 @@ impl<'a> Ui<'a> {
None
};

let notif_win = NotifWin::new(colors.clone(), n_row - 1, n_row, n_col);
let popup_win = PopupWin::new(&config.keybindings, colors.clone(), n_row, n_col);
let notif_win = NotifWin::new(colors.clone(), n_row - 2, n_row, n_col);
let popup_win = PopupWin::new(&config.keybindings, colors.clone(), n_row - 1, n_col);

let keybindings_win =
KeybindingsWin::new(&config.keybindings, colors.clone(), n_row - 1, n_row, n_col);

Ui {
n_row,
Expand All @@ -235,6 +241,7 @@ impl<'a> Ui<'a> {
active_panel: ActivePanel::PodcastMenu,
notif_win,
popup_win,
keybindings_win,
}
}

Expand All @@ -244,9 +251,11 @@ impl<'a> Ui<'a> {
self.podcast_menu.redraw();
self.episode_menu.redraw();
self.podcast_menu.activate();

self.update_details_panel();

self.notif_win.redraw();
self.keybindings_win.redraw();

// welcome screen if user does not have any podcasts yet
if self.podcast_menu.items.is_empty() {
Expand Down Expand Up @@ -430,14 +439,14 @@ impl<'a> Ui<'a> {

let (pod_col, ep_col, det_col) = Self::calculate_sizes(n_col);

self.podcast_menu.resize(n_row - 1, pod_col, 0);
self.episode_menu.resize(n_row - 1, ep_col, pod_col - 1);
self.podcast_menu.resize(n_row - 2, pod_col, 0);
self.episode_menu.resize(n_row - 2, ep_col, pod_col - 1);
self.highlight_items();

if self.details_panel.is_some() {
if det_col > 0 {
let det = self.details_panel.as_mut().unwrap();
det.resize(n_row - 1, det_col, pod_col + ep_col - 2);
det.resize(n_row - 2, det_col, pod_col + ep_col - 2);
// resizing the menus may change which item is selected
self.update_details_panel();
} else {
Expand All @@ -455,16 +464,17 @@ impl<'a> Ui<'a> {
"Details".to_string(),
2,
self.colors.clone(),
n_row - 1,
n_row - 2,
det_col,
pod_col + ep_col - 2,
(0, 1, 0, 1),
));
self.update_details_panel();
}

self.popup_win.resize(n_row, n_col);
self.notif_win.resize(n_row, n_col);
self.popup_win.resize(n_row - 1, n_col);
self.notif_win.resize(n_row - 1, n_col);
self.keybindings_win.resize(n_row, n_col);
}

/// Move the menu cursor around and redraw menus when necessary.
Expand Down
6 changes: 1 addition & 5 deletions src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ impl<'a> PopupWin<'a> {
"More details of how to customize shellcaster can be found on the Github repo readme:",
None,
);
let _ = welcome_win.write_wrap_line(
row + 1,
"https://github.com/gilcu3/shellcaster",
None,
);
let _ = welcome_win.write_wrap_line(row + 1, "https://github.com/gilcu3/shellcaster", None);

welcome_win
}
Expand Down

0 comments on commit 8122f85

Please sign in to comment.