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
pub mod lib_functions;

use std::path::{PathBuf, Path};
use std::fs::{self, File};
use std::io::{BufWriter, BufReader, Write, Error, ErrorKind, stdout};
use std::process::{Command, Stdio};

use dirs;
use include_dir::{include_dir, Dir};
use anyhow::Result;
use crossterm::{
    execute,
    style::{Color, Print, ResetColor, SetForegroundColor}
};

use crate::{Song, Fingering};


const FORBIDDEN_CHARS: [char; 9] = ['<', '>', ':', '/', '\\', '|', '?', '*', '`'];



pub fn init() -> Result<(), Box<dyn std::error::Error>> {
    let assets: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/assets");
    let mut path: PathBuf = dirs::data_dir()
        .ok_or("Cannot get path for data!")?;
    path.push("songbook");
    if !path.exists() { fs::create_dir_all(&path)? }
    assets.extract(&path)?;

    Ok(())
}

pub fn show(
    song_path: &Path,
    key: Option<crate::Key>,
    chords: bool,     // show chords
    rhythm: bool,     // show rhythm
    fingerings: bool, // show fingerings
    notes: bool,      // show notes
    is_colored: bool
) -> Result<()> {
    let mut path = get_lib_path()?;
    path = path.join(song_path);

    let file = File::open(path)?;
    let reader = BufReader::new(file);
    let mut song: Song = serde_yaml::from_reader(reader)?;
    if let Some(k) = key {
        if let Some(mut m_key) = song.metadata.key {
            while m_key.get_note() != k.get_note() {
                song.transpose(1);
                m_key = song.metadata.key.unwrap();
            }
        } else { println!("Add a key before transposing, try 'songbook edit <song_name>'") }
    }

    let text =
        if is_colored {
            song.get_colored(chords, rhythm, fingerings, notes)
        } else {
            song.get_song_as_text(chords, rhythm, fingerings, notes)
        };
    print(&text)?;


    Ok(())
}


pub fn edit(added_path: &Path) -> Result<()> {
    let mut path = get_lib_path()?;
    path = path.join(added_path);
    if !path.exists() {
        return Err( Error::new(ErrorKind::NotFound, "There's no such file!").into() )
    }

    let file = File::open(&path)?;
    let reader = BufReader::new(file);
    let mut song: Song = serde_yaml::from_reader(reader)?;

    let mut text = song.get_for_editing();
    text = edit::edit(text)?;
    song.change_from_edited_str(&text);

    let file = File::create(path)?;
    let writer = BufWriter::new(file);

    serde_yaml::to_writer(writer, &song)?;


    Ok(())
}


pub fn add(song: &Song) -> Result<()> {
    let mut path = get_lib_path()?;
    if !path.exists() { fs::create_dir_all(&path)? }

    let song_name = get_without_forbidden_chars(
        format!("{} - {}", song.metadata.artist, song.metadata.title)
    );
    path.push(&song_name);
    path = get_free_path(path, &song_name);

    let file = File::create(path)?;
    let writer = BufWriter::new(file);

    serde_yaml::to_writer(writer, &song)?;


    Ok(())
}


