songbook 0.1.1

Songbook with TUI and CLI
Documentation
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
pub mod block;
pub mod row;
pub mod chord;

use serde::{Serialize, Deserialize};
use crossterm::style::Stylize;

use crate::Fingering;
use crate::{
    METADATA_START,
    METADATA_END,
    SONG_TITLE_SYMBOL,
    SONG_ARTIST_SYMBOL,
    SONG_KEY_SYMBOL,
    SONG_CAPO_SYMBOL,
    SONG_AUTOSCROLL_SPEED_SYMBOL,

    BLOCK_START,
    BLOCK_END,
    STANDART_TUNING,
    
    TITLE_COLOR,
    NOTES_COLOR,

    SONG_NOTE_START_SYMBOL,
    SONG_NOTE_END_SYMBOL,

    KEYS
};
use crate::{Note, Key};
use crate::sum_text_in_fingerings;
use crate::song::chord::Chord;
use crate::song::block::{Block, Line};
use crate::song::row::ChordPosition;


#[derive(Serialize, Deserialize, Debug)]
pub struct Song {
    pub metadata: Metadata,
    pub chord_list: Vec<Chord>,
    pub blocks: Vec<Block>,
    pub notes: Option<String> // Заметки по песне в общем
}
// Тональности:
// Am - C
// A#m - C#
// Bm - D
// Cm - D#
// C#m - E
// Dm - F
// D#m - F#
// Em - G
// Fm - G#
// F#m - A
// Gm - A#
// G#m - B

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Metadata {
    pub title: String,
    pub artist: String,
    pub key: Option<Key>,
    pub capo: Option<u8>,
    pub autoscroll_speed: Option<u64>, // in milliseconds
}

impl Metadata {
    fn get_for_editing(&self, s: &mut String) {
        s.push_str(METADATA_START);
        s.push('\n');


        s.push_str(SONG_TITLE_SYMBOL);
        s.push_str(&self.title);
        s.push('\n');

        s.push_str(SONG_ARTIST_SYMBOL);
        s.push_str(&self.artist);
        s.push('\n');

        s.push_str(SONG_KEY_SYMBOL);
        if let Some(key) = self.key {
            s.push_str(&key.to_string())
        }
        s.push('\n');

        s.push_str(SONG_CAPO_SYMBOL);
        if let Some(capo) = self.capo {
            s.push_str(&capo.to_string())
        }
        s.push('\n');

        s.push_str(SONG_AUTOSCROLL_SPEED_SYMBOL);
        if let Some(speed) = self.autoscroll_speed {
            s.push_str(&speed.to_string())
        }
        s.push('\n');


        s.push_str(METADATA_END);
        s.push('\n');

        s.push('\n');
        s.push('\n');
    }

    fn change_from_edited_str(&mut self, text: &str) {
        let mut title = String::new();
        let mut artist = String::new();
        let mut key: Option<Key> = None;
        let mut capo: Option<u8> = None;
        let mut autoscroll_speed: Option<u64> = None;
        for line in text.lines() {
            if line.starts_with(SONG_TITLE_SYMBOL) {
                title = line[SONG_TITLE_SYMBOL.len()..].trim().to_string();
            } else if line.starts_with(SONG_ARTIST_SYMBOL) {
                artist = line[SONG_ARTIST_SYMBOL.len()..].trim().to_string();
            } else if line.starts_with(SONG_KEY_SYMBOL) {
                let k = line[SONG_KEY_SYMBOL.len()..].trim();
                key = Key::new(k);
            } else if line.starts_with(SONG_CAPO_SYMBOL) {
                if let Ok(c) = line[SONG_CAPO_SYMBOL.len()..].trim().parse::<u8>() {
                    capo = Some(c)
                }
            } else if line.starts_with(SONG_AUTOSCROLL_SPEED_SYMBOL) {
                if let Ok(s) = line[SONG_AUTOSCROLL_SPEED_SYMBOL.len()..].trim().parse::<u64>() {
                    autoscroll_speed = Some(s)
                }
            }
        }

        if !title.is_empty() { self.title = title }
        if !artist.is_empty() { self.artist = artist }
        self.key = key;
        self.capo = capo;
        self.autoscroll_speed = autoscroll_speed;
    }
}


impl Song {
    pub fn new(title: &str, artist: &str) -> Self {
        Self {
            metadata: Metadata {
                title: title.to_string(), 
                artist: artist.to_string(),
                key: None,
                capo: None,
                autoscroll_speed: None
            },
            chord_list: Vec::new(),
            blocks: Vec::new(),
            notes: None
        }
    }

