Skip to content

Commit

Permalink
Create readme
Browse files Browse the repository at this point in the history
  • Loading branch information
imDema committed Oct 4, 2019
1 parent cfbab3b commit 8698a07
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# dir2m3u

Create `.m3u` playlists from a directoy structure

## Usage

```
dir2m3u
Create m3u playlists from directories
USAGE:
dir2m3u [FLAGS] <DIRECTORY>
FLAGS:
-h, --help Prints help information
-r, --recursive recursive: turn subdirectories into playlists too
-V, --version Prints version information
ARGS:
<DIRECTORY> directory used to create the playlist [default: .]
```

Run the executable to create `m3u` playlist files with the songs in the directories you provided

Use `-r` to create a playlist for each directory contained in the one you selected.

All the `m3u` playlist files will have the same name as the directory they were created from

### Notes

Learning project
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn run(conf: Config) -> Result<(), Box<dyn Error>> {
Ok(())
}

/// Traverse all directories contained in `dir` recursively and call `make_playlist` for each one
fn recursive(dest: &PathBuf, dir: PathBuf) -> Result<(), Box<dyn Error>> {
let dirs = dir
.read_dir()?
Expand All @@ -43,6 +44,7 @@ fn recursive(dest: &PathBuf, dir: PathBuf) -> Result<(), Box<dyn Error>> {
make_playlist(&dest, dir)
}

/// Call `gen_playlist` and write it to file
fn make_playlist(dest: &PathBuf, dir: PathBuf) -> Result<(), Box<dyn Error>> {
if let Some(buf) = gen_playlist(&dir)? {
let mut filename = dir.canonicalize()?.file_name().unwrap().to_owned();
Expand All @@ -56,8 +58,11 @@ fn make_playlist(dest: &PathBuf, dir: PathBuf) -> Result<(), Box<dyn Error>> {
Ok(())
}

const M3U_HEAD: &str = "#EXTM3U\n";

/// Iterate over `mp3` and create m3u format String
fn gen_playlist(dir: &Path) -> Result<Option<String>, Box<dyn Error>> {
let mut buf = String::from("#EXTM3U\n");
let mut buf = String::from(M3U_HEAD);

let songs = dir
.read_dir()?
Expand All @@ -72,7 +77,7 @@ fn gen_playlist(dir: &Path) -> Result<Option<String>, Box<dyn Error>> {
buf.push_str(format!("#EXTINF:{0:?}\n{1}\n", songname, song.display()).as_str());
}

if buf.len() > "#EXTM3U\n".len() {
if buf.len() > M3U_HEAD.len() {
Ok(Some(buf))
} else {
Ok(None)
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ fn main() {
.help("directory used to create the playlist"))
.arg(Arg::with_name("recursive")
.short("r")
.long("recursive")
.help("recursive: turn subdirectories into playlists too"))
.arg(Arg::with_name("depth")
.short("d")
.long("depth")
.help("set maximum depth for recursion (NOT IMPLEMENTED)"))
.get_matches();

Expand Down

0 comments on commit 8698a07

Please sign in to comment.