Skip to content

Commit

Permalink
#73 improve artist summary page
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Sep 29, 2024
1 parent 16eda73 commit 47e9b87
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 119 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions endsong_ui/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions endsong_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,66 @@ pub const fn spaces(num: usize) -> &'static str {
endsong_macros::generate_spaces_match!(100)
}

/// Replaces Windows forbidden symbols in path with an '_'
///
/// Also removes whitespace and replaces empty
/// strings with '_'
#[must_use]
pub fn normalize_path(path: &str) -> String {
// https://stackoverflow.com/a/31976060
// Array > HashSet bc of overhead
let forbidden_characters = [' ', '<', '>', ':', '"', '/', '\\', '|', '?', '*'];
let mut new_path = String::with_capacity(path.len());

for ch in path.chars() {
if forbidden_characters.contains(&ch) {
// replace a forbidden symbol with an underscore (for now...)
new_path.push('_');
} else {
new_path.push(ch);
}
}

// https://stackoverflow.com/a/1976050
if new_path.is_empty() || new_path == "." {
new_path = "_".into();
}

new_path
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn normalize_paths() {
// should change the forbidden symbols to '_' in these
assert_eq!(normalize_path("A|B"), "A_B");
assert_eq!(normalize_path("A>B<C"), "A_B_C");
assert_eq!(normalize_path(":A\"B"), "_A_B");
assert_eq!(normalize_path("A/B"), normalize_path("A\\B"));
assert_eq!(normalize_path("?A?"), "_A_");
assert_eq!(normalize_path("A*B"), "A_B");

// whitespace should be removed
assert_eq!(normalize_path(" A"), "_A");
assert_eq!(normalize_path("A "), "A_");
assert_eq!(normalize_path(" "), "_");
assert_eq!(normalize_path(" "), "___");

// empty/only dot should be changed (Windows)
assert_eq!(normalize_path(""), "_");
assert_eq!(normalize_path("."), "_");

assert_eq!(normalize_path(" A|B<>B? "), "_A_B__B__");

// shouldn't change anything about these
assert_eq!(normalize_path("A_B"), "A_B");
assert_eq!(normalize_path("AB"), "AB");
}
}

/// Prelude containing all the modules,
/// a function for parsing dates, some structs used for printing,
/// and a trait to add a [pretty display method][print::DurationUtils::display]
Expand Down
4 changes: 1 addition & 3 deletions endsong_ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ fn main() {
// test_two(&entries);
// test_plot(&entries);

summarize::artist(&entries, &Artist::new("Sabaton"));

// ui::start(&entries);
ui::start(&entries);
}

/// tests various [`print`][crate::print] and [`endsong::gather`] functions
Expand Down
61 changes: 1 addition & 60 deletions endsong_ui/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn write_and_open_plot(plot: &Plot, title: &str) {
// creates plots/ folder
std::fs::create_dir_all("plots").unwrap();

let title = normalize_path(title);
let title = crate::normalize_path(title);

// opens the plot in the browser
match std::env::consts::OS {
Expand Down Expand Up @@ -109,62 +109,3 @@ fn write_and_open_plot(plot: &Plot, title: &str) {
}
};
}

/// Replaces Windows forbidden symbols in path with an '_'
///
/// Also removes whitespace and replaces empty
/// strings with '_'
fn normalize_path(path: &str) -> String {
// https://stackoverflow.com/a/31976060
// Array > HashSet bc of overhead
let forbidden_characters = [' ', '<', '>', ':', '"', '/', '\\', '|', '?', '*'];
let mut new_path = String::with_capacity(path.len());

for ch in path.chars() {
if forbidden_characters.contains(&ch) {
// replace a forbidden symbol with an underscore (for now...)
new_path.push('_');
} else {
new_path.push(ch);
}
}

// https://stackoverflow.com/a/1976050
if new_path.is_empty() || new_path == "." {
new_path = "_".into();
}

new_path
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn normalize_paths() {
// should change the forbidden symbols to '_' in these
assert_eq!(normalize_path("A|B"), "A_B");
assert_eq!(normalize_path("A>B<C"), "A_B_C");
assert_eq!(normalize_path(":A\"B"), "_A_B");
assert_eq!(normalize_path("A/B"), normalize_path("A\\B"));
assert_eq!(normalize_path("?A?"), "_A_");
assert_eq!(normalize_path("A*B"), "A_B");

// whitespace should be removed
assert_eq!(normalize_path(" A"), "_A");
assert_eq!(normalize_path("A "), "A_");
assert_eq!(normalize_path(" "), "_");
assert_eq!(normalize_path(" "), "___");

// empty/only dot should be changed (Windows)
assert_eq!(normalize_path(""), "_");
assert_eq!(normalize_path("."), "_");

assert_eq!(normalize_path(" A|B<>B? "), "_A_B__B__");

// shouldn't change anything about these
assert_eq!(normalize_path("A_B"), "A_B");
assert_eq!(normalize_path("AB"), "AB");
}
}
Loading

0 comments on commit 47e9b87

Please sign in to comment.