    pub fn get_song_as_text(
        &self,
        chords: bool,
        rhythm: bool,
        fingerings: bool,
        notes: bool
    ) -> String {
        let mut s = String::new();


        if !self.metadata.artist.is_empty() && !self.metadata.title.is_empty() {
            s.push_str( &format!("{} - {}", self.metadata.artist, self.metadata.title) );
            s.push_str("\n\n");
        }

        if let Some(n) = &self.notes && notes {
            s.push_str(n);
            s.push('\n');
        }

        if chords && fingerings {
            let mut fings = Vec::new();
            
            #[cfg(not(feature = "song_library"))]
            for f in self.get_fingerings() {
                fings.push(f[0].clone());
            }
            
            #[cfg(feature = "song_library")]
            for chord in &self.chord_list {
                if let Ok(Some(f)) = crate::song_library::get_fingering(&chord.text) {
                    fings.push(f)
                } else {
                    fings.push( chord.get_fingerings(&STANDART_TUNING)[0].clone() )
                }
            }

            
            if let Some(text) = sum_text_in_fingerings(&fings, None) {
                s.push_str(&text);
            }
        }

        s.push_str(&self.to_string(chords, rhythm, notes));


        return s
    }

    pub fn print(&self, chords: bool, rhythm: bool, fingerings: bool, notes: bool) {
        println!("{}", self.get_song_as_text(chords, rhythm, fingerings, notes));
    }

    pub fn to_string(&self, chords: bool, rhythm: bool, notes: bool) -> String {
        let mut s = String::new();
        let mut is_first = true;
        for block in &self.blocks {
            if is_first { is_first = false }
            else { s.push('\n') }

            if let Some(title) = &block.title {
                if !is_first && !title.is_empty() { s.push('\n') }
                s.push_str(&title);
                s.push(' ');
            }
            if let Some(n) = &block.notes && notes {
                if !is_first && block.title.is_none() { s.push('\n') }
                s.push_str(n);
            }
            if !block.lines.is_empty() { s.push('\n') }

            let mut is_first_line = true;
            for line in &block.lines {
                if is_first_line { is_first_line = false }
                else { s.push('\n') }
                match line {
                    Line::TextBlock(row) => s.push_str(&row.to_string(chords, rhythm)),
                    Line::ChordsLine(chords) => {
                        for chord in chords {
                            s.push_str(&chord.text);
                            s.push(' ');
                        }
                    },
                    Line::PlainText(text) => s.push_str(text),
                    Line::Tab(text) => s.push_str(text),
                    Line::EmptyLine => {}
                }
            }
        }

        return s
    }

    pub fn get_colored(
        &self,
        chords: bool,
        rhythm: bool,
        fingerings: bool,
        notes: bool
    ) -> String {

        let mut s = String::new();
        if !self.metadata.artist.is_empty() && !self.metadata.title.is_empty() {
            s.push_str(& format!("{} - {}\n\n", self.metadata.artist, self.metadata.title));
        }

        if let Some(n) = &self.notes && notes {
            for line in n.lines() {
                s.push_str( &format!("{}\n", line.with(NOTES_COLOR)) );
            }
        }

        if chords && fingerings {
            let mut fings = Vec::new();
            
            #[cfg(not(feature = "song_library"))]
            for f in self.get_fingerings() {
                fings.push(f[0].clone());
            }
            
            #[cfg(feature = "song_library")]
            for chord in &self.chord_list {
                if let Ok(Some(f)) = crate::song_library::get_fingering(&chord.text) {
                    fings.push(f)
                } else {
                    fings.push( chord.get_fingerings(&STANDART_TUNING)[0].clone() )
                }
            }

            
            if let Some(text) = sum_text_in_fingerings(&fings, None) {
                s.push_str(&text);
            }
        }
        
        let mut is_first = true;
        for block in &self.blocks {
            if is_first { is_first = false }
            else { s.push('\n') }

            if let Some(title) = &block.title {
                if !is_first && !title.is_empty() { s.push('\n') }
                s.push_str(&format!("{}", title.clone().with(TITLE_COLOR)));
                s.push(' ');
            }
            if let Some(n) = &block.notes && notes {
                if !is_first && block.title.is_none() { s.push('\n') }
                s.push_str(&format!("{}", n.clone().with(NOTES_COLOR)));
            }
            if !block.lines.is_empty() { s.push('\n') }
            
            let mut is_first_line = true;
            for line in &block.lines {
                if is_first_line { is_first_line = false }
                else { s.push('\n') }
                line.get_colored(&mut s, chords, rhythm);
            }
        }
        
        return s
    }

