From 1f9056caac48b4ebee9f0c1759b5a84616923b9e Mon Sep 17 00:00:00 2001 From: Brage Fuglseth Date: Wed, 25 Oct 2023 21:55:22 +0200 Subject: [PATCH] fix: improve chord prettifier --- src/chord_name_entry.rs | 9 +++++---- src/chord_ops.rs | 29 +++++++++++++---------------- src/window.rs | 14 ++++++-------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/chord_name_entry.rs b/src/chord_name_entry.rs index 07ca968..b1cc588 100644 --- a/src/chord_name_entry.rs +++ b/src/chord_name_entry.rs @@ -1,8 +1,8 @@ +use crate::chord_ops::{prettify_chord_name, serialize_chord_name}; use adw::subclass::prelude::*; use gtk::glib; use gtk::prelude::*; use std::cell::RefCell; -use crate::chord_ops::{prettify_chord_name, serialize_chord_name}; mod imp { use super::*; @@ -50,15 +50,16 @@ mod imp { entry_wrapper.revealer.set_reveal_child(changed); })); - self.entry - .connect_activate(glib::clone!(@weak revealer, @weak self as entry => move |_| { + self.entry.connect_activate( + glib::clone!(@weak revealer, @weak self as entry => move |_| { revealer.set_visible(false); revealer.set_reveal_child(false); let prettified_name = prettify_chord_name(&entry.entry.text()); entry.obj().overwrite_text(&prettified_name); entry.entry.set_position(-1); - })); + }), + ); let entry = self.entry.get(); diff --git a/src/chord_ops.rs b/src/chord_ops.rs index c73b9bd..84506d9 100644 --- a/src/chord_ops.rs +++ b/src/chord_ops.rs @@ -100,25 +100,22 @@ pub fn adjust_chord(chord: [Option; 6], barre: u8) -> [Option; 6] } pub fn prettify_chord_name(input: &str) -> String { - input - .replace(" ", "") - .to_ascii_lowercase() - .split('/') - .map(|part| { - part.chars() - .enumerate() - .map(|tuple| match tuple { - (0, c) => c.to_ascii_uppercase(), - (1|2, '#') => '♯', - (1|2, 'b') => '♭', - (_, c) => c, - }) - .collect::() + std::iter::once(' ') + .chain(input.chars().filter(|c| !c.is_ascii_whitespace())) + .map(|c| c.to_ascii_lowercase()) + .tuple_windows::<(_, _)>() + .map(|tuple| match tuple { + ('a' | 'c' | 'd' | 'f' | 'g' | 'j' | 'm' | '2' | '4' | '7', '#') => '♯', + ('a' | 'b' | 'd' | 'e' | 'g' | 'j' | 'm' | '2' | '4' | '7', 'b') => '♭', + (' ' | '/', c) => c.to_ascii_uppercase(), + (_, c) => c, }) - .intersperse("/".into()) .collect() } pub fn serialize_chord_name(input: &str) -> String { - input.replace("♯", "#").replace("♭", "b").to_ascii_lowercase() + input + .replace("♯", "#") + .replace("♭", "b") + .to_ascii_lowercase() } diff --git a/src/window.rs b/src/window.rs index e957097..399951e 100644 --- a/src/window.rs +++ b/src/window.rs @@ -20,8 +20,8 @@ use crate::{ chord_diagram::FretboardChordDiagram, chord_name_algorithm::calculate_chord_name, - chord_name_entry::FretboardChordNameEntry, chord_preview::FretboardChordPreview, - config::APP_ID, database::ChordsDatabase, chord_ops::prettify_chord_name, + chord_name_entry::FretboardChordNameEntry, chord_ops::prettify_chord_name, + chord_preview::FretboardChordPreview, config::APP_ID, database::ChordsDatabase, }; use adw::prelude::*; use adw::subclass::prelude::*; @@ -408,9 +408,7 @@ impl FretboardWindow { if let Some(chord) = chord_opt { imp.chord_diagram.set_chord(*chord); - imp - .feedback_stack - .set_visible_child_name("chord-actions"); + imp.feedback_stack.set_visible_child_name("chord-actions"); } else { imp.chord_diagram.set_chord(EMPTY_CHORD); imp.feedback_stack.set_visible_child_name("label"); @@ -435,8 +433,7 @@ impl FretboardWindow { }; imp.entry.overwrite_text(&name); - imp - .feedback_stack + imp.feedback_stack .set_visible_child_name(if !name.is_empty() { "chord-actions" } else { @@ -531,7 +528,8 @@ impl FretboardWindow { container.insert(&flow_box_child, -1); } - imp.variants_page.set_title(&prettify_chord_name(&chord_name)); + imp.variants_page + .set_title(&prettify_chord_name(&chord_name)); imp.variants_scrolled_window .set_vadjustment(Some(>k::Adjustment::builder().lower(0.0).build()));