Skip to content

Commit

Permalink
Add adjustable margins and fix margins on menu titles
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-hughes committed May 20, 2021
1 parent 295cc85 commit b32c825
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
32 changes: 15 additions & 17 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,18 @@ impl Menuable for Podcast {
// to the end
if length > crate::config::PODCAST_UNPLAYED_TOTALS_LENGTH {
let meta_str = format!("({}/{})", self.num_unplayed(), self.episodes.len());
title_length = length - meta_str.chars().count();
title_length = length - meta_str.chars().count() - 2;

let out = self.title.substr(0, title_length);

return format!(
"{} {:>width$}",
" {} {:>width$} ",
out,
meta_str,
width = length - out.grapheme_len()
);
// this pads spaces between title and totals
width = length - out.grapheme_len() - 2
); // this pads spaces between title and totals
} else {
return self.title.substr(0, title_length);
return format!(" {} ", self.title.substr(0, title_length - 2));
}
}

Expand Down Expand Up @@ -157,7 +156,6 @@ impl Menuable for Episode {
}
None => self.title.substr(0, length),
};
let out_len = out.grapheme_len();
if length > crate::config::EPISODE_PUBDATE_LENGTH {
let dur = self.format_duration();
let meta_dur = format!("[{}]", dur);
Expand All @@ -168,35 +166,35 @@ impl Menuable for Episode {
let meta_str = format!("({}) {}", pd, meta_dur);
let added_len = meta_str.chars().count();

let out_added = out.substr(0, length - added_len);
let out_added = out.substr(0, length - added_len - 2);
return format!(
"{} {:>width$}",
" {} {:>width$} ",
out_added,
meta_str,
width = length - out_len
width = length - out_added.grapheme_len() - 2
);
} else {
// just print duration
let out_added = out.substr(0, length - meta_dur.chars().count());
let out_added = out.substr(0, length - meta_dur.chars().count() - 2);
return format!(
"{} {:>width$}",
" {} {:>width$} ",
out_added,
meta_dur,
width = length - out_len
width = length - out_added.grapheme_len() - 2
);
}
} else if length > crate::config::EPISODE_DURATION_LENGTH {
let dur = self.format_duration();
let meta_dur = format!("[{}]", dur);
let out_added = out.substr(0, length - meta_dur.chars().count());
let out_added = out.substr(0, length - meta_dur.chars().count() - 2);
return format!(
"{} {:>width$}",
" {} {:>width$} ",
out_added,
meta_dur,
width = length - out_len
width = length - out_added.grapheme_len() - 2
);
} else {
return out;
return format!(" {} ", out.substr(0, length - 2));
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl<'a> Ui<'a> {
n_row - 1,
pod_col,
0,
(0, 0, 0, 0),
);
let podcast_menu = Menu::new(podcast_panel, None, items);

Expand All @@ -193,6 +194,7 @@ impl<'a> Ui<'a> {
n_row - 1,
ep_col,
pod_col - 1,
(0, 0, 0, 0),
);

let episode_menu = Menu::new(episode_panel, None, first_pod);
Expand Down Expand Up @@ -824,7 +826,15 @@ impl<'a> Ui<'a> {
n_col: u16,
start_x: u16,
) -> Panel {
return Panel::new("Details".to_string(), 2, colors, n_row, n_col, start_x);
return Panel::new(
"Details".to_string(),
2,
colors,
n_row,
n_col,
start_x,
(0, 1, 0, 1),
);
}

/// Updates the details panel with information about the current
Expand Down
41 changes: 19 additions & 22 deletions src/ui/panel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io;
use std::rc::Rc;
use std::{convert::TryInto, io};

use chrono::{DateTime, Utc};
use crossterm::{cursor, queue, style};
Expand Down Expand Up @@ -30,9 +30,10 @@ pub struct Details {
/// Panels abstract away a pancurses window, and handles all methods
/// associated with writing data to that window. A panel includes a
/// border and margin around the edge of the window, and a title that
/// appears at the top. The Panel will translate the x and y coordinates
/// to account for the border and margins, so users of the methods can
/// calculate rows and columns relative to the Panel.
/// appears at the top. Margins are set individually, in the order
/// (top, right, bottom, left). The Panel will translate the x and y
/// coordinates to account for the border and margins, so users of the
/// methods can calculate rows and columns relative to the Panel.
#[derive(Debug)]
pub struct Panel {
screen_pos: usize,
Expand All @@ -41,6 +42,7 @@ pub struct Panel {
start_x: u16,
n_row: u16,
n_col: u16,
margins: (u16, u16, u16, u16),
}

impl Panel {
Expand All @@ -52,6 +54,7 @@ impl Panel {
n_row: u16,
n_col: u16,
start_x: u16,
margins: (u16, u16, u16, u16),
) -> Self {
return Panel {
screen_pos: screen_pos,
Expand All @@ -60,6 +63,7 @@ impl Panel {
start_x: start_x,
n_row: n_row,
n_col: n_col,
margins: margins,
};
}

Expand Down Expand Up @@ -175,7 +179,7 @@ impl Panel {
};
queue!(
io::stdout(),
cursor::MoveTo(self.abs_x(0), self.abs_y(y as i16)),
cursor::MoveTo(self.abs_x(0), self.abs_y(y)),
style::PrintStyledContent(styled)
)
.unwrap();
Expand All @@ -198,11 +202,7 @@ impl Panel {
key.push(':');
value.insert(0, ' ');

queue!(
io::stdout(),
cursor::MoveTo(self.abs_x(0), self.abs_y(y as i16))
)
.unwrap();
queue!(io::stdout(), cursor::MoveTo(self.abs_x(0), self.abs_y(y))).unwrap();

let key_styled = match key_style {
Some(kstyle) => kstyle.apply(key),
Expand Down Expand Up @@ -242,7 +242,7 @@ impl Panel {
for line in wrapper {
queue!(
io::stdout(),
cursor::MoveTo(self.abs_x(0), self.abs_y(row as i16)),
cursor::MoveTo(self.abs_x(0), self.abs_y(row)),
style::PrintStyledContent(content_style.apply(line))
)
.unwrap();
Expand Down Expand Up @@ -360,29 +360,26 @@ impl Panel {
/// Returns the effective number of rows (accounting for borders
/// and margins).
pub fn get_rows(&self) -> u16 {
return self.n_row - 2; // border on top and bottom
// 2 for border on top and bottom
return self.n_row - self.margins.0 - self.margins.2 - 2;
}

/// Returns the effective number of columns (accounting for
/// borders and margins).
pub fn get_cols(&self) -> u16 {
return self.n_col - 5; // 2 for border, 2 for margins, and 1
// extra for some reason...
// 2 for border, and 1 extra for some reason...
return self.n_col - self.margins.1 - self.margins.3 - 3;
}

/// Calculates the y-value relative to the terminal rather than to
/// the panel (i.e., taking into account borders and margins).
fn abs_y(&self, y: i16) -> u16 {
return (y + 1)
.try_into()
.expect("Can't convert signed integer to unsigned");
fn abs_y(&self, y: u16) -> u16 {
return y + self.margins.0 + 1;
}

/// Calculates the x-value relative to the terminal rather than to
/// the panel (i.e., taking into account borders and margins).
fn abs_x(&self, x: i16) -> u16 {
return (x + self.start_x as i16 + 2)
.try_into()
.expect("Can't convert signed integer to unsigned");
fn abs_x(&self, x: u16) -> u16 {
return x + self.start_x + self.margins.3 + 1;
}
}
5 changes: 4 additions & 1 deletion src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ impl<'a> PopupWin<'a> {
self.total_rows - 1,
self.total_cols,
0,
(1, 1, 1, 1),
);
welcome_win.redraw();

let mut row = 0;
row = welcome_win.write_wrap_line(row + 1, "Welcome to shellcaster!", None);
row = welcome_win.write_wrap_line(row, "Welcome to shellcaster!", None);

row = welcome_win.write_wrap_line(row + 2,
&format!("Your podcast list is currently empty. Press {} to add a new podcast feed, {} to quit, or see all available commands by typing {} to get help.", key_strs[0], key_strs[1], key_strs[2]), None);
Expand Down Expand Up @@ -224,6 +225,7 @@ impl<'a> PopupWin<'a> {
self.total_rows - 1,
self.total_cols,
0,
(1, 1, 1, 1),
);
help_win.redraw();

Expand Down Expand Up @@ -305,6 +307,7 @@ impl<'a> PopupWin<'a> {
self.total_rows - 1,
self.total_cols,
0,
(0, 0, 0, 0),
);

let header = format!(
Expand Down

0 comments on commit b32c825

Please sign in to comment.