Initial TUI implementation
This commit is contained in:
parent
1f5207fd65
commit
bfcc04539e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
/codecov
|
||||||
|
159
Cargo.lock
generated
159
Cargo.lock
generated
@ -43,6 +43,12 @@ version = "1.3.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cassowary"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.0.79"
|
version = "1.0.79"
|
||||||
@ -70,6 +76,31 @@ dependencies = [
|
|||||||
"vec_map",
|
"vec_map",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossterm"
|
||||||
|
version = "0.26.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a84cda67535339806297f1b331d6dd6320470d2a0fe65381e79ee9e156dd3d13"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"crossterm_winapi",
|
||||||
|
"libc",
|
||||||
|
"mio",
|
||||||
|
"parking_lot",
|
||||||
|
"signal-hook",
|
||||||
|
"signal-hook-mio",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossterm_winapi"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "difflib"
|
name = "difflib"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
@ -210,12 +241,43 @@ version = "0.3.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cd550e73688e6d578f0ac2119e32b797a327631a42f9433e59d02e139c8df60d"
|
checksum = "cd550e73688e6d578f0ac2119e32b797a327631a42f9433e59d02e139c8df60d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lock_api"
|
||||||
|
version = "0.4.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"scopeguard",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mio"
|
||||||
|
version = "0.8.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
"wasi",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mockall"
|
name = "mockall"
|
||||||
version = "0.11.4"
|
version = "0.11.4"
|
||||||
@ -247,8 +309,10 @@ dependencies = [
|
|||||||
name = "musichoard"
|
name = "musichoard"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"crossterm",
|
||||||
"mockall",
|
"mockall",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
"ratatui",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"structopt",
|
"structopt",
|
||||||
@ -277,6 +341,29 @@ version = "1.17.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "parking_lot"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||||
|
dependencies = [
|
||||||
|
"lock_api",
|
||||||
|
"parking_lot_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "parking_lot_core"
|
||||||
|
version = "0.9.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"redox_syscall 0.2.16",
|
||||||
|
"smallvec",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "predicates"
|
name = "predicates"
|
||||||
version = "2.1.5"
|
version = "2.1.5"
|
||||||
@ -349,6 +436,28 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ratatui"
|
||||||
|
version = "0.20.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dcc0d032bccba900ee32151ec0265667535c230169f5a011154cdcd984e16829"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"cassowary",
|
||||||
|
"crossterm",
|
||||||
|
"unicode-segmentation",
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "redox_syscall"
|
||||||
|
version = "0.2.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.3.5"
|
version = "0.3.5"
|
||||||
@ -395,6 +504,12 @@ version = "1.0.13"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
|
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "scopeguard"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.159"
|
version = "1.0.159"
|
||||||
@ -426,6 +541,42 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signal-hook"
|
||||||
|
version = "0.3.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"signal-hook-registry",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signal-hook-mio"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"mio",
|
||||||
|
"signal-hook",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signal-hook-registry"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smallvec"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
@ -486,7 +637,7 @@ checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"fastrand",
|
"fastrand",
|
||||||
"redox_syscall",
|
"redox_syscall 0.3.5",
|
||||||
"rustix",
|
"rustix",
|
||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
@ -545,6 +696,12 @@ version = "0.9.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
16
Cargo.toml
16
Cargo.toml
@ -6,12 +6,14 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
crossterm = "0.26.1"
|
||||||
serde_json = "1.0"
|
serde = { version = "1.0.159", features = ["derive"] }
|
||||||
structopt = "0.3"
|
serde_json = "1.0.95"
|
||||||
uuid = { version = "1.3", features = ["serde"] }
|
structopt = "0.3.26"
|
||||||
|
ratatui = "0.20.1"
|
||||||
|
uuid = { version = "1.3.0", features = ["serde"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
mockall = "0.11"
|
mockall = "0.11.4"
|
||||||
once_cell = "1.17"
|
once_cell = "1.17.1"
|
||||||
tempfile = "3.5"
|
tempfile = "3.5.0"
|
||||||
|
19
README.md
19
README.md
@ -12,21 +12,26 @@ cargo install grcov
|
|||||||
### Generating Code Coverage
|
### Generating Code Coverage
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
env CARGO_TARGET_DIR=codecov \
|
||||||
cargo clean
|
cargo clean
|
||||||
env RUSTFLAGS="-C instrument-coverage" \
|
env RUSTFLAGS="-C instrument-coverage" \
|
||||||
LLVM_PROFILE_FILE="target/debug/profraw/musichoard-%p-%m.profraw" \
|
LLVM_PROFILE_FILE="codecov/debug/profraw/musichoard-%p-%m.profraw" \
|
||||||
|
CARGO_TARGET_DIR=codecov \
|
||||||
cargo test
|
cargo test
|
||||||
grcov target/debug/profraw \
|
grcov codecov/debug/profraw \
|
||||||
--binary-path ./target/debug/ \
|
--binary-path ./codecov/debug/ \
|
||||||
--output-types html \
|
--output-types html \
|
||||||
--source-dir . \
|
--source-dir . \
|
||||||
--ignore-not-existing \
|
--ignore-not-existing \
|
||||||
--ignore "tests/*" \
|
--ignore "tests/*" \
|
||||||
--ignore "src/main.rs" \
|
--ignore "src/main.rs" \
|
||||||
--excl-start "mod tests \{" \
|
--excl-start "mod tests \{|GRCOV_EXCL_START" \
|
||||||
--output-path ./target/debug/coverage/
|
--excl-stop "GRCOV_EXCL_STOP" \
|
||||||
xdg-open target/debug/coverage/index.html
|
--output-path ./codecov/debug/coverage/
|
||||||
|
xdg-open codecov/debug/coverage/index.html
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that some changes may not be visible until `target/debug/coverage` is removed and the `grcov`
|
Note that some changes may not be visible until `codecov/debug/coverage` is removed and the `grcov`
|
||||||
command is rerun.
|
command is rerun.
|
||||||
|
|
||||||
|
For most cases `cargo clean` can be replaced with `rm -rf ./codecov/debug/{coverage,profraw}`.
|
||||||
|
184
src/collection/mod.rs
Normal file
184
src/collection/mod.rs
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
//! Module for managing the music collection, i.e. "The Music Hoard".
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::{self, Database},
|
||||||
|
library::{self, Library, Query},
|
||||||
|
Artist,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The collection type.
|
||||||
|
pub type Collection = Vec<Artist>;
|
||||||
|
|
||||||
|
/// Error type for collection manager.
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum Error {
|
||||||
|
/// The [`CollectionManager`] failed to read/write from/to the library.
|
||||||
|
LibraryError(String),
|
||||||
|
/// The [`CollectionManager`] failed to read/write from/to the database.
|
||||||
|
DatabaseError(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
Self::LibraryError(ref s) => write!(f, "failed to read/write from/to the library: {s}"),
|
||||||
|
Self::DatabaseError(ref s) => {
|
||||||
|
write!(f, "failed to read/write from/to the database: {s}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<library::Error> for Error {
|
||||||
|
fn from(err: library::Error) -> Error {
|
||||||
|
Error::LibraryError(err.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<database::Error> for Error {
|
||||||
|
fn from(err: database::Error) -> Error {
|
||||||
|
Error::DatabaseError(err.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait CollectionManager {
|
||||||
|
/// Rescan the library and integrate any updates into the collection.
|
||||||
|
fn rescan_library(&mut self) -> Result<(), Error>;
|
||||||
|
|
||||||
|
/// Save the collection state to the database.
|
||||||
|
fn save_to_database(&mut self) -> Result<(), Error>;
|
||||||
|
|
||||||
|
/// Get the current collection.
|
||||||
|
fn get_collection(&self) -> &Collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The collection manager. It is responsible for pulling information from both the library and the
|
||||||
|
/// database, ensuring its consistent and writing back any changes.
|
||||||
|
pub struct MhCollectionManager {
|
||||||
|
library: Box<dyn Library + Send + Sync>,
|
||||||
|
database: Box<dyn Database + Send + Sync>,
|
||||||
|
collection: Collection,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MhCollectionManager {
|
||||||
|
/// Create a new [`CollectionManager`] with the provided [`Library`] and [`Database`].
|
||||||
|
pub fn new(
|
||||||
|
library: Box<dyn Library + Send + Sync>,
|
||||||
|
database: Box<dyn Database + Send + Sync>,
|
||||||
|
) -> Self {
|
||||||
|
MhCollectionManager {
|
||||||
|
library,
|
||||||
|
database,
|
||||||
|
collection: vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CollectionManager for MhCollectionManager {
|
||||||
|
fn rescan_library(&mut self) -> Result<(), Error> {
|
||||||
|
self.collection = self.library.list(&Query::default())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_to_database(&mut self) -> Result<(), Error> {
|
||||||
|
self.database.write(&self.collection)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_collection(&self) -> &Collection {
|
||||||
|
&self.collection
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use mockall::predicate;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
database::{self, MockDatabase},
|
||||||
|
library::{self, MockLibrary, Query},
|
||||||
|
tests::COLLECTION,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{CollectionManager, Error, MhCollectionManager};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read_get_write() {
|
||||||
|
let mut library = MockLibrary::new();
|
||||||
|
let mut database = MockDatabase::new();
|
||||||
|
|
||||||
|
let library_input = Query::default();
|
||||||
|
let library_result = Ok(COLLECTION.to_owned());
|
||||||
|
|
||||||
|
let database_input = COLLECTION.to_owned();
|
||||||
|
let database_result = Ok(());
|
||||||
|
|
||||||
|
library
|
||||||
|
.expect_list()
|
||||||
|
.with(predicate::eq(library_input))
|
||||||
|
.times(1)
|
||||||
|
.return_once(|_| library_result);
|
||||||
|
|
||||||
|
database
|
||||||
|
.expect_write()
|
||||||
|
.with(predicate::eq(database_input))
|
||||||
|
.times(1)
|
||||||
|
.return_once(|_| database_result);
|
||||||
|
|
||||||
|
let mut collection_manager =
|
||||||
|
MhCollectionManager::new(Box::new(library), Box::new(database));
|
||||||
|
|
||||||
|
collection_manager.rescan_library().unwrap();
|
||||||
|
assert_eq!(collection_manager.get_collection(), &*COLLECTION);
|
||||||
|
collection_manager.save_to_database().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn library_error() {
|
||||||
|
let mut library = MockLibrary::new();
|
||||||
|
let database = MockDatabase::new();
|
||||||
|
|
||||||
|
let library_result = Err(library::Error::InvalidData(String::from("invalid data")));
|
||||||
|
|
||||||
|
library
|
||||||
|
.expect_list()
|
||||||
|
.times(1)
|
||||||
|
.return_once(|_| library_result);
|
||||||
|
|
||||||
|
let mut collection_manager =
|
||||||
|
MhCollectionManager::new(Box::new(library), Box::new(database));
|
||||||
|
|
||||||
|
let actual_err = collection_manager.rescan_library().unwrap_err();
|
||||||
|
let expected_err = Error::LibraryError(
|
||||||
|
library::Error::InvalidData(String::from("invalid data")).to_string(),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual_err, expected_err);
|
||||||
|
assert_eq!(actual_err.to_string(), expected_err.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn database_error() {
|
||||||
|
let library = MockLibrary::new();
|
||||||
|
let mut database = MockDatabase::new();
|
||||||
|
|
||||||
|
let database_result = Err(database::Error::IoError(String::from("I/O error")));
|
||||||
|
|
||||||
|
database
|
||||||
|
.expect_write()
|
||||||
|
.times(1)
|
||||||
|
.return_once(|_| database_result);
|
||||||
|
|
||||||
|
let mut collection_manager =
|
||||||
|
MhCollectionManager::new(Box::new(library), Box::new(database));
|
||||||
|
|
||||||
|
let actual_err = collection_manager.save_to_database().unwrap_err();
|
||||||
|
let expected_err =
|
||||||
|
Error::DatabaseError(database::Error::IoError(String::from("I/O error")).to_string());
|
||||||
|
|
||||||
|
assert_eq!(actual_err, expected_err);
|
||||||
|
assert_eq!(actual_err.to_string(), expected_err.to_string());
|
||||||
|
}
|
||||||
|
}
|
@ -3,13 +3,18 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use crate::collection::Collection;
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use mockall::automock;
|
use mockall::automock;
|
||||||
|
|
||||||
use super::{Database, DatabaseRead, DatabaseWrite};
|
use super::{Database, Error};
|
||||||
|
|
||||||
|
impl From<serde_json::Error> for Error {
|
||||||
|
fn from(err: serde_json::Error) -> Error {
|
||||||
|
Error::SerDeError(err.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Trait for the JSON database backend.
|
/// Trait for the JSON database backend.
|
||||||
#[cfg_attr(test, automock)]
|
#[cfg_attr(test, automock)]
|
||||||
@ -23,47 +28,37 @@ pub trait JsonDatabaseBackend {
|
|||||||
|
|
||||||
/// JSON database.
|
/// JSON database.
|
||||||
pub struct JsonDatabase {
|
pub struct JsonDatabase {
|
||||||
backend: Box<dyn JsonDatabaseBackend + Send>,
|
backend: Box<dyn JsonDatabaseBackend + Send + Sync>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JsonDatabase {
|
impl JsonDatabase {
|
||||||
/// Create a new JSON database with the provided backend, e.g. [JsonDatabaseFileBackend].
|
/// Create a new JSON database with the provided backend, e.g. [`JsonDatabaseFileBackend`].
|
||||||
pub fn new(backend: Box<dyn JsonDatabaseBackend + Send>) -> Self {
|
pub fn new(backend: Box<dyn JsonDatabaseBackend + Send + Sync>) -> Self {
|
||||||
JsonDatabase { backend }
|
JsonDatabase { backend }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DatabaseRead for JsonDatabase {
|
impl Database for JsonDatabase {
|
||||||
fn read<D>(&self, collection: &mut D) -> Result<(), std::io::Error>
|
fn read(&self, collection: &mut Collection) -> Result<(), Error> {
|
||||||
where
|
|
||||||
D: DeserializeOwned,
|
|
||||||
{
|
|
||||||
let serialized = self.backend.read()?;
|
let serialized = self.backend.read()?;
|
||||||
*collection = serde_json::from_str(&serialized)?;
|
*collection = serde_json::from_str(&serialized)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl DatabaseWrite for JsonDatabase {
|
fn write(&mut self, collection: &Collection) -> Result<(), Error> {
|
||||||
fn write<S>(&mut self, collection: &S) -> Result<(), std::io::Error>
|
|
||||||
where
|
|
||||||
S: Serialize,
|
|
||||||
{
|
|
||||||
let serialized = serde_json::to_string(&collection)?;
|
let serialized = serde_json::to_string(&collection)?;
|
||||||
self.backend.write(&serialized)?;
|
self.backend.write(&serialized)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Database for JsonDatabase {}
|
|
||||||
|
|
||||||
/// JSON database backend that uses a local file for persistent storage.
|
/// JSON database backend that uses a local file for persistent storage.
|
||||||
pub struct JsonDatabaseFileBackend {
|
pub struct JsonDatabaseFileBackend {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JsonDatabaseFileBackend {
|
impl JsonDatabaseFileBackend {
|
||||||
/// Create a [JsonDatabaseFileBackend] that will read/write to the provided path.
|
/// Create a [`JsonDatabaseFileBackend`] that will read/write to the provided path.
|
||||||
pub fn new(path: &Path) -> Self {
|
pub fn new(path: &Path) -> Self {
|
||||||
JsonDatabaseFileBackend {
|
JsonDatabaseFileBackend {
|
||||||
path: path.to_path_buf(),
|
path: path.to_path_buf(),
|
||||||
|
@ -1,25 +1,46 @@
|
|||||||
//! Module for storing MusicHoard data in a database.
|
//! Module for storing MusicHoard data in a database.
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use std::fmt;
|
||||||
use serde::Serialize;
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use mockall::automock;
|
||||||
|
|
||||||
|
use crate::collection::Collection;
|
||||||
|
|
||||||
pub mod json;
|
pub mod json;
|
||||||
|
|
||||||
/// Trait for database reads.
|
/// Error type for database calls.
|
||||||
pub trait DatabaseRead {
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
/// The database experienced an I/O error.
|
||||||
|
IoError(String),
|
||||||
|
/// The database experienced a (de)serialisation error.
|
||||||
|
SerDeError(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
Self::IoError(ref s) => write!(f, "the database experienced an I/O error: {s}"),
|
||||||
|
Self::SerDeError(ref s) => {
|
||||||
|
write!(f, "the database experienced a (de)serialisation error: {s}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for Error {
|
||||||
|
fn from(err: std::io::Error) -> Error {
|
||||||
|
Error::IoError(err.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait for interacting with the database.
|
||||||
|
#[cfg_attr(test, automock)]
|
||||||
|
pub trait Database {
|
||||||
/// Read collection from the database.
|
/// Read collection from the database.
|
||||||
fn read<D>(&self, collection: &mut D) -> Result<(), std::io::Error>
|
fn read(&self, collection: &mut Collection) -> Result<(), Error>;
|
||||||
where
|
|
||||||
D: DeserializeOwned;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Trait for database writes.
|
|
||||||
pub trait DatabaseWrite {
|
|
||||||
/// Write collection to the database.
|
/// Write collection to the database.
|
||||||
fn write<S>(&mut self, collection: &S) -> Result<(), std::io::Error>
|
fn write(&mut self, collection: &Collection) -> Result<(), Error>;
|
||||||
where
|
|
||||||
S: Serialize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for database reads and writes.
|
|
||||||
pub trait Database: DatabaseRead + DatabaseWrite {}
|
|
||||||
|
98
src/lib.rs
98
src/lib.rs
@ -3,6 +3,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub mod collection;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod library;
|
pub mod library;
|
||||||
|
|
||||||
@ -53,97 +54,14 @@ pub struct Artist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
#[macro_use]
|
||||||
use super::*;
|
mod testing;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
|
use super::*;
|
||||||
vec![
|
|
||||||
Artist {
|
pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| collection!());
|
||||||
id: ArtistId {
|
|
||||||
name: "album_artist a".to_string(),
|
|
||||||
},
|
|
||||||
albums: vec![
|
|
||||||
Album {
|
|
||||||
id: AlbumId {
|
|
||||||
year: 1998,
|
|
||||||
title: "album_title a.a".to_string(),
|
|
||||||
},
|
|
||||||
tracks: vec![
|
|
||||||
Track {
|
|
||||||
number: 1,
|
|
||||||
title: "track a.a.1".to_string(),
|
|
||||||
artist: vec!["artist a.a.1".to_string()],
|
|
||||||
format: TrackFormat::Flac,
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
number: 2,
|
|
||||||
title: "track a.a.2".to_string(),
|
|
||||||
artist: vec![
|
|
||||||
"artist a.a.2.1".to_string(),
|
|
||||||
"artist a.a.2.2".to_string(),
|
|
||||||
],
|
|
||||||
format: TrackFormat::Flac,
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
number: 3,
|
|
||||||
title: "track a.a.3".to_string(),
|
|
||||||
artist: vec!["artist a.a.3".to_string()],
|
|
||||||
format: TrackFormat::Flac,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
Album {
|
|
||||||
id: AlbumId {
|
|
||||||
year: 2015,
|
|
||||||
title: "album_title a.b".to_string(),
|
|
||||||
},
|
|
||||||
tracks: vec![
|
|
||||||
Track {
|
|
||||||
number: 1,
|
|
||||||
title: "track a.b.1".to_string(),
|
|
||||||
artist: vec!["artist a.b.1".to_string()],
|
|
||||||
format: TrackFormat::Mp3,
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
number: 2,
|
|
||||||
title: "track a.b.2".to_string(),
|
|
||||||
artist: vec!["artist a.b.2".to_string()],
|
|
||||||
format: TrackFormat::Flac,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
Artist {
|
|
||||||
id: ArtistId {
|
|
||||||
name: "album_artist b.a".to_string(),
|
|
||||||
},
|
|
||||||
albums: vec![Album {
|
|
||||||
id: AlbumId {
|
|
||||||
year: 2003,
|
|
||||||
title: "album_title b.a".to_string(),
|
|
||||||
},
|
|
||||||
tracks: vec![
|
|
||||||
Track {
|
|
||||||
number: 1,
|
|
||||||
title: "track b.a.1".to_string(),
|
|
||||||
artist: vec!["artist b.a.1".to_string()],
|
|
||||||
format: TrackFormat::Mp3,
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
number: 2,
|
|
||||||
title: "track b.a.2".to_string(),
|
|
||||||
artist: vec![
|
|
||||||
"artist b.a.2.1".to_string(),
|
|
||||||
"artist b.a.2.2".to_string(),
|
|
||||||
],
|
|
||||||
format: TrackFormat::Mp3,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ pub trait BeetsLibraryExecutor {
|
|||||||
|
|
||||||
/// Beets library.
|
/// Beets library.
|
||||||
pub struct BeetsLibrary {
|
pub struct BeetsLibrary {
|
||||||
executor: Box<dyn BeetsLibraryExecutor + Send>,
|
executor: Box<dyn BeetsLibraryExecutor + Send + Sync>,
|
||||||
}
|
}
|
||||||
|
|
||||||
trait LibraryPrivate {
|
trait LibraryPrivate {
|
||||||
@ -110,8 +110,8 @@ trait LibraryPrivate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BeetsLibrary {
|
impl BeetsLibrary {
|
||||||
/// Create a new beets library with the provided executor, e.g. [BeetsLibraryCommandExecutor].
|
/// Create a new beets library with the provided executor, e.g. [`BeetsLibraryCommandExecutor`].
|
||||||
pub fn new(executor: Box<dyn BeetsLibraryExecutor + Send>) -> BeetsLibrary {
|
pub fn new(executor: Box<dyn BeetsLibraryExecutor + Send + Sync>) -> BeetsLibrary {
|
||||||
BeetsLibrary { executor }
|
BeetsLibrary { executor }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -247,7 +247,7 @@ pub struct BeetsLibraryCommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BeetsLibraryCommandExecutor {
|
impl BeetsLibraryCommandExecutor {
|
||||||
/// Create a new [BeetsLibraryCommandExecutor] that uses the provided beets executable.
|
/// Create a new [`BeetsLibraryCommandExecutor`] that uses the provided beets executable.
|
||||||
pub fn new(bin: &str) -> Self {
|
pub fn new(bin: &str) -> Self {
|
||||||
BeetsLibraryCommandExecutor {
|
BeetsLibraryCommandExecutor {
|
||||||
bin: bin.to_string(),
|
bin: bin.to_string(),
|
||||||
@ -263,7 +263,7 @@ impl BeetsLibraryCommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Default for BeetsLibraryCommandExecutor {
|
impl Default for BeetsLibraryCommandExecutor {
|
||||||
/// Create a new [BeetsLibraryCommandExecutor] that uses the system's default beets executable.
|
/// Create a new [`BeetsLibraryCommandExecutor`] that uses the system's default beets executable.
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
BeetsLibraryCommandExecutor::new("beet")
|
BeetsLibraryCommandExecutor::new("beet")
|
||||||
}
|
}
|
||||||
@ -404,13 +404,14 @@ mod tests {
|
|||||||
// Putting the last track first will make the entire artist come first in the output.
|
// Putting the last track first will make the entire artist come first in the output.
|
||||||
expected.rotate_right(1);
|
expected.rotate_right(1);
|
||||||
|
|
||||||
// Same applies to that artists' albums, but here the artist has only one album.
|
// Same applies to that artists' albums.
|
||||||
assert_eq!(expected[0].albums.len(), 1);
|
expected[0].albums.rotate_right(1);
|
||||||
|
|
||||||
// Same applies to that album's tracks.
|
// Same applies to that album's tracks.
|
||||||
expected[0].albums[0].tracks.rotate_right(1);
|
expected[0].albums[0].tracks.rotate_right(1);
|
||||||
|
|
||||||
// And the (now) second album's tracks first track comes last.
|
// And the original first album's (now the first album of the second artist) tracks first
|
||||||
|
// track comes last.
|
||||||
expected[1].albums[0].tracks.rotate_left(1);
|
expected[1].albums[0].tracks.rotate_left(1);
|
||||||
|
|
||||||
let mut executor = MockBeetsLibraryExecutor::new();
|
let mut executor = MockBeetsLibraryExecutor::new();
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
//! Module for interacting with the music library.
|
//! Module for interacting with the music library.
|
||||||
|
|
||||||
use std::{num::ParseIntError, str::Utf8Error};
|
use std::{num::ParseIntError, str::Utf8Error, fmt};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use mockall::automock;
|
||||||
|
|
||||||
use crate::Artist;
|
use crate::Artist;
|
||||||
|
|
||||||
pub mod beets;
|
pub mod beets;
|
||||||
|
|
||||||
/// A single query option.
|
/// A single query option.
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum QueryOption<T> {
|
pub enum QueryOption<T> {
|
||||||
/// Inclusive query.
|
/// Inclusive query.
|
||||||
Include(T),
|
Include(T),
|
||||||
@ -17,26 +21,26 @@ pub enum QueryOption<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> QueryOption<T> {
|
impl<T> QueryOption<T> {
|
||||||
/// Return `true` if [QueryOption] is not [QueryOption::None].
|
/// Return `true` if [`QueryOption`] is not [`QueryOption::None`].
|
||||||
pub fn is_some(&self) -> bool {
|
pub fn is_some(&self) -> bool {
|
||||||
!matches!(self, QueryOption::None)
|
!matches!(self, QueryOption::None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if [QueryOption] is [QueryOption::None].
|
/// Return `true` if [`QueryOption`] is [`QueryOption::None`].
|
||||||
pub fn is_none(&self) -> bool {
|
pub fn is_none(&self) -> bool {
|
||||||
matches!(self, QueryOption::None)
|
matches!(self, QueryOption::None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Default for QueryOption<T> {
|
impl<T> Default for QueryOption<T> {
|
||||||
/// Create a [QueryOption::None] for type `T`.
|
/// Create a [`QueryOption::None`] for type `T`.
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::None
|
Self::None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Options for refining library queries.
|
/// Options for refining library queries.
|
||||||
#[derive(Default)]
|
#[derive(Debug, Default, PartialEq, Eq)]
|
||||||
pub struct Query {
|
pub struct Query {
|
||||||
album_artist: QueryOption<String>,
|
album_artist: QueryOption<String>,
|
||||||
album_year: QueryOption<u32>,
|
album_year: QueryOption<u32>,
|
||||||
@ -111,6 +115,18 @@ pub enum Error {
|
|||||||
Utf8Error(String),
|
Utf8Error(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
Self::CmdExecError(ref s) => write!(f, "the library failed to execute a command: {s}"),
|
||||||
|
Self::InvalidData(ref s) => write!(f, "the library received invalid data: {s}"),
|
||||||
|
Self::IoError(ref s) => write!(f, "the library experienced an I/O error: {s}"),
|
||||||
|
Self::ParseIntError(ref s) => write!(f, "the library received an invalid integer: {s}"),
|
||||||
|
Self::Utf8Error(ref s) => write!(f, "the library received invalid UTF-8: {s}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
fn from(err: std::io::Error) -> Error {
|
fn from(err: std::io::Error) -> Error {
|
||||||
Error::IoError(err.to_string())
|
Error::IoError(err.to_string())
|
||||||
@ -130,6 +146,7 @@ impl From<Utf8Error> for Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for interacting with the music library.
|
/// Trait for interacting with the music library.
|
||||||
|
#[cfg_attr(test, automock)]
|
||||||
pub trait Library {
|
pub trait Library {
|
||||||
/// List lirbary items that match the a specific query.
|
/// List lirbary items that match the a specific query.
|
||||||
fn list(&mut self, query: &Query) -> Result<Vec<Artist>, Error>;
|
fn list(&mut self, query: &Query) -> Result<Vec<Artist>, Error>;
|
||||||
|
75
src/main.rs
75
src/main.rs
@ -1,23 +1,27 @@
|
|||||||
|
use ratatui::backend::CrosstermBackend;
|
||||||
|
use ratatui::Terminal;
|
||||||
|
use std::io;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
use musichoard::{
|
use musichoard::{
|
||||||
database::{
|
collection::MhCollectionManager,
|
||||||
json::{JsonDatabase, JsonDatabaseFileBackend},
|
database::json::{JsonDatabase, JsonDatabaseFileBackend},
|
||||||
DatabaseWrite,
|
library::beets::{BeetsLibrary, BeetsLibraryCommandExecutor},
|
||||||
},
|
};
|
||||||
library::{
|
|
||||||
beets::{BeetsLibrary, BeetsLibraryCommandExecutor},
|
mod tui;
|
||||||
Library, Query,
|
use tui::{
|
||||||
},
|
app::App, event::EventChannel, handler::TuiEventHandler, listener::TuiEventListener, ui::Ui,
|
||||||
|
Tui,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
struct Opt {
|
struct Opt {
|
||||||
#[structopt(
|
#[structopt(
|
||||||
short = "b",
|
short = "b",
|
||||||
long = "beets-config",
|
long = "beets",
|
||||||
name = "beets config file path",
|
name = "beets config file path",
|
||||||
parse(from_os_str)
|
parse(from_os_str)
|
||||||
)]
|
)]
|
||||||
@ -34,21 +38,56 @@ struct Opt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Create the application.
|
||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
|
|
||||||
let mut beets = BeetsLibrary::new(Box::new(
|
let beets = BeetsLibrary::new(Box::new(
|
||||||
BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()),
|
BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()),
|
||||||
));
|
));
|
||||||
|
|
||||||
let collection = beets
|
let database = JsonDatabase::new(Box::new(JsonDatabaseFileBackend::new(
|
||||||
.list(&Query::new())
|
|
||||||
.expect("failed to query the library");
|
|
||||||
|
|
||||||
let mut database = JsonDatabase::new(Box::new(JsonDatabaseFileBackend::new(
|
|
||||||
&opt.database_file_path,
|
&opt.database_file_path,
|
||||||
)));
|
)));
|
||||||
|
|
||||||
database
|
let collection_manager = MhCollectionManager::new(Box::new(beets), Box::new(database));
|
||||||
.write(&collection)
|
|
||||||
.expect("failed to write to the database");
|
// Initialize the terminal user interface.
|
||||||
|
let backend = CrosstermBackend::new(io::stdout());
|
||||||
|
let terminal = Terminal::new(backend).expect("failed to initialise terminal");
|
||||||
|
|
||||||
|
let channel = EventChannel::new();
|
||||||
|
let listener = TuiEventListener::new(channel.sender());
|
||||||
|
let handler = TuiEventHandler::new(channel.receiver());
|
||||||
|
|
||||||
|
let ui = Ui::new();
|
||||||
|
|
||||||
|
let app = App::new(Box::new(collection_manager)).expect("failed to initialise app");
|
||||||
|
|
||||||
|
// Run the TUI application.
|
||||||
|
Tui::run(terminal, app, ui, handler, listener).expect("failed to run tui");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[macro_use]
|
||||||
|
mod testing;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use mockall::mock;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
|
use musichoard::collection::{self, Collection, CollectionManager};
|
||||||
|
use musichoard::*;
|
||||||
|
|
||||||
|
pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| collection!());
|
||||||
|
|
||||||
|
mock! {
|
||||||
|
pub CollectionManager {}
|
||||||
|
|
||||||
|
impl CollectionManager for CollectionManager {
|
||||||
|
fn rescan_library(&mut self) -> Result<(), collection::Error>;
|
||||||
|
fn save_to_database(&mut self) -> Result<(), collection::Error>;
|
||||||
|
fn get_collection(&self) -> &Collection;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
168
src/testing.rs
Normal file
168
src/testing.rs
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
macro_rules! collection {
|
||||||
|
() => {
|
||||||
|
vec![
|
||||||
|
Artist {
|
||||||
|
id: ArtistId {
|
||||||
|
name: "album_artist a".to_string(),
|
||||||
|
},
|
||||||
|
albums: vec![
|
||||||
|
Album {
|
||||||
|
id: AlbumId {
|
||||||
|
year: 1998,
|
||||||
|
title: "album_title a.a".to_string(),
|
||||||
|
},
|
||||||
|
tracks: vec![
|
||||||
|
Track {
|
||||||
|
number: 1,
|
||||||
|
title: "track a.a.1".to_string(),
|
||||||
|
artist: vec!["artist a.a.1".to_string()],
|
||||||
|
format: TrackFormat::Flac,
|
||||||
|
},
|
||||||
|
Track {
|
||||||
|
number: 2,
|
||||||
|
title: "track a.a.2".to_string(),
|
||||||
|
artist: vec![
|
||||||
|
"artist a.a.2.1".to_string(),
|
||||||
|
"artist a.a.2.2".to_string(),
|
||||||
|
],
|
||||||
|
format: TrackFormat::Mp3,
|
||||||
|
},
|
||||||
|
Track {
|
||||||
|
number: 3,
|
||||||
|
title: "track a.a.3".to_string(),
|
||||||
|
artist: vec!["artist a.a.3".to_string()],
|
||||||
|
format: TrackFormat::Flac,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Album {
|
||||||
|
id: AlbumId {
|
||||||
|
year: 2015,
|
||||||
|
title: "album_title a.b".to_string(),
|
||||||
|
},
|
||||||
|
tracks: vec![
|
||||||
|
Track {
|
||||||
|
number: 1,
|
||||||
|
title: "track a.b.1".to_string(),
|
||||||
|
artist: vec!["artist a.b.1".to_string()],
|
||||||
|
format: TrackFormat::Flac,
|
||||||
|
},
|
||||||
|
Track {
|
||||||
|
number: 2,
|
||||||
|
title: "track a.b.2".to_string(),
|
||||||
|
artist: vec!["artist a.b.2".to_string()],
|
||||||
|
format: TrackFormat::Flac,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Artist {
|
||||||
|
id: ArtistId {
|
||||||
|
name: "album_artist b".to_string(),
|
||||||
|
},
|
||||||
|
albums: vec![
|
||||||
|
Album {
|
||||||
|
id: AlbumId {
|
||||||
|
year: 2003,
|
||||||
|
title: "album_title b.a".to_string(),
|
||||||
|
},
|
||||||
|
tracks: vec![
|
||||||
|
Track {
|
||||||
|
number: 1,
|
||||||
|
title: "track b.a.1".to_string(),
|
||||||
|
artist: vec!["artist b.a.1".to_string()],
|
||||||
|
format: TrackFormat::Mp3,
|
||||||
|
},
|
||||||
|
Track {
|
||||||
|
number: 2,
|
||||||
|
title: "track b.a.2".to_string(),
|
||||||
|
artist: vec![
|
||||||
|
"artist b.a.2.1".to_string(),
|
||||||
|
"artist b.a.2.2".to_string(),
|
||||||
|
],
|
||||||
|
format: TrackFormat::Mp3,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Album {
|
||||||
|
id: AlbumId {
|
||||||
|
year: 2008,
|
||||||
|
title: "album_title b.b".to_string(),
|
||||||
|
},
|
||||||
|
tracks: vec![
|
||||||
|
Track {
|
||||||
|
number: 1,
|
||||||
|
title: "track b.b.1".to_string(),
|
||||||
|
artist: vec!["artist b.b.1".to_string()],
|
||||||
|
format: TrackFormat::Flac,
|
||||||
|
},
|
||||||
|
Track {
|
||||||
|
number: 2,
|
||||||
|
title: "track b.b.2".to_string(),
|
||||||
|
artist: vec![
|
||||||
|
"artist b.b.2.1".to_string(),
|
||||||
|
"artist b.b.2.2".to_string(),
|
||||||
|
],
|
||||||
|
format: TrackFormat::Mp3,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Artist {
|
||||||
|
id: ArtistId {
|
||||||
|
name: "album_artist c".to_string(),
|
||||||
|
},
|
||||||
|
albums: vec![
|
||||||
|
Album {
|
||||||
|
id: AlbumId {
|
||||||
|
year: 1985,
|
||||||
|
title: "album_title c.a".to_string(),
|
||||||
|
},
|
||||||
|
tracks: vec![
|
||||||
|
Track {
|
||||||
|
number: 1,
|
||||||
|
title: "track c.a.1".to_string(),
|
||||||
|
artist: vec!["artist c.a.1".to_string()],
|
||||||
|
format: TrackFormat::Mp3,
|
||||||
|
},
|
||||||
|
Track {
|
||||||
|
number: 2,
|
||||||
|
title: "track c.a.2".to_string(),
|
||||||
|
artist: vec![
|
||||||
|
"artist c.a.2.1".to_string(),
|
||||||
|
"artist c.a.2.2".to_string(),
|
||||||
|
],
|
||||||
|
format: TrackFormat::Mp3,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
Album {
|
||||||
|
id: AlbumId {
|
||||||
|
year: 2018,
|
||||||
|
title: "album_title c.b".to_string(),
|
||||||
|
},
|
||||||
|
tracks: vec![
|
||||||
|
Track {
|
||||||
|
number: 1,
|
||||||
|
title: "track c.b.1".to_string(),
|
||||||
|
artist: vec!["artist c.b.1".to_string()],
|
||||||
|
format: TrackFormat::Flac,
|
||||||
|
},
|
||||||
|
Track {
|
||||||
|
number: 2,
|
||||||
|
title: "track c.b.2".to_string(),
|
||||||
|
artist: vec![
|
||||||
|
"artist c.b.2.1".to_string(),
|
||||||
|
"artist c.b.2.2".to_string(),
|
||||||
|
],
|
||||||
|
format: TrackFormat::Flac,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
803
src/tui/app.rs
Normal file
803
src/tui/app.rs
Normal file
@ -0,0 +1,803 @@
|
|||||||
|
use musichoard::{collection::CollectionManager, Album, AlbumId, Artist, ArtistId, Track};
|
||||||
|
|
||||||
|
use super::Error;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub enum Category {
|
||||||
|
Artist,
|
||||||
|
Album,
|
||||||
|
Track,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TrackSelection {
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TrackSelection {
|
||||||
|
fn initialise(tracks: &Vec<Track>) -> Option<TrackSelection> {
|
||||||
|
if !tracks.is_empty() {
|
||||||
|
Some(TrackSelection { index: 0 })
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment(&mut self, tracks: &Vec<Track>) {
|
||||||
|
if let Some(result) = self.index.checked_add(1) {
|
||||||
|
if result < tracks.len() {
|
||||||
|
self.index = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement(&mut self, _tracks: &Vec<Track>) {
|
||||||
|
if let Some(result) = self.index.checked_sub(1) {
|
||||||
|
self.index = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AlbumSelection {
|
||||||
|
index: usize,
|
||||||
|
track: Option<TrackSelection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AlbumSelection {
|
||||||
|
fn initialise(albums: &Vec<Album>) -> Option<AlbumSelection> {
|
||||||
|
if !albums.is_empty() {
|
||||||
|
Some(AlbumSelection {
|
||||||
|
index: 0,
|
||||||
|
track: TrackSelection::initialise(&albums[0].tracks),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment(&mut self, albums: &Vec<Album>) {
|
||||||
|
if let Some(result) = self.index.checked_add(1) {
|
||||||
|
if result < albums.len() {
|
||||||
|
self.index = result;
|
||||||
|
self.track = TrackSelection::initialise(&albums[self.index].tracks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement(&mut self, albums: &Vec<Album>) {
|
||||||
|
if let Some(result) = self.index.checked_sub(1) {
|
||||||
|
self.index = result;
|
||||||
|
self.track = TrackSelection::initialise(&albums[self.index].tracks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ArtistSelection {
|
||||||
|
index: usize,
|
||||||
|
album: Option<AlbumSelection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ArtistSelection {
|
||||||
|
fn initialise(artists: &Vec<Artist>) -> Option<ArtistSelection> {
|
||||||
|
if !artists.is_empty() {
|
||||||
|
Some(ArtistSelection {
|
||||||
|
index: 0,
|
||||||
|
album: AlbumSelection::initialise(&artists[0].albums),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment(&mut self, artists: &Vec<Artist>) {
|
||||||
|
if let Some(result) = self.index.checked_add(1) {
|
||||||
|
if result < artists.len() {
|
||||||
|
self.index = result;
|
||||||
|
self.album = AlbumSelection::initialise(&artists[self.index].albums);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement(&mut self, artists: &Vec<Artist>) {
|
||||||
|
if let Some(result) = self.index.checked_sub(1) {
|
||||||
|
self.index = result;
|
||||||
|
self.album = AlbumSelection::initialise(&artists[self.index].albums);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Selection {
|
||||||
|
active: Category,
|
||||||
|
artist: Option<ArtistSelection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct App {
|
||||||
|
collection_manager: Box<dyn CollectionManager>,
|
||||||
|
selection: Selection,
|
||||||
|
running: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
pub fn new(mut collection_manager: Box<dyn CollectionManager>) -> Result<Self, Error> {
|
||||||
|
collection_manager.rescan_library()?;
|
||||||
|
let selection = Selection {
|
||||||
|
active: Category::Artist,
|
||||||
|
artist: ArtistSelection::initialise(collection_manager.get_collection()),
|
||||||
|
};
|
||||||
|
Ok(App {
|
||||||
|
collection_manager,
|
||||||
|
selection,
|
||||||
|
running: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_running(&self) -> bool {
|
||||||
|
self.running
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn increment_category(&mut self) {
|
||||||
|
self.selection.active = match self.selection.active {
|
||||||
|
Category::Artist => Category::Album,
|
||||||
|
Category::Album => Category::Track,
|
||||||
|
Category::Track => Category::Track,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decrement_category(&mut self) {
|
||||||
|
self.selection.active = match self.selection.active {
|
||||||
|
Category::Artist => Category::Artist,
|
||||||
|
Category::Album => Category::Artist,
|
||||||
|
Category::Track => Category::Album,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn increment_selection(&mut self) {
|
||||||
|
match self.selection.active {
|
||||||
|
Category::Artist => self.increment_artist_selection(),
|
||||||
|
Category::Album => self.increment_album_selection(),
|
||||||
|
Category::Track => self.increment_track_selection(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decrement_selection(&mut self) {
|
||||||
|
match self.selection.active {
|
||||||
|
Category::Artist => self.decrement_artist_selection(),
|
||||||
|
Category::Album => self.decrement_album_selection(),
|
||||||
|
Category::Track => self.decrement_track_selection(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment_artist_selection(&mut self) {
|
||||||
|
if let Some(ref mut artist_selection) = self.selection.artist {
|
||||||
|
let artists = &self.collection_manager.get_collection();
|
||||||
|
artist_selection.increment(artists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement_artist_selection(&mut self) {
|
||||||
|
if let Some(ref mut artist_selection) = self.selection.artist {
|
||||||
|
let artists = &self.collection_manager.get_collection();
|
||||||
|
artist_selection.decrement(artists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment_album_selection(&mut self) {
|
||||||
|
if let Some(ref mut artist_selection) = self.selection.artist {
|
||||||
|
if let Some(ref mut album_selection) = artist_selection.album {
|
||||||
|
let artists = &self.collection_manager.get_collection();
|
||||||
|
let albums = &artists[artist_selection.index].albums;
|
||||||
|
album_selection.increment(albums);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement_album_selection(&mut self) {
|
||||||
|
if let Some(ref mut artist_selection) = self.selection.artist {
|
||||||
|
if let Some(ref mut album_selection) = artist_selection.album {
|
||||||
|
let artists = &self.collection_manager.get_collection();
|
||||||
|
let albums = &artists[artist_selection.index].albums;
|
||||||
|
album_selection.decrement(albums);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment_track_selection(&mut self) {
|
||||||
|
if let Some(ref mut artist_selection) = self.selection.artist {
|
||||||
|
if let Some(ref mut album_selection) = artist_selection.album {
|
||||||
|
if let Some(ref mut track_selection) = album_selection.track {
|
||||||
|
let artists = &self.collection_manager.get_collection();
|
||||||
|
let albums = &artists[artist_selection.index].albums;
|
||||||
|
let tracks = &albums[album_selection.index].tracks;
|
||||||
|
track_selection.increment(tracks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement_track_selection(&mut self) {
|
||||||
|
if let Some(ref mut artist_selection) = self.selection.artist {
|
||||||
|
if let Some(ref mut album_selection) = artist_selection.album {
|
||||||
|
if let Some(ref mut track_selection) = album_selection.track {
|
||||||
|
let artists = &self.collection_manager.get_collection();
|
||||||
|
let albums = &artists[artist_selection.index].albums;
|
||||||
|
let tracks = &albums[album_selection.index].tracks;
|
||||||
|
track_selection.decrement(tracks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_active_category(&self) -> Category {
|
||||||
|
self.selection.active
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_artists(&self) -> &Vec<Artist> {
|
||||||
|
&self.collection_manager.get_collection()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_albums(&self) -> Option<&Vec<Album>> {
|
||||||
|
let artists = self.get_artists();
|
||||||
|
if let Some(artist_index) = self.selected_artist() {
|
||||||
|
Some(&artists[artist_index].albums)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_tracks(&self) -> Option<&Vec<Track>> {
|
||||||
|
if let Some(albums) = self.get_albums() {
|
||||||
|
if let Some(album_index) = self.selected_album() {
|
||||||
|
Some(&albums[album_index].tracks)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_artist_ids(&self) -> Vec<&ArtistId> {
|
||||||
|
let artists = self.get_artists();
|
||||||
|
artists.iter().map(|a| &a.id).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_album_ids(&self) -> Vec<&AlbumId> {
|
||||||
|
if let Some(albums) = self.get_albums() {
|
||||||
|
albums.iter().map(|a| &a.id).collect()
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_track_ids(&self) -> Vec<&Track> {
|
||||||
|
if let Some(tracks) = self.get_tracks() {
|
||||||
|
tracks.iter().collect()
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn selected_artist(&self) -> Option<usize> {
|
||||||
|
if let Some(ref artist_selection) = self.selection.artist {
|
||||||
|
Some(artist_selection.index)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn selected_album(&self) -> Option<usize> {
|
||||||
|
if let Some(ref artist_selection) = self.selection.artist {
|
||||||
|
if let Some(ref album_selection) = artist_selection.album {
|
||||||
|
Some(album_selection.index)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn selected_track(&self) -> Option<usize> {
|
||||||
|
if let Some(ref artist_selection) = self.selection.artist {
|
||||||
|
if let Some(ref album_selection) = artist_selection.album {
|
||||||
|
if let Some(ref track_selection) = album_selection.track {
|
||||||
|
Some(track_selection.index)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quit(&mut self) {
|
||||||
|
self.running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::tests::{MockCollectionManager, COLLECTION};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_track_selection() {
|
||||||
|
let tracks = &COLLECTION[0].albums[0].tracks;
|
||||||
|
assert!(tracks.len() > 1);
|
||||||
|
|
||||||
|
let empty = TrackSelection::initialise(&vec![]);
|
||||||
|
assert!(empty.is_none());
|
||||||
|
|
||||||
|
let sel = TrackSelection::initialise(tracks);
|
||||||
|
assert!(sel.is_some());
|
||||||
|
|
||||||
|
let mut sel = sel.unwrap();
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
|
||||||
|
sel.decrement(tracks);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
|
||||||
|
sel.increment(tracks);
|
||||||
|
assert_eq!(sel.index, 1);
|
||||||
|
|
||||||
|
sel.decrement(tracks);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
|
||||||
|
for _ in 0..(tracks.len() + 5) {
|
||||||
|
sel.increment(tracks);
|
||||||
|
}
|
||||||
|
assert_eq!(sel.index, tracks.len() - 1);
|
||||||
|
|
||||||
|
// Artifical test case to verify upper limit.
|
||||||
|
let mut sel = TrackSelection {
|
||||||
|
index: std::usize::MAX,
|
||||||
|
};
|
||||||
|
assert_eq!(sel.index, std::usize::MAX);
|
||||||
|
|
||||||
|
sel.increment(&vec![]);
|
||||||
|
assert_eq!(sel.index, std::usize::MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_album_selection() {
|
||||||
|
let albums = &COLLECTION[0].albums;
|
||||||
|
assert!(albums.len() > 1);
|
||||||
|
|
||||||
|
let empty = AlbumSelection::initialise(&vec![]);
|
||||||
|
assert!(empty.is_none());
|
||||||
|
|
||||||
|
let sel = AlbumSelection::initialise(albums);
|
||||||
|
assert!(sel.is_some());
|
||||||
|
|
||||||
|
let mut sel = sel.unwrap();
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
sel.track
|
||||||
|
.as_mut()
|
||||||
|
.unwrap()
|
||||||
|
.increment(&albums[sel.index].tracks);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
// Verify that decrement that doesn't change index does not reset track.
|
||||||
|
sel.decrement(albums);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
sel.increment(albums);
|
||||||
|
assert_eq!(sel.index, 1);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
sel.decrement(albums);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
for _ in 0..(albums.len() + 5) {
|
||||||
|
sel.increment(albums);
|
||||||
|
}
|
||||||
|
assert_eq!(sel.index, albums.len() - 1);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
sel.track
|
||||||
|
.as_mut()
|
||||||
|
.unwrap()
|
||||||
|
.increment(&albums[sel.index].tracks);
|
||||||
|
assert_eq!(sel.index, albums.len() - 1);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
// Verify that increment that doesn't change index does not reset track.
|
||||||
|
sel.increment(albums);
|
||||||
|
assert_eq!(sel.index, albums.len() - 1);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
// Artifical test case to verify upper limit.
|
||||||
|
let mut sel = AlbumSelection {
|
||||||
|
index: std::usize::MAX,
|
||||||
|
track: Some(TrackSelection { index: 1 }),
|
||||||
|
};
|
||||||
|
assert_eq!(sel.index, std::usize::MAX);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
sel.increment(&vec![]);
|
||||||
|
assert_eq!(sel.index, std::usize::MAX);
|
||||||
|
assert!(sel.track.is_some());
|
||||||
|
assert_eq!(sel.track.as_ref().unwrap().index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_artist_selection() {
|
||||||
|
let artists = &COLLECTION;
|
||||||
|
assert!(artists.len() > 1);
|
||||||
|
|
||||||
|
let empty = ArtistSelection::initialise(&vec![]);
|
||||||
|
assert!(empty.is_none());
|
||||||
|
|
||||||
|
let sel = ArtistSelection::initialise(artists);
|
||||||
|
assert!(sel.is_some());
|
||||||
|
|
||||||
|
let mut sel = sel.unwrap();
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
sel.album
|
||||||
|
.as_mut()
|
||||||
|
.unwrap()
|
||||||
|
.increment(&artists[sel.index].albums);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
// Verify that decrement that doesn't change index does not reset album.
|
||||||
|
sel.decrement(artists);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
sel.increment(artists);
|
||||||
|
assert_eq!(sel.index, 1);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
sel.decrement(artists);
|
||||||
|
assert_eq!(sel.index, 0);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
for _ in 0..(artists.len() + 5) {
|
||||||
|
sel.increment(artists);
|
||||||
|
}
|
||||||
|
assert_eq!(sel.index, artists.len() - 1);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 0);
|
||||||
|
|
||||||
|
sel.album
|
||||||
|
.as_mut()
|
||||||
|
.unwrap()
|
||||||
|
.increment(&artists[sel.index].albums);
|
||||||
|
assert_eq!(sel.index, artists.len() - 1);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
// Verify that increment that doesn't change index does not reset album.
|
||||||
|
sel.increment(artists);
|
||||||
|
assert_eq!(sel.index, artists.len() - 1);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
// Artifical test case to verify upper limit.
|
||||||
|
let mut sel = ArtistSelection {
|
||||||
|
index: std::usize::MAX,
|
||||||
|
album: Some(AlbumSelection {
|
||||||
|
index: 1,
|
||||||
|
track: None,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
assert_eq!(sel.index, std::usize::MAX);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 1);
|
||||||
|
|
||||||
|
sel.increment(&vec![]);
|
||||||
|
assert_eq!(sel.index, std::usize::MAX);
|
||||||
|
assert!(sel.album.is_some());
|
||||||
|
assert_eq!(sel.album.as_ref().unwrap().index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn app_running() {
|
||||||
|
let mut collection_manager = MockCollectionManager::new();
|
||||||
|
|
||||||
|
collection_manager
|
||||||
|
.expect_rescan_library()
|
||||||
|
.times(1)
|
||||||
|
.return_once(|| Ok(()));
|
||||||
|
collection_manager
|
||||||
|
.expect_get_collection()
|
||||||
|
.return_const(COLLECTION.to_owned());
|
||||||
|
|
||||||
|
let mut app = App::new(Box::new(collection_manager)).unwrap();
|
||||||
|
assert!(app.is_running());
|
||||||
|
|
||||||
|
app.quit();
|
||||||
|
assert!(!app.is_running());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn app_modifiers() {
|
||||||
|
let mut collection_manager = MockCollectionManager::new();
|
||||||
|
|
||||||
|
collection_manager
|
||||||
|
.expect_rescan_library()
|
||||||
|
.times(1)
|
||||||
|
.return_once(|| Ok(()));
|
||||||
|
collection_manager
|
||||||
|
.expect_get_collection()
|
||||||
|
.return_const(COLLECTION.to_owned());
|
||||||
|
|
||||||
|
let mut app = App::new(Box::new(collection_manager)).unwrap();
|
||||||
|
assert!(app.is_running());
|
||||||
|
|
||||||
|
assert!(!app.get_artist_ids().is_empty());
|
||||||
|
assert!(!app.get_album_ids().is_empty());
|
||||||
|
assert!(!app.get_track_ids().is_empty());
|
||||||
|
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(1));
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(1));
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
app.decrement_category();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(1));
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
app.decrement_category();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), Some(1));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
app.increment_selection();
|
||||||
|
app.decrement_category();
|
||||||
|
app.decrement_selection();
|
||||||
|
app.decrement_category();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), Some(1));
|
||||||
|
assert_eq!(app.selected_track(), Some(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn app_no_tracks() {
|
||||||
|
let mut collection_manager = MockCollectionManager::new();
|
||||||
|
let mut collection = COLLECTION.to_owned();
|
||||||
|
collection[0].albums[0].tracks = vec![];
|
||||||
|
|
||||||
|
collection_manager
|
||||||
|
.expect_rescan_library()
|
||||||
|
.times(1)
|
||||||
|
.return_once(|| Ok(()));
|
||||||
|
collection_manager
|
||||||
|
.expect_get_collection()
|
||||||
|
.return_const(collection);
|
||||||
|
|
||||||
|
let mut app = App::new(Box::new(collection_manager)).unwrap();
|
||||||
|
assert!(app.is_running());
|
||||||
|
|
||||||
|
assert!(!app.get_artist_ids().is_empty());
|
||||||
|
assert!(!app.get_album_ids().is_empty());
|
||||||
|
assert!(app.get_track_ids().is_empty());
|
||||||
|
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
app.increment_category();
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), Some(0));
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn app_no_albums() {
|
||||||
|
let mut collection_manager = MockCollectionManager::new();
|
||||||
|
let mut collection = COLLECTION.to_owned();
|
||||||
|
collection[0].albums = vec![];
|
||||||
|
|
||||||
|
collection_manager
|
||||||
|
.expect_rescan_library()
|
||||||
|
.times(1)
|
||||||
|
.return_once(|| Ok(()));
|
||||||
|
collection_manager
|
||||||
|
.expect_get_collection()
|
||||||
|
.return_const(collection);
|
||||||
|
|
||||||
|
let mut app = App::new(Box::new(collection_manager)).unwrap();
|
||||||
|
assert!(app.is_running());
|
||||||
|
|
||||||
|
assert!(!app.get_artist_ids().is_empty());
|
||||||
|
assert!(app.get_album_ids().is_empty());
|
||||||
|
assert!(app.get_track_ids().is_empty());
|
||||||
|
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), Some(0));
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn app_no_artists() {
|
||||||
|
let mut collection_manager = MockCollectionManager::new();
|
||||||
|
let collection = vec![];
|
||||||
|
|
||||||
|
collection_manager
|
||||||
|
.expect_rescan_library()
|
||||||
|
.times(1)
|
||||||
|
.return_once(|| Ok(()));
|
||||||
|
collection_manager
|
||||||
|
.expect_get_collection()
|
||||||
|
.return_const(collection);
|
||||||
|
|
||||||
|
let mut app = App::new(Box::new(collection_manager)).unwrap();
|
||||||
|
assert!(app.is_running());
|
||||||
|
|
||||||
|
assert!(app.get_artist_ids().is_empty());
|
||||||
|
assert!(app.get_album_ids().is_empty());
|
||||||
|
assert!(app.get_track_ids().is_empty());
|
||||||
|
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), None);
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), None);
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Artist);
|
||||||
|
assert_eq!(app.selected_artist(), None);
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), None);
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Album);
|
||||||
|
assert_eq!(app.selected_artist(), None);
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.increment_category();
|
||||||
|
|
||||||
|
app.increment_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), None);
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
|
||||||
|
app.decrement_selection();
|
||||||
|
assert_eq!(app.get_active_category(), Category::Track);
|
||||||
|
assert_eq!(app.selected_artist(), None);
|
||||||
|
assert_eq!(app.selected_album(), None);
|
||||||
|
assert_eq!(app.selected_track(), None);
|
||||||
|
}
|
||||||
|
}
|
145
src/tui/event.rs
Normal file
145
src/tui/event.rs
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
use crossterm::event::{KeyEvent, MouseEvent};
|
||||||
|
use std::fmt;
|
||||||
|
use std::sync::mpsc;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum EventError {
|
||||||
|
SendError(Event),
|
||||||
|
RecvError,
|
||||||
|
IoError(std::io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for EventError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match *self {
|
||||||
|
Self::SendError(ref e) => write!(f, "failed to send event: {e:?}"),
|
||||||
|
Self::RecvError => write!(f, "receive event call failed"),
|
||||||
|
Self::IoError(ref e) => {
|
||||||
|
write!(f, "an I/O error was triggered during event handling: {e}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<mpsc::SendError<Event>> for EventError {
|
||||||
|
fn from(err: mpsc::SendError<Event>) -> EventError {
|
||||||
|
EventError::SendError(err.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<mpsc::RecvError> for EventError {
|
||||||
|
fn from(_: mpsc::RecvError) -> EventError {
|
||||||
|
EventError::RecvError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum Event {
|
||||||
|
Key(KeyEvent),
|
||||||
|
Mouse(MouseEvent),
|
||||||
|
Resize(u16, u16),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EventChannel {
|
||||||
|
sender: mpsc::Sender<Event>,
|
||||||
|
receiver: mpsc::Receiver<Event>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EventSender {
|
||||||
|
sender: mpsc::Sender<Event>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EventReceiver {
|
||||||
|
receiver: mpsc::Receiver<Event>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventChannel {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let (sender, receiver) = mpsc::channel();
|
||||||
|
EventChannel { sender, receiver }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sender(&self) -> EventSender {
|
||||||
|
EventSender {
|
||||||
|
sender: self.sender.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn receiver(self) -> EventReceiver {
|
||||||
|
EventReceiver {
|
||||||
|
receiver: self.receiver,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventSender {
|
||||||
|
pub fn send(&self, event: Event) -> Result<(), EventError> {
|
||||||
|
Ok(self.sender.send(event)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventReceiver {
|
||||||
|
pub fn recv(&self) -> Result<Event, EventError> {
|
||||||
|
Ok(self.receiver.recv()?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
|
||||||
|
|
||||||
|
use super::{Event, EventChannel, EventError};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn event_sender() {
|
||||||
|
let channel = EventChannel::new();
|
||||||
|
let sender = channel.sender();
|
||||||
|
let receiver = channel.receiver();
|
||||||
|
let event = Event::Key(KeyEvent::new(KeyCode::Up, KeyModifiers::empty()));
|
||||||
|
|
||||||
|
let result = sender.send(event);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
|
||||||
|
drop(receiver);
|
||||||
|
let result = sender.send(event);
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn event_receiver() {
|
||||||
|
let channel = EventChannel::new();
|
||||||
|
let sender = channel.sender();
|
||||||
|
let receiver = channel.receiver();
|
||||||
|
let event = Event::Key(KeyEvent::new(KeyCode::Up, KeyModifiers::empty()));
|
||||||
|
|
||||||
|
sender.send(event).unwrap();
|
||||||
|
let result = receiver.recv();
|
||||||
|
assert!(result.is_ok());
|
||||||
|
|
||||||
|
drop(sender);
|
||||||
|
let result = receiver.recv();
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn errors() {
|
||||||
|
let send_err = EventError::SendError(Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Up,
|
||||||
|
modifiers: KeyModifiers::empty(),
|
||||||
|
kind: KeyEventKind::Press,
|
||||||
|
state: KeyEventState::NONE,
|
||||||
|
}));
|
||||||
|
let recv_err = EventError::RecvError;
|
||||||
|
let io_err = EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "interrupted"));
|
||||||
|
|
||||||
|
assert!(!send_err.to_string().is_empty());
|
||||||
|
assert!(!recv_err.to_string().is_empty());
|
||||||
|
assert!(!io_err.to_string().is_empty());
|
||||||
|
|
||||||
|
assert!(!format!("{:?}", send_err).is_empty());
|
||||||
|
assert!(!format!("{:?}", recv_err).is_empty());
|
||||||
|
assert!(!format!("{:?}", io_err).is_empty());
|
||||||
|
}
|
||||||
|
}
|
71
src/tui/handler.rs
Normal file
71
src/tui/handler.rs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use mockall::automock;
|
||||||
|
|
||||||
|
use super::{app::App, event::{Event, EventReceiver, EventError}};
|
||||||
|
|
||||||
|
#[cfg_attr(test, automock)]
|
||||||
|
pub trait EventHandler {
|
||||||
|
fn handle_next_event(&self, app: &mut App) -> Result<(), EventError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait EventHandlerPrivate {
|
||||||
|
fn handle_key_event(app: &mut App, key_event: KeyEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TuiEventHandler {
|
||||||
|
events: EventReceiver,
|
||||||
|
}
|
||||||
|
|
||||||
|
// GRCOV_EXCL_START
|
||||||
|
impl TuiEventHandler {
|
||||||
|
pub fn new(events: EventReceiver) -> Self {
|
||||||
|
TuiEventHandler { events }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventHandler for TuiEventHandler {
|
||||||
|
fn handle_next_event(&self, app: &mut App) -> Result<(), EventError> {
|
||||||
|
match self.events.recv()? {
|
||||||
|
Event::Key(key_event) => Self::handle_key_event(app, key_event),
|
||||||
|
Event::Mouse(_) => {}
|
||||||
|
Event::Resize(_, _) => {}
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventHandlerPrivate for TuiEventHandler {
|
||||||
|
fn handle_key_event(app: &mut App, key_event: KeyEvent) {
|
||||||
|
match key_event.code {
|
||||||
|
// Exit application on `ESC` or `q`.
|
||||||
|
KeyCode::Esc | KeyCode::Char('q') => {
|
||||||
|
app.quit();
|
||||||
|
}
|
||||||
|
// Exit application on `Ctrl-C`.
|
||||||
|
KeyCode::Char('c') | KeyCode::Char('C') => {
|
||||||
|
if key_event.modifiers == KeyModifiers::CONTROL {
|
||||||
|
app.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Category change.
|
||||||
|
KeyCode::Left => {
|
||||||
|
app.decrement_category();
|
||||||
|
}
|
||||||
|
KeyCode::Right => {
|
||||||
|
app.increment_category();
|
||||||
|
}
|
||||||
|
// Selection change.
|
||||||
|
KeyCode::Up => {
|
||||||
|
app.decrement_selection();
|
||||||
|
}
|
||||||
|
KeyCode::Down => {
|
||||||
|
app.increment_selection();
|
||||||
|
}
|
||||||
|
// Other keys.
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// GRCOV_EXCL_STOP
|
49
src/tui/listener.rs
Normal file
49
src/tui/listener.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use crossterm::event::{self, Event as CrosstermEvent};
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use mockall::automock;
|
||||||
|
|
||||||
|
use super::event::{EventSender, EventError, Event};
|
||||||
|
|
||||||
|
#[cfg_attr(test, automock)]
|
||||||
|
pub trait EventListener {
|
||||||
|
fn spawn(self) -> thread::JoinHandle<EventError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TuiEventListener {
|
||||||
|
events: EventSender,
|
||||||
|
}
|
||||||
|
|
||||||
|
// GRCOV_EXCL_START
|
||||||
|
impl TuiEventListener {
|
||||||
|
pub fn new(events: EventSender) -> Self {
|
||||||
|
TuiEventListener { events }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventListener for TuiEventListener {
|
||||||
|
fn spawn(self) -> thread::JoinHandle<EventError> {
|
||||||
|
thread::spawn(move || {
|
||||||
|
loop {
|
||||||
|
// Put this inside an if event::poll {...} if the display needs to be refreshed on a
|
||||||
|
// periodic basis. See
|
||||||
|
// https://github.com/tui-rs-revival/rust-tui-template/blob/master/src/event.rs.
|
||||||
|
match event::read() {
|
||||||
|
Ok(event) => {
|
||||||
|
if let Err(err) = match event {
|
||||||
|
CrosstermEvent::Key(e) => self.events.send(Event::Key(e)),
|
||||||
|
CrosstermEvent::Mouse(e) => self.events.send(Event::Mouse(e)),
|
||||||
|
CrosstermEvent::Resize(w, h) => self.events.send(Event::Resize(w, h)),
|
||||||
|
_ => unimplemented!(),
|
||||||
|
} {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => return EventError::IoError(err),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// GRCOV_EXCL_STOP
|
303
src/tui/mod.rs
Normal file
303
src/tui/mod.rs
Normal file
@ -0,0 +1,303 @@
|
|||||||
|
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||||
|
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
||||||
|
use musichoard::collection;
|
||||||
|
use ratatui::backend::Backend;
|
||||||
|
use ratatui::Terminal;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
pub mod app;
|
||||||
|
pub mod event;
|
||||||
|
pub mod handler;
|
||||||
|
pub mod listener;
|
||||||
|
pub mod ui;
|
||||||
|
|
||||||
|
use self::app::App;
|
||||||
|
use self::event::EventError;
|
||||||
|
use self::handler::EventHandler;
|
||||||
|
use self::listener::EventListener;
|
||||||
|
use self::ui::Ui;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum Error {
|
||||||
|
CollectionError(String),
|
||||||
|
IoError(String),
|
||||||
|
EventError(String),
|
||||||
|
ListenerPanic,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<collection::Error> for Error {
|
||||||
|
fn from(err: collection::Error) -> Error {
|
||||||
|
Error::CollectionError(err.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<io::Error> for Error {
|
||||||
|
fn from(err: io::Error) -> Error {
|
||||||
|
Error::IoError(err.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<EventError> for Error {
|
||||||
|
fn from(err: EventError) -> Error {
|
||||||
|
Error::EventError(err.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Tui<B: Backend> {
|
||||||
|
terminal: Terminal<B>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B: Backend> Tui<B> {
|
||||||
|
fn init(&mut self) -> Result<(), Error> {
|
||||||
|
self.terminal.hide_cursor()?;
|
||||||
|
self.terminal.clear()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exit(&mut self) -> Result<(), Error> {
|
||||||
|
self.terminal.show_cursor()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_must_use)]
|
||||||
|
fn exit_suppress_errors(&mut self) {
|
||||||
|
self.exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main_loop(&mut self, mut app: App, ui: Ui, handler: impl EventHandler) -> Result<(), Error> {
|
||||||
|
while app.is_running() {
|
||||||
|
self.terminal.draw(|frame| ui.render(&app, frame))?;
|
||||||
|
handler.handle_next_event(&mut app)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(
|
||||||
|
term: Terminal<B>,
|
||||||
|
app: App,
|
||||||
|
ui: Ui,
|
||||||
|
handler: impl EventHandler,
|
||||||
|
listener: impl EventListener,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let mut tui = Tui { terminal: term };
|
||||||
|
|
||||||
|
tui.init()?;
|
||||||
|
|
||||||
|
let listener_handle = listener.spawn();
|
||||||
|
let result = tui.main_loop(app, ui, handler);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_) => {
|
||||||
|
tui.exit()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
// We want to call exit before handling the result to reset the terminal. Therefore,
|
||||||
|
// we suppress exit errors (if any) to not mask the original error.
|
||||||
|
tui.exit_suppress_errors();
|
||||||
|
|
||||||
|
if listener_handle.is_finished() {
|
||||||
|
match listener_handle.join() {
|
||||||
|
Ok(err) => return Err(err.into()),
|
||||||
|
// Calling std::panic::resume_unwind(err) as recommended by the Rust docs
|
||||||
|
// will not produce an error message. The panic error message is printed at
|
||||||
|
// the location of the panic which at the time is hidden by the TUI.
|
||||||
|
Err(_) => return Err(Error::ListenerPanic),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GRCOV_EXCL_START
|
||||||
|
fn enable() -> Result<(), Error> {
|
||||||
|
terminal::enable_raw_mode()?;
|
||||||
|
crossterm::execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disable() -> Result<(), Error> {
|
||||||
|
terminal::disable_raw_mode()?;
|
||||||
|
crossterm::execute!(io::stdout(), LeaveAlternateScreen, DisableMouseCapture)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_must_use)]
|
||||||
|
fn disable_suppress_errors() {
|
||||||
|
Self::disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(
|
||||||
|
term: Terminal<B>,
|
||||||
|
app: App,
|
||||||
|
ui: Ui,
|
||||||
|
handler: impl EventHandler,
|
||||||
|
listener: impl EventListener,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
Self::enable()?;
|
||||||
|
let result = Self::main(term, app, ui, handler, listener);
|
||||||
|
match result {
|
||||||
|
Ok(_) => {
|
||||||
|
Self::disable()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
// We want to call disable before handling the result to reset the terminal.
|
||||||
|
// Therefore, we suppress disable errors (if any) to not mask the original error.
|
||||||
|
Self::disable_suppress_errors();
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// GRCOV_EXCL_STOP
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::{io, thread};
|
||||||
|
|
||||||
|
use musichoard::collection::{Collection, self};
|
||||||
|
use ratatui::{backend::TestBackend, Terminal};
|
||||||
|
|
||||||
|
use crate::tests::{MockCollectionManager, COLLECTION};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
app::App, event::EventError, handler::MockEventHandler, listener::MockEventListener,
|
||||||
|
ui::Ui, Error, Tui,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn terminal() -> Terminal<TestBackend> {
|
||||||
|
let backend = TestBackend::new(150, 30);
|
||||||
|
Terminal::new(backend).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn app(collection: Collection) -> App {
|
||||||
|
let mut collection_manager = MockCollectionManager::new();
|
||||||
|
|
||||||
|
collection_manager
|
||||||
|
.expect_rescan_library()
|
||||||
|
.returning(|| Ok(()));
|
||||||
|
collection_manager
|
||||||
|
.expect_get_collection()
|
||||||
|
.return_const(collection);
|
||||||
|
|
||||||
|
App::new(Box::new(collection_manager)).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn listener() -> MockEventListener {
|
||||||
|
let mut listener = MockEventListener::new();
|
||||||
|
listener.expect_spawn().return_once(|| {
|
||||||
|
thread::spawn(|| {
|
||||||
|
thread::park();
|
||||||
|
return EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "unparked"));
|
||||||
|
})
|
||||||
|
});
|
||||||
|
listener
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handler() -> MockEventHandler {
|
||||||
|
let mut handler = MockEventHandler::new();
|
||||||
|
handler.expect_handle_next_event().return_once(|app| {
|
||||||
|
app.quit();
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
handler
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run() {
|
||||||
|
let terminal = terminal();
|
||||||
|
let app = app(COLLECTION.to_owned());
|
||||||
|
let ui = Ui::new();
|
||||||
|
|
||||||
|
let listener = listener();
|
||||||
|
let handler = handler();
|
||||||
|
|
||||||
|
let result = Tui::main(terminal, app, ui, handler, listener);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn event_error() {
|
||||||
|
let terminal = terminal();
|
||||||
|
let app = app(COLLECTION.to_owned());
|
||||||
|
let ui = Ui::new();
|
||||||
|
|
||||||
|
let listener = listener();
|
||||||
|
|
||||||
|
let mut handler = MockEventHandler::new();
|
||||||
|
handler
|
||||||
|
.expect_handle_next_event()
|
||||||
|
.return_once(|_| Err(EventError::RecvError));
|
||||||
|
|
||||||
|
let result = Tui::main(terminal, app, ui, handler, listener);
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap_err(),
|
||||||
|
Error::EventError(EventError::RecvError.to_string())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn listener_error() {
|
||||||
|
let terminal = terminal();
|
||||||
|
let app = app(COLLECTION.to_owned());
|
||||||
|
let ui = Ui::new();
|
||||||
|
|
||||||
|
let error = EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "error"));
|
||||||
|
let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| error);
|
||||||
|
while !listener_handle.is_finished() {}
|
||||||
|
|
||||||
|
let mut listener = MockEventListener::new();
|
||||||
|
listener.expect_spawn().return_once(|| listener_handle);
|
||||||
|
|
||||||
|
let mut handler = MockEventHandler::new();
|
||||||
|
handler
|
||||||
|
.expect_handle_next_event()
|
||||||
|
.return_once(|_| Err(EventError::RecvError));
|
||||||
|
|
||||||
|
let result = Tui::main(terminal, app, ui, handler, listener);
|
||||||
|
assert!(result.is_err());
|
||||||
|
|
||||||
|
let error = EventError::IoError(io::Error::new(io::ErrorKind::Interrupted, "error"));
|
||||||
|
assert_eq!(result.unwrap_err(), Error::EventError(error.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn listener_panic() {
|
||||||
|
let terminal = terminal();
|
||||||
|
let app = app(COLLECTION.to_owned());
|
||||||
|
let ui = Ui::new();
|
||||||
|
|
||||||
|
let listener_handle: thread::JoinHandle<EventError> = thread::spawn(|| panic!());
|
||||||
|
while !listener_handle.is_finished() {}
|
||||||
|
|
||||||
|
let mut listener = MockEventListener::new();
|
||||||
|
listener.expect_spawn().return_once(|| listener_handle);
|
||||||
|
|
||||||
|
let mut handler = MockEventHandler::new();
|
||||||
|
handler
|
||||||
|
.expect_handle_next_event()
|
||||||
|
.return_once(|_| Err(EventError::RecvError));
|
||||||
|
|
||||||
|
let result = Tui::main(terminal, app, ui, handler, listener);
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err(), Error::ListenerPanic);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn errors() {
|
||||||
|
let collection_err: Error = collection::Error::DatabaseError(String::from("")).into();
|
||||||
|
let io_err: Error = io::Error::new(io::ErrorKind::Interrupted, "error").into();
|
||||||
|
let event_err: Error = EventError::RecvError.into();
|
||||||
|
let listener_err = Error::ListenerPanic;
|
||||||
|
|
||||||
|
assert!(!format!("{:?}", collection_err).is_empty());
|
||||||
|
assert!(!format!("{:?}", io_err).is_empty());
|
||||||
|
assert!(!format!("{:?}", event_err).is_empty());
|
||||||
|
assert!(!format!("{:?}", listener_err).is_empty());
|
||||||
|
}
|
||||||
|
}
|
360
src/tui/ui.rs
Normal file
360
src/tui/ui.rs
Normal file
@ -0,0 +1,360 @@
|
|||||||
|
use musichoard::TrackFormat;
|
||||||
|
use ratatui::{
|
||||||
|
backend::Backend,
|
||||||
|
layout::{Alignment, Rect},
|
||||||
|
style::{Color, Style},
|
||||||
|
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
|
||||||
|
Frame,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::app::{App, Category};
|
||||||
|
|
||||||
|
struct ArtistArea {
|
||||||
|
list: Rect,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AlbumArea {
|
||||||
|
list: Rect,
|
||||||
|
info: Rect,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TrackArea {
|
||||||
|
list: Rect,
|
||||||
|
info: Rect,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FrameAreas {
|
||||||
|
artists: ArtistArea,
|
||||||
|
albums: AlbumArea,
|
||||||
|
tracks: TrackArea,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SelectionList<'a> {
|
||||||
|
list: List<'a>,
|
||||||
|
state: ListState,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ArtistState<'a> {
|
||||||
|
list: SelectionList<'a>,
|
||||||
|
active: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AlbumState<'a> {
|
||||||
|
list: SelectionList<'a>,
|
||||||
|
info: Paragraph<'a>,
|
||||||
|
active: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TrackState<'a> {
|
||||||
|
list: SelectionList<'a>,
|
||||||
|
info: Paragraph<'a>,
|
||||||
|
active: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AppState<'a> {
|
||||||
|
artists: ArtistState<'a>,
|
||||||
|
albums: AlbumState<'a>,
|
||||||
|
tracks: TrackState<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Ui {}
|
||||||
|
|
||||||
|
impl Ui {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Ui {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn construct_areas(frame: Rect) -> FrameAreas {
|
||||||
|
let width_one_third = frame.width / 3;
|
||||||
|
let height_one_third = frame.height / 3;
|
||||||
|
|
||||||
|
let panel_width = width_one_third;
|
||||||
|
let panel_width_last = frame.width - 2 * panel_width;
|
||||||
|
let panel_height_top = frame.height - height_one_third;
|
||||||
|
let panel_height_bottom = height_one_third;
|
||||||
|
|
||||||
|
let artist_list = Rect {
|
||||||
|
x: frame.x,
|
||||||
|
y: frame.y,
|
||||||
|
width: panel_width,
|
||||||
|
height: frame.height,
|
||||||
|
};
|
||||||
|
|
||||||
|
let album_list = Rect {
|
||||||
|
x: artist_list.x + artist_list.width,
|
||||||
|
y: frame.y,
|
||||||
|
width: panel_width,
|
||||||
|
height: panel_height_top,
|
||||||
|
};
|
||||||
|
|
||||||
|
let album_info = Rect {
|
||||||
|
x: album_list.x,
|
||||||
|
y: album_list.y + album_list.height,
|
||||||
|
width: album_list.width,
|
||||||
|
height: panel_height_bottom,
|
||||||
|
};
|
||||||
|
|
||||||
|
let track_list = Rect {
|
||||||
|
x: album_list.x + album_list.width,
|
||||||
|
y: frame.y,
|
||||||
|
width: panel_width_last,
|
||||||
|
height: panel_height_top,
|
||||||
|
};
|
||||||
|
|
||||||
|
let track_info = Rect {
|
||||||
|
x: track_list.x,
|
||||||
|
y: track_list.y + track_list.height,
|
||||||
|
width: track_list.width,
|
||||||
|
height: panel_height_bottom,
|
||||||
|
};
|
||||||
|
|
||||||
|
FrameAreas {
|
||||||
|
artists: ArtistArea { list: artist_list },
|
||||||
|
albums: AlbumArea {
|
||||||
|
list: album_list,
|
||||||
|
info: album_info,
|
||||||
|
},
|
||||||
|
tracks: TrackArea {
|
||||||
|
list: track_list,
|
||||||
|
info: track_info,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn construct_artist_list(app: &App) -> ArtistState {
|
||||||
|
let artists = app.get_artist_ids();
|
||||||
|
let list = List::new(
|
||||||
|
artists
|
||||||
|
.iter()
|
||||||
|
.map(|id| ListItem::new(id.name.as_str()))
|
||||||
|
.collect::<Vec<ListItem>>(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let selected_artist = app.selected_artist();
|
||||||
|
|
||||||
|
let mut state = ListState::default();
|
||||||
|
state.select(selected_artist);
|
||||||
|
|
||||||
|
let active = app.get_active_category() == Category::Artist;
|
||||||
|
|
||||||
|
ArtistState {
|
||||||
|
list: SelectionList { list, state },
|
||||||
|
active,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn construct_album_list(app: &App) -> AlbumState {
|
||||||
|
let albums = app.get_album_ids();
|
||||||
|
let list = List::new(
|
||||||
|
albums
|
||||||
|
.iter()
|
||||||
|
.map(|id| ListItem::new(id.title.as_str()))
|
||||||
|
.collect::<Vec<ListItem>>(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let selected_album = app.selected_album();
|
||||||
|
|
||||||
|
let mut state = ListState::default();
|
||||||
|
state.select(selected_album);
|
||||||
|
|
||||||
|
let active = app.get_active_category() == Category::Album;
|
||||||
|
|
||||||
|
let album = selected_album.map(|i| albums[i]);
|
||||||
|
let info = Paragraph::new(format!(
|
||||||
|
"Title: {}\n\
|
||||||
|
Year: {}",
|
||||||
|
album.map(|a| a.title.as_str()).unwrap_or(""),
|
||||||
|
album
|
||||||
|
.map(|a| a.year.to_string())
|
||||||
|
.unwrap_or_else(|| "".to_string()),
|
||||||
|
));
|
||||||
|
|
||||||
|
AlbumState {
|
||||||
|
list: SelectionList { list, state },
|
||||||
|
info,
|
||||||
|
active,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn construct_track_list(app: &App) -> TrackState {
|
||||||
|
let tracks = app.get_track_ids();
|
||||||
|
let list = List::new(
|
||||||
|
tracks
|
||||||
|
.iter()
|
||||||
|
.map(|id| ListItem::new(id.title.as_str()))
|
||||||
|
.collect::<Vec<ListItem>>(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let selected_track = app.selected_track();
|
||||||
|
|
||||||
|
let mut state = ListState::default();
|
||||||
|
state.select(selected_track);
|
||||||
|
|
||||||
|
let active = app.get_active_category() == Category::Track;
|
||||||
|
|
||||||
|
let track = selected_track.map(|i| tracks[i]);
|
||||||
|
let info = Paragraph::new(format!(
|
||||||
|
"Track: {}\n\
|
||||||
|
Title: {}\n\
|
||||||
|
Artist: {}\n\
|
||||||
|
Format: {}",
|
||||||
|
track
|
||||||
|
.map(|t| t.number.to_string())
|
||||||
|
.unwrap_or_else(|| "".to_string()),
|
||||||
|
track.map(|t| t.title.as_str()).unwrap_or(""),
|
||||||
|
track
|
||||||
|
.map(|t| t.artist.join("; "))
|
||||||
|
.unwrap_or_else(|| "".to_string()),
|
||||||
|
track
|
||||||
|
.map(|t| match t.format {
|
||||||
|
TrackFormat::Flac => "FLAC",
|
||||||
|
TrackFormat::Mp3 => "MP3",
|
||||||
|
})
|
||||||
|
.unwrap_or(""),
|
||||||
|
));
|
||||||
|
|
||||||
|
TrackState {
|
||||||
|
list: SelectionList { list, state },
|
||||||
|
info,
|
||||||
|
active,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn construct_app_state(app: &App) -> AppState {
|
||||||
|
AppState {
|
||||||
|
artists: Self::construct_artist_list(app),
|
||||||
|
albums: Self::construct_album_list(app),
|
||||||
|
tracks: Self::construct_track_list(app),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn style(_active: bool) -> Style {
|
||||||
|
Style::default().fg(Color::White).bg(Color::Black)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn block_style(active: bool) -> Style {
|
||||||
|
Self::style(active)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn highlight_style(active: bool) -> Style {
|
||||||
|
if active {
|
||||||
|
Style::default().fg(Color::White).bg(Color::DarkGray)
|
||||||
|
} else {
|
||||||
|
Self::style(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn block<'a>(title: &str, active: bool) -> Block<'a> {
|
||||||
|
Block::default()
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.style(Self::block_style(active))
|
||||||
|
.title(format!(" {title} "))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_list_widget<B: Backend>(
|
||||||
|
title: &str,
|
||||||
|
mut list: SelectionList,
|
||||||
|
active: bool,
|
||||||
|
area: Rect,
|
||||||
|
frame: &mut Frame<'_, B>,
|
||||||
|
) {
|
||||||
|
frame.render_stateful_widget(
|
||||||
|
list.list
|
||||||
|
.highlight_style(Self::highlight_style(active))
|
||||||
|
.highlight_symbol(">> ")
|
||||||
|
.style(Self::style(active))
|
||||||
|
.block(Self::block(title, active)),
|
||||||
|
area,
|
||||||
|
&mut list.state,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_info_widget<B: Backend>(
|
||||||
|
title: &str,
|
||||||
|
paragraph: Paragraph,
|
||||||
|
active: bool,
|
||||||
|
area: Rect,
|
||||||
|
frame: &mut Frame<'_, B>,
|
||||||
|
) {
|
||||||
|
frame.render_widget(
|
||||||
|
paragraph
|
||||||
|
.style(Self::style(active))
|
||||||
|
.block(Self::block(title, active)),
|
||||||
|
area,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_artist_column<B: Backend>(
|
||||||
|
state: ArtistState,
|
||||||
|
area: ArtistArea,
|
||||||
|
frame: &mut Frame<'_, B>,
|
||||||
|
) {
|
||||||
|
Self::render_list_widget("Artists", state.list, state.active, area.list, frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_album_column<B: Backend>(
|
||||||
|
state: AlbumState,
|
||||||
|
area: AlbumArea,
|
||||||
|
frame: &mut Frame<'_, B>,
|
||||||
|
) {
|
||||||
|
Self::render_list_widget("Albums", state.list, state.active, area.list, frame);
|
||||||
|
Self::render_info_widget("Album info", state.info, state.active, area.info, frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_track_column<B: Backend>(
|
||||||
|
state: TrackState,
|
||||||
|
area: TrackArea,
|
||||||
|
frame: &mut Frame<'_, B>,
|
||||||
|
) {
|
||||||
|
Self::render_list_widget("Tracks", state.list, state.active, area.list, frame);
|
||||||
|
Self::render_info_widget("Track info", state.info, state.active, area.info, frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render<B: Backend>(&self, app: &App, frame: &mut Frame<'_, B>) {
|
||||||
|
let areas = Self::construct_areas(frame.size());
|
||||||
|
let app_state = Self::construct_app_state(app);
|
||||||
|
|
||||||
|
Self::render_artist_column(app_state.artists, areas.artists, frame);
|
||||||
|
Self::render_album_column(app_state.albums, areas.albums, frame);
|
||||||
|
Self::render_track_column(app_state.tracks, areas.tracks, frame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
// This is UI so the only sensible unit test is to run the code through various app states.
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
tests::COLLECTION,
|
||||||
|
tui::tests::{app, terminal},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::Ui;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty() {
|
||||||
|
let mut terminal = terminal();
|
||||||
|
let app = app(vec![]);
|
||||||
|
let ui = Ui::new();
|
||||||
|
|
||||||
|
terminal.draw(|frame| ui.render(&app, frame)).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn collection() {
|
||||||
|
let mut terminal = terminal();
|
||||||
|
let mut app = app(COLLECTION.to_owned());
|
||||||
|
let ui = Ui::new();
|
||||||
|
|
||||||
|
terminal.draw(|frame| ui.render(&app, frame)).unwrap();
|
||||||
|
|
||||||
|
// Change the track (which has a different track format).
|
||||||
|
app.increment_category();
|
||||||
|
app.increment_category();
|
||||||
|
app.increment_selection();
|
||||||
|
|
||||||
|
terminal.draw(|frame| ui.render(&app, frame)).unwrap();
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ use std::{fs, path::PathBuf};
|
|||||||
use musichoard::{
|
use musichoard::{
|
||||||
database::{
|
database::{
|
||||||
json::{JsonDatabase, JsonDatabaseFileBackend},
|
json::{JsonDatabase, JsonDatabaseFileBackend},
|
||||||
DatabaseRead, DatabaseWrite,
|
Database,
|
||||||
},
|
},
|
||||||
Artist,
|
Artist,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user