Skip to content

Commit

Permalink
Added the details command.
Browse files Browse the repository at this point in the history
See README for details.
  • Loading branch information
b0wter committed Apr 10, 2021
1 parent 3bc9b0d commit 36a491e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ comment
```
To preview the changes add the `-d` (dry run) parameter.

Details
-------
```bash
./fbrary -l $LIBRARY_FILENAME details 1 2 ...
```
Writes all available information to the terminal. Fields that are empty are represented as `-`. A rating of `-1` means that none has been set.

Hints
=====

Expand Down
2 changes: 1 addition & 1 deletion snapcraft/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fbrary
base: core20
version: '2.1.1'
version: '2.1.2'
summary: Manage your audio book library from the command line.
description: |
Use this tool to manage your audiobook library from the
Expand Down
2 changes: 2 additions & 0 deletions src/cli/Arguments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module Arguments =
| [<CliPrefix(CliPrefix.None)>] Unmatched of string
| [<CliPrefix(CliPrefix.None)>] Write of ParseResults<WriteArgs>
| [<CliPrefix(CliPrefix.None)>] Migrate
| [<CliPrefix(CliPrefix.None)>] Details of int list
| [<CliPrefix(CliPrefix.DoubleDash); First>] Version
interface IArgParserTemplate with
member s.Usage =
Expand All @@ -121,5 +122,6 @@ module Arguments =
| Unmatched _ -> "Reads all mp3/ogg files in the given paths and checks if all files are known to the library."
| Write _ -> "Write the meta data stored in the library to the actual mp3/ogg files."
| Migrate -> "Migrate an old library file to the current format."
| Details _ -> "List the complete details (including files) for the given audio books."
| Version -> "Echo the version of this software."

26 changes: 25 additions & 1 deletion src/cli/Audiobook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,28 @@ module Audiobook =
let allFiles (a: Audiobook) : string list =
match a.Source with
| SingleFile s -> [ s ]
| MultiFile m -> m
| MultiFile m -> m

let details (a: Audiobook) : string list =
let formatDuration (t: TimeSpan) = t.ToString("h\:mm")
let formatBool = function true -> "yes" | false -> "no"
let formatState = function State.Aborted -> "aborted" | State.Completed -> "completed" | State.NotCompleted -> "not completed"
let formatSources =
function
| AudiobookSource.SingleFile s -> [ $"Source: %s{s}" ]
| AudiobookSource.MultiFile many ->
"Source:" :: (many |> List.map (fun s -> $"\t%s{s}"))
let asString = function | Some s -> s | None -> "-"
[
$"Id: %i{a.Id}"
$"Album: %s{a.Album |> asString}"
$"Album Artist: %s{a.AlbumArtist |> asString}"
$"Artist: %s{a.Artist |> asString}"
$"Title: %s{a.Title |> asString}"
$"Duration: %s{a.Duration |> formatDuration}"
$"Rating: %i{a.Rating |> Option.map (Rating.value) |> Option.getOrElse -1}"
$"State: %s{a.State |> formatState}"
$"Genre: %s{a.Genre |> asString}"
$"Comment: %s{a.Comment |> asString}"
$"Has Picture: %s{a.HasPicture |> formatBool}"
] @ (a.Source |> formatSources)
7 changes: 7 additions & 0 deletions src/cli/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ module Config =
NonInteractive = false
}

type DetailsConfig = {
Ids: int list
}

type Command
= Add of AddConfig
| List of ListConfig
Expand All @@ -194,6 +198,7 @@ module Config =
| Uninitialized
| Write of WriteConfig
| Migrate
| Details of DetailsConfig
| Version

type Config = {
Expand Down Expand Up @@ -360,6 +365,8 @@ module Config =
{ config with Command = Write updatedWriteConfig }
| MainArgs.Migrate ->
{ config with Command = Migrate }
| MainArgs.Details ids ->
{ config with Command = Details { Ids = ids } }
| MainArgs.Version ->
{ config with Command = Version }

Expand Down
18 changes: 13 additions & 5 deletions src/cli/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module Program =
| true, false -> getFilesAsBooks path
| false, true -> getSubFoldersAsBooks path
| true, true -> getFilesAsBookAndSubFoldersAsBooks path
let files = files |> List.map (IO.filterMp3Files) |> List.skipEmpty
let files = files |> List.map IO.filterMp3Files |> List.skipEmpty
return! files |> List.map createBook |> List.sequenceResultM
}

Expand Down Expand Up @@ -351,7 +351,7 @@ module Program =

let filter = match config.ListMissing with
| false -> id
| true -> List.filter (IO.fileDoesNotExist)
| true -> List.filter IO.fileDoesNotExist
let files = books |> List.collect (fun book -> book |> Audiobook.allFiles |> filter |> List.map (sprintf "\"%s\""))

let separator = match config.Separator with
Expand All @@ -372,6 +372,12 @@ module Program =
do! metadata |> List.map (TagLib.writeMetaData writeConfig) |> List.sequenceResultM |> Result.map ignore
}

let listDetails (config: Config.DetailsConfig) (library: Library.Library) =
result {
let! books = library |> Library.findByIds config.Ids
do books |> List.iter (Audiobook.details >> List.iter Console.WriteLine)
}

let migrateLibrary (libraryFile: string) _ =
result {
// Make sure the config no longer uses the `LastScanned` field.
Expand Down Expand Up @@ -463,17 +469,19 @@ module Program =
| Config.Files filesConfig ->
let! string = listFiles config.LibraryFile filesConfig
if String.IsNullOrWhiteSpace(string) then ()
else do printfn "%s" string
else do Console.WriteLine(string)
| Config.Unmatched unmatchedConfig ->
return! (runOnExisting (unmatched unmatchedConfig config.LibraryFile))
| Config.Uninitialized ->
do printfn "%s" (parser.PrintUsage())
do Console.WriteLine(parser.PrintUsage())
| Config.Write writeConfig ->
return! (runOnExisting (write writeConfig))
| Config.Migrate ->
return! (runOnExisting (migrateLibrary config.LibraryFile))
| Config.Details detailsConfig ->
return! (runOnExisting (listDetails detailsConfig))
| Config.Version ->
do printfn "%s" Version.current
do Console.WriteLine(Version.current)
}

match r with
Expand Down

0 comments on commit 36a491e

Please sign in to comment.