Skip to content

Commit

Permalink
fix: improve chord prettifier
Browse files Browse the repository at this point in the history
  • Loading branch information
bragefuglseth committed Oct 25, 2023
1 parent 182e951 commit 1f9056c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
9 changes: 5 additions & 4 deletions src/chord_name_entry.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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();

Expand Down
29 changes: 13 additions & 16 deletions src/chord_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,22 @@ pub fn adjust_chord(chord: [Option<usize>; 6], barre: u8) -> [Option<usize>; 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::<String>()
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()
}
14 changes: 6 additions & 8 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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");
Expand All @@ -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 {
Expand Down Expand Up @@ -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(&gtk::Adjustment::builder().lower(0.0).build()));

Expand Down

0 comments on commit 1f9056c

Please sign in to comment.