pub fn sort() -> Result<()> {
    let path = get_lib_path()?;
    recursive_sort(&path, &path)?;
    loop {
        if !remove_empty_folders(&path)? { break }
    }

    Ok(())
}
fn recursive_sort(path: &Path, lib_path: &Path) -> Result<()> {
    for entry in fs::read_dir(&path)? {
        let entry = entry?;
        if entry.path().is_dir() { recursive_sort(&entry.path(), lib_path)?; continue }

        let file = File::open(entry.path())?;
        let reader = BufReader::new(file);
        let song: Song = serde_yaml::from_reader(reader)?;

        let artist = get_without_forbidden_chars(song.metadata.artist.clone());
        let title = get_without_forbidden_chars(song.metadata.title.clone());
        let mut new_path = lib_path.join(&artist);
        if !new_path.exists() { fs::create_dir_all(&new_path)? }
        new_path = new_path.join(&title);
        let is_the_same = if new_path != entry.path() {
            new_path = get_free_path(new_path, &title);
            false
        } else { true };

        let file = File::create(&new_path)?;
        let writer = BufWriter::new(file);
        serde_yaml::to_writer(writer, &song)?;

        if !is_the_same {
            fs::remove_file(entry.path())?;
        }
    }

    Ok(())
}
fn remove_empty_folders(path: &Path) -> Result<bool> {
    let mut is_something_deleted = false;
    for entry in fs::read_dir(path)? {
        let entry = entry?;
        let current_path = entry.path();
        if current_path.is_file() { continue }

        if current_path.is_dir() {
            if fs::read_dir(&current_path)?.next().is_none() {
                fs::remove_dir(&current_path)?;
                is_something_deleted = true;
            } else {
                let is_del = remove_empty_folders(&current_path)?;
                if !is_something_deleted { is_something_deleted = is_del }
            }
        }
    }

    return Ok(is_something_deleted)
}


pub fn rm(added_path: &Path) -> Result<()> {
    let mut path = get_lib_path()?;
    path = path.join(added_path);

    if path.exists() {
        if path.is_file() { fs::remove_file(path)? }
        else if path.is_dir() { fs::remove_dir_all(path)? }
    } else {
        return Err( Error::new(
            ErrorKind::NotFound,
            format!("There's no such path: {:#?}", added_path)
        ).into())
    }

    Ok(())
}


pub fn mv(input_path: &Path, output_path: &Path) -> Result<()> {
    let path = get_lib_path()?;
    let i_path = path.join(input_path);
    if !i_path.exists() {
        return Err( Error::new(
            ErrorKind::NotFound,
            format!("There's no such path: {:#?}", input_path)
        ).into())
    }

    let mut o_path = path.join(output_path);
    if o_path.is_dir() { o_path = o_path.join( i_path.file_name()
        .expect("Cannot get input_path file name!") ) }

    fs::rename(i_path, o_path)?;

    Ok(())
}


pub fn cp(input_path: &Path, output_path: &Path) -> Result<()> {
    let path = get_lib_path()?;
    let i_path = path.join(input_path);
    if !i_path.exists() {
        return Err( Error::new(
            ErrorKind::NotFound,
            format!("There's no such path: {:#?}", input_path)
        ).into())
    }

    let mut o_path = path.join(output_path);
    if o_path.is_dir() { o_path = o_path.join( i_path.file_name()
        .expect("Cannot get input_path file name!") ) }

    if i_path != o_path {
        if i_path.is_dir() {
            cp_recursive(&i_path, &o_path)?;
        } else {
            fs::copy(i_path, o_path)?;
        }
    }

    Ok(())
}
fn cp_recursive(in_dir: &Path, out_dir: &Path) -> Result<()> {
    fs::create_dir_all(out_dir)?;
    for entry in fs::read_dir(in_dir)? {
        let entry = entry?;
        let path = entry.path();
        let out = out_dir.join(
            if let Some(n) = path.file_name() { n } else { continue }
        );
        if path.is_dir() {
            cp_recursive(&path, &out)?;
        } else {
            fs::copy(path, out)?;
        }
    }

    Ok(())
}


pub fn ls(added_path: Option<&Path>) -> Result<()> {
    let mut path = get_lib_path()?;
    if let Some(p) = added_path { path = path.join(p) }
    if !path.exists() {
        return Err( Error::new(ErrorKind::NotFound, "There's no such dir!").into() )
    }

    for entry in fs::read_dir(path)? {
        let entry = entry?;
        if let Some(name) = entry.file_name().to_str() {
            if entry.path().is_dir() {
                execute!(
                    stdout(),
                    SetForegroundColor(Color::Blue),
                    Print(name),
                    Print("\n"),
                    ResetColor
                )?;
            } else {
                println!("{}", name);
            }
        }
    }

    Ok(())
}