    pub fn detect_key(&mut self) -> Note {
        let this_keys: Vec<Note> = self.chord_list
            .iter()
            .map(|c| c.get_keynote() )
            .collect();

        let total: f32 = this_keys.len() as f32;
        let mut key: Option<Key> = None;
        let mut similarity: f32 = 0.0; // Значение в процентах

        for key_block in KEYS {
            let keynote = key_block[0];
            let mut matches = 0.0;
            for key in &this_keys {
                if key_block.iter().any(|k| k == key) {
                    matches += 1.0
                }
            }
            let this_precent: f32 = (matches * 100.0) / total;
            if this_precent > similarity {
                similarity = this_precent;
                key = Some(Key::from_note(keynote));
            }
        }

        self.metadata.key = key;
        if let Some(k) = key {
            k.get_note()
        } else {
            Note::C
        }
    }

    pub fn transpose(&mut self, steps: i32) {
        if let Some(key) = self.metadata.key {
            self.metadata.key = Some(key.transpose(steps))
        }
        for chord in &mut self.chord_list { *chord = chord.transpose(steps) }
        for block in &mut self.blocks {
            for line in &mut block.lines {
                match line {
                    Line::TextBlock(row) => {
                        if let Some(chords) = &mut row.chords {
                            for chord in chords {
                                match chord {
                                    ChordPosition::UpBeat(chord) => *chord = chord.transpose(steps),
                                    ChordPosition::OnIndex{chord, ..} => *chord = chord.transpose(steps)
                                }
                            }
                        }
                    },
                    Line::ChordsLine(chords) =>
                        chords.iter_mut().for_each(|c| *c = c.transpose(steps)),
                    _ => {}
                }
            }
        }
    }

    pub fn get_fingerings(&self) -> Vec<Vec<Fingering>> {
        let mut fings = Vec::new();
        for chord in &self.chord_list {
            fings.push(chord.get_fingerings(&STANDART_TUNING));
        }

        return fings
    }

    pub fn get_for_editing(&self) -> String {
        let mut s = String::new();

        self.metadata.get_for_editing(&mut s);


        if let Some(n) = &self.notes {
            s.push_str(SONG_NOTE_START_SYMBOL);
            s.push('\n');

            s.push_str(n);
            s.push('\n');

            s.push_str(SONG_NOTE_END_SYMBOL);
            s.push('\n');

            s.push('\n');
        }

        let mut is_first = true;
        for block in &self.blocks {
            if is_first { is_first = false }
            else { s.push_str("\n\n") }
            block.get_for_editing(&mut s);
        }

        return s
    }

    pub fn generate_rhythm_from_chords(&mut self) {
        for block in &mut self.blocks {
            for line in &mut block.lines {
                if let Line::TextBlock(row) = line {
                    row.generate_rhythm_from_chords();
                }
            }
        }
    }

    pub fn change_from_edited_str(&mut self, text: &str) {
        let mut blocks: Vec<Block> = Vec::new();
        let mut metadata_text = String::new();

        let mut block_buf = String::new();
        let mut is_in_block = false;

        let mut note_buf = String::new();
        let mut is_in_note = false;

        let mut is_in_metadata = false;
        for line in text.lines() {
            if line.starts_with(SONG_NOTE_START_SYMBOL) {
                is_in_note = true;
            } else if line.starts_with(SONG_NOTE_END_SYMBOL) {
                is_in_note = false;
                self.notes = if note_buf.is_empty() { None } else { Some(note_buf) };
                note_buf = String::new();
            } else if is_in_note {
                if !note_buf.is_empty() {
                    note_buf.push('\n')
                }
                note_buf.push_str(line);

            } else if line.starts_with(BLOCK_START) { is_in_block = true }
            else if line.starts_with(BLOCK_END) {
                is_in_block = false;
                blocks.push( Block::from_edited(&block_buf) );
                block_buf.clear();
            } else if is_in_block { block_buf.push_str(line); block_buf.push('\n'); }

            else if line.starts_with(METADATA_START) { is_in_metadata = true }
            else if line.starts_with(METADATA_END) {
                is_in_metadata = false;
                self.metadata.change_from_edited_str(&metadata_text);
            } else if is_in_metadata {
                metadata_text.push_str(line);
                metadata_text.push('\n');
            }
        }


        self.blocks = blocks;
        self.chord_list = self.get_chord_list();
    }

    fn get_chord_list(&self) -> Vec<Chord> {
        let mut list = Vec::new();
        for block in &self.blocks {
            for line in &block.lines {
                match line {
                    Line::TextBlock(row) => {
                        if let Some(chords) = &row.chords {
                            for chord in chords {
                                match chord {
                                    ChordPosition::UpBeat(chord) => if list.iter().all(|c| c != chord) {
                                        list.push(chord.clone());
                                    },
                                    ChordPosition::OnIndex{chord, ..} => if list.iter().all(|c| c != chord) {
                                        list.push(chord.clone());
                                    },
                                }
                            }
                        }
                    },
                    Line::ChordsLine(chords) => for chord in chords {
                        if list.iter().all(|c| c != chord) {
                            list.push(chord.clone());
                        }
                    },
                    _ => {}
                }
            }
        }

        return list;
    }
}