1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::{cmp::max, fmt, fmt::Write};
use crate::{FretID, Semitones, UkeString, Voicing, MIN_CHART_WIDTH};
pub struct ChordChart {
voicing: Voicing,
/// Number of frets to use to display the chord voicing
width: Semitones,
}
impl ChordChart {
pub fn new(voicing: Voicing, width: Semitones) -> Self {
let width = max(width, MIN_CHART_WIDTH);
assert!(voicing.get_span() <= width);
Self { voicing, width }
}
/// Determine from which fret to show the fretboard.
///
/// If the rightmost fret fits on the diagram, show the fretboard
/// beginning at the first fret, otherwise use the leftmost fret
/// needed for the chords to be played.
pub fn get_base_fret(&self) -> FretID {
let max_fret = self.voicing.get_max_fret();
match max_fret {
max_fret if max_fret <= self.width => 1,
_ => self.voicing.get_min_pressed_fret(),
}
}
/// Get the width of the space that we need to print the names
/// of the root notes (the names of the strings).
pub fn get_root_width(&self) -> usize {
self.voicing
.roots()
.map(|n| n.to_string().len())
.max()
.unwrap()
}
/// Format a line that represents a ukulele string in a chord diagram.
pub fn format_line(
&self,
uke_string: UkeString,
base_fret: FretID,
root_width: usize,
finger: u8,
) -> String {
let (root, fret, note) = uke_string;
let root_str = format!("{:width$}", root.to_string(), width = root_width);
// Show a symbol for the nut if the chord is played on the lower
// end of the fretboard. Indicate ongoing strings otherwise.
let nut = match base_fret {
1 => "||",
_ => "-|",
};
// Mark open strings with a special symbol.
let sym = match fret {
0 => "o",
_ => " ",
};
// Create a line representing the string with the fret to be pressed.
let s: String = (base_fret..base_fret + self.width)
.map(|i| {
if fret == i {
finger.to_string()
} else {
"-".to_string()
}
})
.fold(String::new(), |mut output, c| {
let _ = write!(output, "-{c}-|");
output
});
format!("{root_str} {sym}{nut}{s}- {note}\n")
}
}
impl fmt::Display for ChordChart {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Determine from which fret to show the fretboard.
let base_fret = self.get_base_fret();
// Get the width of the space that we need to print the name
// of the root notes (the names of the strings).
let root_width = self.get_root_width();
let fingers_on_strings = self.voicing.fingers_on_strings();
// Create a diagram for each ukulele string.
let s: String = self
.voicing
.uke_strings()
.rev()
.zip(fingers_on_strings.iter().rev())
.map(|(us, f)| self.format_line(*us, base_fret, root_width, *f))
.collect();
// If the fretboard section shown does not include the nut,
// indicate the number of the first fret shown.
if base_fret > 1 {
return writeln!(f, "{s}{base_fret:width$}", width = root_width + 6);
}
write!(f, "{s}")
}
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use rstest::rstest;
use super::*;
use crate::{Chord, Tuning, VoicingConfig};
#[rstest(chord, tuning, diagram,
case(
"C",
Tuning::C,
indoc!("
A ||---|---|-3-|---|- C
E o||---|---|---|---|- E
C o||---|---|---|---|- C
G o||---|---|---|---|- G
"),
),
case(
"C#",
Tuning::C,
indoc!("
A ||---|---|---|-4-|- C#
E ||-1-|---|---|---|- F
C ||-1-|---|---|---|- C#
G ||-1-|---|---|---|- G#
")
),
case(
"Db",
Tuning::C,
indoc!("
A ||---|---|---|-4-|- Db
E ||-1-|---|---|---|- F
C ||-1-|---|---|---|- Db
G ||-1-|---|---|---|- Ab
")
),
case(
"C#m",
Tuning::C,
indoc!("
A ||---|---|---|-4-|- C#
E o||---|---|---|---|- E
C ||-2-|---|---|---|- C#
G ||-1-|---|---|---|- G#
")
),
case(
"Dbm",
Tuning::C,
indoc!("
A ||---|---|---|-4-|- Db
E o||---|---|---|---|- E
C ||-2-|---|---|---|- Db
G ||-1-|---|---|---|- Ab
")
),
case(
"D",
Tuning::D,
indoc!("
B ||---|---|-3-|---|- D
F# o||---|---|---|---|- F#
D o||---|---|---|---|- D
A o||---|---|---|---|- A
"),
),
case(
"G",
Tuning::G,
indoc!("
E ||---|---|-3-|---|- G
B o||---|---|---|---|- B
G o||---|---|---|---|- G
D o||---|---|---|---|- D
"),
),
)]
fn test_to_diagram(chord: Chord, tuning: Tuning, diagram: &str) {
let config = VoicingConfig {
tuning,
..Default::default()
};
let voicing = chord.voicings(config).next().unwrap();
let chord_chart = ChordChart::new(voicing, 4);
assert_eq!(chord_chart.to_string(), diagram);
}
}