mirror of
https://github.com/talwat/lowfi
synced 2025-04-28 13:33:22 +00:00
feat: readd padding to borderless mode
This commit is contained in:
parent
1e491bb36f
commit
6a6823d078
@ -61,7 +61,7 @@ struct Args {
|
|||||||
|
|
||||||
/// Whether to not include borders in the UI.
|
/// Whether to not include borders in the UI.
|
||||||
#[clap(long, short)]
|
#[clap(long, short)]
|
||||||
no_borders: bool,
|
borderless: bool,
|
||||||
|
|
||||||
/// Whether to start lowfi paused.
|
/// Whether to start lowfi paused.
|
||||||
#[clap(long, short)]
|
#[clap(long, short)]
|
||||||
|
@ -53,11 +53,14 @@ lazy_static! {
|
|||||||
/// The main purpose of this struct is just to add the fancy border,
|
/// The main purpose of this struct is just to add the fancy border,
|
||||||
/// as well as clear the screen before drawing.
|
/// as well as clear the screen before drawing.
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
|
// Whether or not to include borders in the output.
|
||||||
|
borderless: bool,
|
||||||
|
|
||||||
/// The top & bottom borders, which are here since they can be
|
/// The top & bottom borders, which are here since they can be
|
||||||
/// prerendered, as they don't change from window to window.
|
/// prerendered, as they don't change from window to window.
|
||||||
///
|
///
|
||||||
/// [None] if the option to not include windows is set.
|
/// If the option to not include borders is set, these will just be empty [String]s.
|
||||||
borders: Option<[String; 2]>,
|
borders: [String; 2],
|
||||||
|
|
||||||
/// The output, currently just an [`Stdout`].
|
/// The output, currently just an [`Stdout`].
|
||||||
out: Stdout,
|
out: Stdout,
|
||||||
@ -67,18 +70,19 @@ impl Window {
|
|||||||
/// Initializes a new [Window].
|
/// Initializes a new [Window].
|
||||||
///
|
///
|
||||||
/// * `width` - Width of the windows.
|
/// * `width` - Width of the windows.
|
||||||
/// * `borders` - Whether to include borders in the window, or not.
|
/// * `borderless` - Whether to include borders in the window, or not.
|
||||||
pub fn new(width: usize, borders: bool) -> Self {
|
pub fn new(width: usize, borderless: bool) -> Self {
|
||||||
let borders = borders.then(|| {
|
let borders = if !borderless {
|
||||||
[
|
let middle = "─".repeat(width + 2);
|
||||||
format!("┌{}┐\r\n", "─".repeat(width + 2)),
|
|
||||||
// This one doesn't have a leading \r\n to avoid extra space under the window.
|
[format!("┌{middle}┐"), format!("└{middle}┘")]
|
||||||
format!("└{}┘", "─".repeat(width + 2)),
|
} else {
|
||||||
]
|
[String::new(), String::new()]
|
||||||
});
|
};
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
borders,
|
borders,
|
||||||
|
borderless,
|
||||||
out: stdout(),
|
out: stdout(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,12 +91,11 @@ impl Window {
|
|||||||
pub fn draw(&mut self, content: Vec<String>) -> eyre::Result<()> {
|
pub fn draw(&mut self, content: Vec<String>) -> eyre::Result<()> {
|
||||||
let len = content.len() as u16;
|
let len = content.len() as u16;
|
||||||
|
|
||||||
|
// Note that this will have a trailing newline, which we use later.
|
||||||
let menu: String = content.into_iter().fold(String::new(), |mut output, x| {
|
let menu: String = content.into_iter().fold(String::new(), |mut output, x| {
|
||||||
if self.borders.is_some() {
|
// Horizontal Padding & Border
|
||||||
write!(output, "│ {} │\r\n", x.reset()).unwrap();
|
let padding = if !self.borderless { "│" } else { " " };
|
||||||
} else {
|
write!(output, "{padding} {} {padding}\r\n", x.reset()).unwrap();
|
||||||
write!(output, "{}\r\n", x.reset()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
output
|
output
|
||||||
});
|
});
|
||||||
@ -100,23 +103,12 @@ impl Window {
|
|||||||
// We're doing this because Windows is stupid and can't stand
|
// We're doing this because Windows is stupid and can't stand
|
||||||
// writing to the last line repeatedly.
|
// writing to the last line repeatedly.
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
let output_len = len;
|
let (height, suffix) = (len + 2, "\r\n");
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
let output_len = len - 1;
|
let (height, suffix) = (len + 1, "");
|
||||||
|
|
||||||
let (mut rendered, height) = self.borders.as_ref().map_or_else(
|
// There's no need for another newline after the main menu content, because it already has one.
|
||||||
|| (menu.trim().to_owned(), output_len),
|
let rendered = format!("{}\r\n{menu}{}{suffix}", self.borders[0], self.borders[1]);
|
||||||
|borders| {
|
|
||||||
(
|
|
||||||
format!("{}{}{}", borders[0], menu, borders[1]),
|
|
||||||
output_len + 2,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Similar reasoning to the previous comment defining `output_len`.
|
|
||||||
#[cfg(windows)]
|
|
||||||
rendered.push_str("\r\n");
|
|
||||||
|
|
||||||
crossterm::execute!(
|
crossterm::execute!(
|
||||||
self.out,
|
self.out,
|
||||||
@ -134,15 +126,15 @@ impl Window {
|
|||||||
/// The code for the terminal interface itself.
|
/// The code for the terminal interface itself.
|
||||||
///
|
///
|
||||||
/// * `minimalist` - All this does is hide the bottom control bar.
|
/// * `minimalist` - All this does is hide the bottom control bar.
|
||||||
/// * `borders` - Whether to include borders or not.
|
/// * `borderless` - Whether to include borders or not.
|
||||||
/// * `width` - The width of player
|
/// * `width` - The width of player
|
||||||
async fn interface(
|
async fn interface(
|
||||||
player: Arc<Player>,
|
player: Arc<Player>,
|
||||||
minimalist: bool,
|
minimalist: bool,
|
||||||
borders: bool,
|
borderless: bool,
|
||||||
width: usize,
|
width: usize,
|
||||||
) -> eyre::Result<()> {
|
) -> eyre::Result<()> {
|
||||||
let mut window = Window::new(width, borders);
|
let mut window = Window::new(width, borderless);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Load `current` once so that it doesn't have to be loaded over and over
|
// Load `current` once so that it doesn't have to be loaded over and over
|
||||||
@ -261,7 +253,7 @@ pub async fn start(player: Arc<Player>, sender: Sender<Messages>, args: Args) ->
|
|||||||
let interface = task::spawn(interface(
|
let interface = task::spawn(interface(
|
||||||
Arc::clone(&player),
|
Arc::clone(&player),
|
||||||
args.minimalist,
|
args.minimalist,
|
||||||
!args.no_borders,
|
args.borderless,
|
||||||
21 + args.width.min(32) * 2,
|
21 + args.width.min(32) * 2,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user