Skip to content

Commit

Permalink
Refactor 'ui::help' in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Sep 1, 2023
1 parent d42a895 commit a31db62
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 91 deletions.
191 changes: 100 additions & 91 deletions endsong_ui/src/ui/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use unicode_width::UnicodeWidthStr;

use super::Color;

/// Represents a command
///
/// Fields are name, alias, description
#[derive(Copy, Clone, Debug)]
struct Command(&'static str, &'static str, &'static str);

/// Used by [`match_input()`][`super::match_input()`] for `help` command
///
/// Prints the available commands to the [`std::io::stdout`]
Expand All @@ -15,38 +21,41 @@ pub fn help() {

// PRINT COMMANDS
print("print", print_commands());

print("print top", print_top_commands());

// GRAPH COMMANDS
print("graph/plot", plot_commands());
}

/// Prints the commands
fn print(title: &str, commands: &[[&str; 3]]) {
println!(
// "{title} commands" has to be centered to "=>"
// so columns 22 and 23
// everything before that has to be filled with '='
// everything after that has to be filled with '=' till column 50
"{}{}{}",
Color::LightGreen,
center_phrase(title, 22, 50),
Color::Reset
);
for command in commands {
fn print(title: &str, commands: &[Command]) {
/// Base
const BASE_LENGTH: usize = 20;

/// Length of " => "
const ARROW_LENGTH: usize = " => ".len();

/// Length of "alias "
const ALIAS_LENGTH: usize = "alias: ".len();

// "{title} commands" has to be centered to "=>"
// so columns 22 and 23
// everything before that has to be filled with '='
// everything after that has to be filled with '=' till column 50
let centered_title = center_phrase(title, 22, 50);

let reset = Color::Reset;
let (light_green, red, pink) = (Color::LightGreen, Color::Red, Color::Pink);

println!("{light_green}{centered_title}{reset}");

let alias_pre = adjust_length("alias: ", BASE_LENGTH + ARROW_LENGTH + ALIAS_LENGTH);
for Command(command, alias, description) in commands {
println!(
"{}{}{} => {}\n{}{}{}{}",
Color::Red,
adjust_length(command[0], 20),
Color::Reset,
// 20 (see above) - 4 (see below) ????
prepend_spaces_for_newline(command[2], 16),
Color::Pink,
// 20 see above, 4 length of " => ", 7 length of "alias: "
adjust_length("alias: ", 20 + 4 + 7),
command[1],
Color::Reset
"{red}{}{reset} => {}\n{pink}{alias_pre}{alias}{reset}",
adjust_length(command, BASE_LENGTH),
// 20 (see above) - 4 (see below) ???? - 4 (implied in `description`)
prepend_spaces_for_newline(description, BASE_LENGTH - ARROW_LENGTH - 4),
);
}
}
Expand Down Expand Up @@ -122,134 +131,134 @@ fn center_phrase(phrase: &str, start: usize, end: usize) -> String {
}

/// Returns meta commands
const fn meta_commands<'a>() -> &'a [[&'a str; 3]] {
const fn meta_commands() -> &'static [Command] {
&[
["help", "h", "prints this command list"],
["exit", "quit", "exits the program"],
Command("help", "h", "prints this command list"),
Command("exit", "quit", "exits the program"),
]
}

/// Returns print commands
const fn print_commands<'a>() -> &'a [[&'a str; 3]] {
const fn print_commands() -> &'static [Command] {
&[
["print time", "pt", "prints the total time spent listening"],
[
Command("print time", "pt", "prints the total time spent listening"),
Command(
"print time date",
"ptd",
"prints the time spent listening in a specific date range
opens another prompt where you input the date range",
],
[
opens another prompt where you input the date range",
),
Command(
"print max time",
"pmt",
"calculates the dates during which you listened
the most to music for a given duration",
],
[
the most to music for a given duration",
),
Command(
"print artist",
"part",
"prints every album from the artist
opens another prompt where you input the artist name",
],
[
opens another prompt where you input the artist name",
),
Command(
"print album",
"palb",
"prints every song from the album
opens another prompt where you input the artist name
and then the album name",
],
[
opens another prompt where you input the artist name
and then the album name",
),
Command(
"print song",
"pson",
"prints a song
opens another prompt where you input the artist name
and then the album name
and then the song name",
],
[
opens another prompt where you input the artist name
and then the album name
and then the song name",
),
Command(
"print songs",
"psons",
"prints a song with all the albums it may be from
opens another prompt where you input the artist name
and then the song name",
],
[
opens another prompt where you input the artist name
and then the song name",
),
Command(
"print artist date",
"partd",
"prints every album from the artist within a date range
opens another prompt where you input the artist name
and then the date range",
],
[
opens another prompt where you input the artist name
and then the date range",
),
Command(
"print album date",
"palbd",
"prints every song from the album within a date range
opens another prompt where you input the artist name
and then the album name",
],
[
opens another prompt where you input the artist name
and then the album name",
),
Command(
"print song date",
"psond",
"prints a song within a date range
opens another prompt where you input the artist name
and then the album name
and then the song name
and then the date range",
],
[
opens another prompt where you input the artist name
and then the album name
and then the song name
and then the date range",
),
Command(
"print songs date",
"psonsd",
"prints a song with all the albums it may be from within a date range
opens another prompt where you input the artist name
and then the song name
and then the date range",
],
opens another prompt where you input the artist name
and then the song name
and then the date range",
),
]
}

/// Returns print top commands
const fn print_top_commands<'a>() -> &'a [[&'a str; 3]] {
const fn print_top_commands() -> &'static [Command] {
&[
["print top artists", "ptarts", "prints top n artists"],
["print top albums", "ptalbs", "prints top n albums"],
["print top songs", "ptsons", "prints top n songs"],
Command("print top artists", "ptarts", "prints top n artists"),
Command("print top albums", "ptalbs", "prints top n albums"),
Command("print top songs", "ptsons", "prints top n songs"),
]
}

/// Returns graph commands
const fn plot_commands<'a>() -> &'a [[&'a str; 3]] {
const fn plot_commands() -> &'static [Command] {
&[
[
Command(
"plot",
"g",
"creates a plot of the absolute amount of plays of the given aspect
and opens it in the web browser",
],
[
and opens it in the web browser",
),
Command(
"plot rel",
"gr",
"creates a plot of the amount of plays of the given aspect relative
to all, the artist or album
and opens it in the web browser",
],
[
to all, the artist or album
and opens it in the web browser",
),
Command(
"plot compare",
"gc",
"creates a plot of two traces - see `plot`
and opens it in the web browser",
],
[
and opens it in the web browser",
),
Command(
"plot compare rel",
"gcr",
"creates a plot of two relative traces - see `plot rel`
and opens it in the web browser",
],
[
and opens it in the web browser",
),
Command(
"plot top",
"gt",
"creates a plot of the absolute traces of top n aspects
and opens it in the web browser",
],
and opens it in the web browser",
),
]
}

Expand Down
1 change: 1 addition & 0 deletions endsong_ui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl Completer for ShellHelper {
/// ANSI Colors
///
/// See <https://bixense.com/clicolors>
#[derive(Debug, Copy, Clone)]
enum Color {
/// Resets the following text with `\x1b[0m`
Reset,
Expand Down

0 comments on commit a31db62

Please sign in to comment.