pub fn tree(added_path: Option<&Path>) -> Result<()> {
    let mut path = get_lib_path()?;
    if let Some(p) = added_path { path = path.join(p) }
    if !path.is_dir() {
        return Err( Error::new(ErrorKind::NotFound, "There's no such dir!").into() )
    }

    recursive_tree(&path, 1, false)?;

    Ok(())
}

fn recursive_tree(dir: &Path, indent: usize, is_parent_last: bool) -> Result<()> {
    if let Some(name) = dir.file_name().and_then(|f| f.to_str()) {
        println!("{}", name);
    }

    let last = if let Some(entry) = fs::read_dir(dir)?.last() { entry?.path() }
    else { return Ok(()) };

    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let is_last = entry.path() == last;
        if let Some(name) = entry.file_name().to_str() {
            if indent > 1 {
                if is_parent_last {
                    print!("{}", "    ".repeat(indent - 1));
                } else {
                    print!("{}", "│   ".repeat(indent - 1));
                }
            }

            if is_last {
                print!("└── ");
            } else {
                print!("├── ");
            }

            if entry.path().is_dir() {
                recursive_tree(&entry.path(), indent + 1, is_last)?;
            } else {
                println!("{}", name);
            }
        }
    }

    Ok(())
}


pub fn mkdir(added_path: &Path) -> Result<()> {
    let mut path = get_lib_path()?;
    path = path.join(added_path);

    if path.exists() {
         
        return Err( Error::new(
            ErrorKind::AlreadyExists, 
            format!("{:#?} is already exists!", added_path)
        ).into())
    }

    fs::create_dir_all(path)?;

    Ok(())
}

pub fn add_fingering(fing: &Fingering) -> Result<(), Box<dyn std::error::Error>> {
    let mut path: PathBuf = dirs::data_dir()
        .ok_or("Cannot get path for data!")?;
    path.push("songbook");
    path.push("fingerings");
    if !path.exists() { fs::create_dir_all(&path)? }

    let fing_name = get_without_forbidden_chars(
        fing.get_title()
            .ok_or("Cannot get the fingering title!")?
    );
    path.push(&fing_name);

    let file = File::create(path)?;
    let writer = BufWriter::new(file);

    serde_yaml::to_writer(writer, &fing)?;

    
    Ok(())
}

pub fn get_fingering(chord_name: &str) -> Result<Option<Fingering>, Box<dyn std::error::Error>> {
    let mut path: PathBuf = dirs::data_dir()
        .ok_or("Cannot get path for data!")?;
    path.push("songbook");
    path.push("fingerings");
    path.push(&chord_name);
    
    if !path.exists() { return Ok(None) }

    let file = File::open(path)?;
    let reader = BufReader::new(file);
    let fing: Fingering = serde_yaml::from_reader(reader)?;

    
    Ok(Some(fing))
}


fn print(text: &str) -> Result<()> {
    if let Ok(mut child) = Command::new("less").arg("-R").stdin(Stdio::piped()).spawn() {
        if let Some(mut stdin) = child.stdin.take() {
            stdin.write_all(text.as_bytes())?;
        }
        child.wait()?;
    } else {
        println!("{text}");
    }

    Ok(())
}

fn get_without_forbidden_chars(text: String) -> String {
    text.chars().map(|c|
        if FORBIDDEN_CHARS.iter().any(|f| *f == c) { '_' }
        else { c }
    ).collect()
}

fn get_free_path(mut path: PathBuf, name: &str) -> PathBuf {
    let mut counter = 1;
    while path.exists() {
        path.set_file_name(&format!("{}({})", name, counter));
        counter += 1;
    }

    return path
}

pub fn get_lib_path() -> Result<PathBuf> {
    if let Some(mut path) = dirs::data_dir() {
        path.push("songbook");
        path.push("library");

        Ok(path)
    }
    else { Err( Error::new(ErrorKind::NotFound, "Cannot get data directory!").into() ) }
}