26 Commits
v1.2.0 ... main

Author SHA1 Message Date
5aedb0d14a chore: bump version (v1.3.2) 2026-02-15 00:18:59 +01:00
4b9cc3acd3 chore: update to 2024 rust edition 2026-02-15 00:16:19 +01:00
d746fbb152 chore: update deps 2026-02-14 23:49:53 +01:00
6f762742bd chore: bump version (v1.3.1) 2024-07-25 22:44:40 +02:00
1c8ff6d2e9 docs: fix required gstreamer version in readme 2024-07-25 22:42:07 +02:00
ec9188fbaf chore: use new edition 2024-07-25 22:41:27 +02:00
5c11600c64 chore: remove nix files due to lack of maintenance 2024-07-25 22:31:46 +02:00
05ad75e20d chore: update deps 2024-07-25 22:27:33 +02:00
a10345fbbe build: bump version (v1.3.0) 2022-04-07 21:12:25 +02:00
f3423eea53 fix: fix "from", "to" & "config" cli argument processing 2022-04-07 21:10:37 +02:00
80e3d02cb4 refactor: simplify config processing 2022-04-07 20:58:05 +02:00
b7a7abbf61 refactor: fmt 2022-04-07 19:36:49 +02:00
5abadc3131 build: update deps 2022-04-07 19:35:37 +02:00
9c86cdcc62 build: update deps 2022-02-24 21:45:52 +01:00
605fa5c15b build: update deps 2022-02-04 23:29:19 +01:00
9c1d39ba5f fix: allow multiple values for tag "musicbrainz-artistid" & "musicbrainz-albumartistid"
issue #2
2021-12-14 19:08:05 +01:00
c6c9da2f27 build: update deps 2021-12-14 18:49:01 +01:00
6872e7897b build: bump version (v1.2.2) 2021-08-25 17:11:23 +02:00
2d1497cb36 build: update deps 2021-08-25 17:09:33 +02:00
f1fb3506b5 build: update deps 2021-07-25 14:37:08 +02:00
f4050fe645 refactor: switch from space to tab indentation 2021-07-10 10:07:30 +02:00
b533f059d7 build: bump version (v1.2.1) 2021-07-05 20:02:49 +02:00
00a25e168d refactor: use more readable clamp method 2021-07-05 19:53:49 +02:00
b51c9939c1 build: upgrade deps 2021-07-05 19:46:06 +02:00
c22d45818e build: update deps 2021-06-18 11:46:48 +02:00
18cc852e6b build: update deps 2021-05-27 22:52:48 +02:00
12 changed files with 1672 additions and 1498 deletions

View File

@@ -1,7 +1,7 @@
root = true
[*]
indent_style = space
indent_style = tab
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true

1
.rustfmt.toml Normal file
View File

@@ -0,0 +1 @@
hard_tabs = true

View File

@@ -1,5 +1,26 @@
# Changelog
## v1.3.2
* dependencies upgraded
## v1.3.1
* dependencies upgraded
## v1.3.0
* allow multiple values for the tags "musicbrainz-artistid" and "musicbrainz-albumartistid"
* fix "from", "to" & "config" cli argument processing
## v1.2.2
* dependencies upgraded
## v1.2.1
* dependencies upgraded
## v1.2.0
* "copy" encoding format added

987
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
[package]
name = "audio-conv"
version = "1.2.0"
edition = "2018"
version = "1.3.2"
edition = "2024"
description = "Copies directory structure and converts audio files in it"
authors = ["Thomas Heck <t@b128.net>"]
repository = "https://gitlab.com/chpio/audio-conv"
@@ -21,22 +21,22 @@ include = [
]
[dependencies]
gstreamer-audio = { version = "0.16", features = ["v1_10"] }
gstreamer = { version = "0.16", features = ["v1_10"] }
gstreamer-base = { version = "0.16", features = ["v1_10"] }
glib = "0.10"
gstreamer = { version = "0.24", features = ["v1_16"] }
gstreamer-base = { version = "0.24", features = ["v1_16"] }
gstreamer-audio = { version = "0.24", features = ["v1_16"] }
glib = "0.21"
futures = "0.3"
num_cpus = "1"
walkdir = "2"
libc = "0.2"
anyhow = "1"
clap = "2"
clap = { version = "4", features = ["cargo"] }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
serde_yaml = "0.9"
regex = "1"
globset = "0.4"
derive_more = "0.99"
tui = { version = "0.14", default-features = false, features = ["crossterm"] }
derive_more = { version = "2", features = ["full"] }
tui = { version = "0.19", default-features = false, features = ["crossterm"] }
[dependencies.tokio]
version = "1"

View File

@@ -5,7 +5,7 @@ second path. The directory structure from the first path gets also copied to the
## Dependencies
Requires *gstreamer* version 1.10 or higher with the *base* plugin.
Requires *gstreamer* version 1.16 or higher with the *base* plugin.
The supported source audio formats (or even other media that is able to contain audio) depend on
the installed *gstreamer* plugins.

58
flake.lock generated
View File

@@ -1,58 +0,0 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1618868421,
"narHash": "sha256-vyoJhLV6cJ8/tWz+l9HZLIkb9Rd9esE7p+0RL6zDR6Y=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "eed214942bcfb3a8cc09eb3b28ca7d7221e44a94",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"import-cargo": {
"locked": {
"lastModified": 1594305518,
"narHash": "sha256-frtArgN42rSaEcEOYWg8sVPMUK+Zgch3c+wejcpX3DY=",
"owner": "edolstra",
"repo": "import-cargo",
"rev": "25d40be4a73d40a2572e0cc233b83253554f06c5",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "import-cargo",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1619118726,
"narHash": "sha256-eEB4bIcl/REE5c9rMl4k3FmI9clTfjoFky5dm73gthY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "14c8ae6efbf38dd4fd5fedaeeea2641c9c0e1e97",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"import-cargo": "import-cargo",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

View File

@@ -1,63 +0,0 @@
{
description = "Converts audio files";
inputs = {
nixpkgs.url = github:NixOS/nixpkgs;
flake-utils.url = "github:numtide/flake-utils";
import-cargo.url = github:edolstra/import-cargo;
};
outputs = { self, flake-utils, nixpkgs, import-cargo }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
buildtimeDeps = with pkgs; [
cargo
rustc
pkg-config
];
runtimeDeps = with pkgs; [
gst_all_1.gstreamer
# needed for opus, resample, ...
gst_all_1.gst-plugins-base
# needed for flac
gst_all_1.gst-plugins-good
];
inherit (import-cargo.builders) importCargo;
in {
defaultPackage = pkgs.stdenv.mkDerivation {
name = "audio-conv";
src = self;
nativeBuildInputs = [
# setupHook which makes sure that a CARGO_HOME with vendored dependencies
# exists
(importCargo { lockFile = ./Cargo.lock; inherit pkgs; }).cargoHome
]
++ buildtimeDeps;
buildInputs = runtimeDeps;
buildPhase = ''
cargo build --release --offline
'';
installPhase = ''
install -Dm775 ./target/release/audio-conv $out/bin/audio-conv
'';
};
devShell = pkgs.stdenv.mkDerivation {
name = "audio-conv";
buildInputs = [ pkgs.rustfmt pkgs.rust-analyzer ]
++ buildtimeDeps
++ runtimeDeps;
};
}
);
}

View File

@@ -1,320 +1,322 @@
use anyhow::{Context, Error, Result};
use clap::{ArgAction, builder::ValueParser};
use globset::GlobBuilder;
use regex::bytes::{Regex, RegexBuilder};
use serde::Deserialize;
use std::{
io::Write,
path::{Path, PathBuf},
io::Write,
path::{Path, PathBuf},
};
#[derive(Debug)]
pub struct Config {
pub from: PathBuf,
pub to: PathBuf,
pub matches: Vec<TranscodeMatch>,
pub jobs: Option<usize>,
pub from: PathBuf,
pub to: PathBuf,
pub matches: Vec<TranscodeMatch>,
pub jobs: Option<usize>,
}
#[derive(Debug)]
pub struct TranscodeMatch {
pub regexes: Vec<Regex>,
pub to: Transcode,
pub regexes: Vec<Regex>,
pub to: Transcode,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "codec")]
pub enum Transcode {
#[serde(rename = "opus")]
Opus {
#[serde(default = "default_opus_bitrate")]
bitrate: u16,
#[serde(rename = "opus")]
Opus {
#[serde(default = "default_opus_bitrate")]
bitrate: u16,
#[serde(default = "bitrate_type_vbr")]
bitrate_type: BitrateType,
},
#[serde(default = "bitrate_type_vbr")]
bitrate_type: BitrateType,
},
#[serde(rename = "flac")]
Flac {
#[serde(default = "default_flac_compression")]
compression: u8,
},
#[serde(rename = "flac")]
Flac {
#[serde(default = "default_flac_compression")]
compression: u8,
},
#[serde(rename = "mp3")]
Mp3 {
#[serde(default = "default_mp3_bitrate")]
bitrate: u16,
#[serde(rename = "mp3")]
Mp3 {
#[serde(default = "default_mp3_bitrate")]
bitrate: u16,
#[serde(default = "bitrate_type_vbr")]
bitrate_type: BitrateType,
},
#[serde(default = "bitrate_type_vbr")]
bitrate_type: BitrateType,
},
#[serde(rename = "copy")]
Copy,
#[serde(rename = "copy")]
Copy,
}
impl Transcode {
pub fn extension(&self) -> &'static str {
match self {
Transcode::Opus { .. } => "opus",
Transcode::Flac { .. } => "flac",
Transcode::Mp3 { .. } => "mp3",
Transcode::Copy => "",
}
}
pub fn extension(&self) -> &'static str {
match self {
Transcode::Opus { .. } => "opus",
Transcode::Flac { .. } => "flac",
Transcode::Mp3 { .. } => "mp3",
Transcode::Copy => "",
}
}
}
fn default_opus_bitrate() -> u16 {
160
160
}
fn default_flac_compression() -> u8 {
5
5
}
fn bitrate_type_vbr() -> BitrateType {
BitrateType::Vbr
BitrateType::Vbr
}
fn default_mp3_bitrate() -> u16 {
256
256
}
impl Default for Transcode {
fn default() -> Self {
Transcode::Opus {
bitrate: default_opus_bitrate(),
bitrate_type: bitrate_type_vbr(),
}
}
fn default() -> Self {
Transcode::Opus {
bitrate: default_opus_bitrate(),
bitrate_type: bitrate_type_vbr(),
}
}
}
#[derive(Clone, Debug, Deserialize)]
pub enum BitrateType {
#[serde(rename = "cbr")]
Cbr,
#[serde(rename = "vbr")]
Vbr,
#[serde(rename = "cbr")]
Cbr,
#[serde(rename = "vbr")]
Vbr,
}
#[derive(Debug, Default, Deserialize)]
struct ConfigFile {
from: Option<PathBuf>,
to: Option<PathBuf>,
from: Option<PathBuf>,
to: Option<PathBuf>,
#[serde(default)]
matches: Vec<TranscodeMatchFile>,
#[serde(default)]
matches: Vec<TranscodeMatchFile>,
jobs: Option<usize>,
}
#[derive(Debug, Deserialize)]
struct TranscodeMatchFile {
glob: Option<String>,
regex: Option<String>,
glob: Option<String>,
regex: Option<String>,
#[serde(default)]
extensions: Vec<String>,
#[serde(default)]
extensions: Vec<String>,
to: Transcode,
to: Transcode,
}
pub fn config() -> Result<Config> {
use clap::{App, Arg, SubCommand};
use clap::{Arg, Command};
let arg_matches = App::new("audio-conv")
.version(clap::crate_version!())
.about("Converts audio files")
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.required(false)
.takes_value(true)
.help("Path to an audio-conv config file, defaults to \"audio-conv.yaml\""),
)
.arg(
Arg::with_name("from")
.short("f")
.long("from")
.required(false)
.takes_value(true)
.help("\"from\" directory path"),
)
.arg(
Arg::with_name("to")
.short("t")
.long("to")
.required(false)
.takes_value(true)
.help("\"to\" directory path"),
)
.arg(
Arg::with_name("jobs")
.short("j")
.long("jobs")
.required(false)
.takes_value(true)
.help("Allow N jobs/transcodes at once. Defaults to number of logical cores"),
)
.subcommand(SubCommand::with_name("init").about("writes an example config"))
.get_matches();
let arg_matches = Command::new("audio-conv")
.version(clap::crate_version!())
.about("Converts audio files")
.arg(
Arg::new("config")
.short('c')
.long("config")
.required(false)
.value_parser(ValueParser::path_buf())
.action(ArgAction::Set)
.help("Path to an audio-conv config file, defaults to \"audio-conv.yaml\""),
)
.arg(
Arg::new("from")
.short('f')
.long("from")
.required(false)
.value_parser(ValueParser::path_buf())
.action(ArgAction::Set)
.help("\"from\" directory path"),
)
.arg(
Arg::new("to")
.short('t')
.long("to")
.required(false)
.value_parser(ValueParser::path_buf())
.action(ArgAction::Set)
.help("\"to\" directory path"),
)
.arg(
Arg::new("jobs")
.short('j')
.long("jobs")
.required(false)
.value_parser(clap::value_parser!(usize))
.action(ArgAction::Set)
.help("Allow N jobs/transcodes at once. Defaults to number of logical cores"),
)
.subcommand(Command::new("init").about("writes an example config"))
.get_matches();
let current_dir = std::env::current_dir().context("Could not get current directory")?;
let current_dir = std::env::current_dir().context("Could not get current directory")?;
let config_path = arg_matches.value_of_os("config");
let force_load = config_path.is_some();
let config_path = config_path
.map(AsRef::<Path>::as_ref)
.unwrap_or_else(|| AsRef::<Path>::as_ref("audio-conv.yaml"));
let config_path = current_dir.join(config_path);
let config_path = arg_matches.get_one::<PathBuf>("config");
let enforce_config_load = config_path.is_some();
let config_path = config_path
.map(AsRef::<Path>::as_ref)
.unwrap_or_else(|| AsRef::<Path>::as_ref("audio-conv.yaml"));
let config_path = current_dir.join(config_path);
if let Some("init") = arg_matches.subcommand_name() {
std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&config_path)
.and_then(|mut f| f.write_all(std::include_bytes!("../example.audio-conv.yaml")))
.with_context(|| format!("Unable to write config file to {}", config_path.display()))?;
if let Some("init") = arg_matches.subcommand_name() {
std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&config_path)
.and_then(|mut f| f.write_all(std::include_bytes!("../example.audio-conv.yaml")))
.with_context(|| format!("Unable to write config file to {}", config_path.display()))?;
std::process::exit(0);
}
std::process::exit(0);
}
let config_dir = config_path
.parent()
.context("Could not get parent directory of the config file")?;
let config_dir = config_path
.parent()
.context("Could not get parent directory of the config file")?;
let config_file = load_config_file(&config_path)
.with_context(|| format!("Failed loading config file {}", config_path.display()))?;
let config_file = load_config_file(&config_path)
.with_context(|| format!("Failed loading config file {}", config_path.display()))?;
if force_load && config_file.is_none() {
return Err(Error::msg(format!(
"could not find config file \"{}\"",
config_path.display()
)));
}
if enforce_config_load && config_file.is_none() {
return Err(Error::msg(format!(
"could not find config file \"{}\"",
config_path.display()
)));
}
let default_regex = RegexBuilder::new("\\.(flac|wav)$")
.case_insensitive(true)
.build()
.expect("Failed compiling default match regex");
let default_regex = RegexBuilder::new("\\.(flac|wav)$")
.case_insensitive(true)
.build()
.expect("Failed compiling default match regex");
let transcode_matches = config_file
.as_ref()
.map(|config_file| {
config_file
.matches
.iter()
.map(|m| {
let glob = m.glob.iter().map(|glob| {
let glob = GlobBuilder::new(glob)
.case_insensitive(true)
.build()
.context("Failed building glob")?;
let regex = Regex::new(glob.regex()).context("Failed compiling regex")?;
Ok(regex)
});
let transcode_matches = config_file
.as_ref()
.map(|config_file| {
config_file
.matches
.iter()
.map(|m| {
let glob = m.glob.iter().map(|glob| {
let glob = GlobBuilder::new(glob)
.case_insensitive(true)
.build()
.context("Failed building glob")?;
let regex = Regex::new(glob.regex()).context("Failed compiling regex")?;
Ok(regex)
});
let regex = m.regex.iter().map(|regex| {
let regex = RegexBuilder::new(regex)
.case_insensitive(true)
.build()
.context("Failed compiling regex")?;
Ok(regex)
});
let regex = m.regex.iter().map(|regex| {
let regex = RegexBuilder::new(regex)
.case_insensitive(true)
.build()
.context("Failed compiling regex")?;
Ok(regex)
});
let extensions = m.extensions.iter().map(|ext| {
let mut ext = regex::escape(ext);
ext.insert_str(0, &"\\.");
ext.push_str("$");
let extensions = m.extensions.iter().map(|ext| {
let mut ext = regex::escape(ext);
ext.insert_str(0, &"\\.");
ext.push_str("$");
let regex = RegexBuilder::new(&ext)
.case_insensitive(true)
.build()
.context("Failed compiling regex")?;
Ok(regex)
});
let regex = RegexBuilder::new(&ext)
.case_insensitive(true)
.build()
.context("Failed compiling regex")?;
Ok(regex)
});
let mut regexes = glob
.chain(regex)
.chain(extensions)
.collect::<Result<Vec<_>>>()?;
let mut regexes = glob
.chain(regex)
.chain(extensions)
.collect::<Result<Vec<_>>>()?;
if regexes.is_empty() {
regexes.push(default_regex.clone());
}
if regexes.is_empty() {
regexes.push(default_regex.clone());
}
Ok(TranscodeMatch {
regexes,
to: m.to.clone(),
})
})
.collect::<Result<Vec<_>>>()
})
.transpose()?
.filter(|matches| !matches.is_empty())
.unwrap_or_else(|| {
vec![TranscodeMatch {
regexes: vec![default_regex],
to: Transcode::default(),
}]
});
Ok(TranscodeMatch {
regexes,
to: m.to.clone(),
})
})
.collect::<Result<Vec<_>>>()
})
.transpose()?
.filter(|matches| !matches.is_empty())
.unwrap_or_else(|| {
vec![TranscodeMatch {
regexes: vec![default_regex],
to: Transcode::default(),
}]
});
Ok(Config {
from: {
arg_matches
.value_of_os("from")
.map(|p| current_dir.join(p))
.or_else(|| {
config_file
.as_ref()
.map(|c| c.from.as_ref())
.flatten()
.map(|p| config_dir.join(p))
})
.ok_or_else(|| Error::msg("\"from\" not configured"))?
.canonicalize()
.context("Could not canonicalize \"from\" path")?
},
to: arg_matches
.value_of_os("to")
.map(|p| current_dir.join(p))
.or_else(|| {
config_file
.as_ref()
.map(|c| c.to.as_ref())
.flatten()
.map(|p| config_dir.join(p))
})
.ok_or_else(|| Error::msg("\"to\" not configured"))?
.canonicalize()
.context("Could not canonicalize \"to\" path")?,
matches: transcode_matches,
jobs: arg_matches
.value_of_os("jobs")
.map(|jobs_os_str| {
let jobs_str = jobs_os_str.to_str().with_context(|| {
// TODO: use `OsStr.display` when it lands
// https://github.com/rust-lang/rust/pull/80841
format!(
"Could not convert \"jobs\" argument to string due to invalid characters",
)
})?;
jobs_str.parse().with_context(|| {
format!(
"Could not parse \"jobs\" argument \"{}\" to a number",
&jobs_str
)
})
})
.transpose()?,
})
Ok(Config {
from: {
arg_matches
.get_one::<PathBuf>("from")
.map(|p| current_dir.join(p))
.or_else(|| {
config_file
.as_ref()
.map(|c| c.from.as_ref())
.flatten()
.map(|p| config_dir.join(p))
})
.ok_or_else(|| Error::msg("\"from\" not configured"))?
.canonicalize()
.context("Could not canonicalize \"from\" path")?
},
to: arg_matches
.get_one::<PathBuf>("to")
.map(|p| current_dir.join(p))
.or_else(|| {
config_file
.as_ref()
.map(|c| c.to.as_ref())
.flatten()
.map(|p| config_dir.join(p))
})
.ok_or_else(|| Error::msg("\"to\" not configured"))?
.canonicalize()
.context("Could not canonicalize \"to\" path")?,
matches: transcode_matches,
jobs: arg_matches
.get_one("jobs")
.copied()
.or_else(|| config_file.as_ref().map(|c| c.jobs).flatten()),
// .map(|jobs_str| {
// jobs_str.parse().with_context(|| {
// format!(
// "Could not parse \"jobs\" argument \"{}\" to a number",
// &jobs_str
// )
// })
// })
// .transpose()?,
})
}
fn load_config_file(path: &Path) -> Result<Option<ConfigFile>> {
let mut file = match std::fs::File::open(path) {
Ok(file) => file,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(Error::new(err)),
};
let config: ConfigFile =
serde_yaml::from_reader(&mut file).context("Could not parse config file")?;
Ok(Some(config))
let mut file = match std::fs::File::open(path) {
Ok(file) => file,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(Error::new(err)),
};
let config: ConfigFile =
serde_yaml::from_reader(&mut file).context("Could not parse config file")?;
Ok(Some(config))
}

File diff suppressed because it is too large Load Diff

39
src/tag.rs Normal file
View File

@@ -0,0 +1,39 @@
use glib::{GStr, Value, gstr};
use gstreamer::{
Tag, TagFlag,
tags::{CustomTag, merge_strings_with_comma},
};
pub struct MbArtistId;
impl<'a> Tag<'a> for MbArtistId {
type TagType = &'a str;
const TAG_NAME: &'static GStr = gstr!("musicbrainz-artistid");
}
impl CustomTag<'_> for MbArtistId {
const FLAG: TagFlag = TagFlag::Meta;
const NICK: &'static GStr = gstr!("artist ID");
const DESCRIPTION: &'static GStr = gstr!("MusicBrainz artist ID");
fn merge_func(src: &Value) -> Value {
merge_strings_with_comma(src)
}
}
pub struct MbAlbumArtistId;
impl<'a> Tag<'a> for MbAlbumArtistId {
type TagType = &'a str;
const TAG_NAME: &'static GStr = gstr!("musicbrainz-albumartistid");
}
impl CustomTag<'_> for MbAlbumArtistId {
const FLAG: TagFlag = TagFlag::Meta;
const NICK: &'static GStr = gstr!("album artist ID");
const DESCRIPTION: &'static GStr = gstr!("MusicBrainz album artist ID");
fn merge_func(src: &Value) -> Value {
merge_strings_with_comma(src)
}
}

442
src/ui.rs
View File

@@ -2,282 +2,282 @@ use crate::ConversionArgs;
use anyhow::{Context, Result};
use futures::Future;
use std::{
borrow::Cow, cell::RefCell, collections::HashMap, io, mem, path::PathBuf, rc::Rc,
time::Duration,
borrow::Cow, cell::RefCell, collections::HashMap, io, mem, path::PathBuf, rc::Rc,
time::Duration,
};
use tokio::{task, time::interval};
use tui::{backend::CrosstermBackend, Terminal};
use tui::{Terminal, backend::CrosstermBackend};
pub const UPDATE_INTERVAL_MILLIS: u64 = 100;
#[derive(Debug)]
pub enum Msg {
Init { task_len: usize, log_path: PathBuf },
Exit,
TaskStart { id: usize, args: ConversionArgs },
TaskEnd { id: usize },
TaskProgress { id: usize, ratio: f64 },
TaskError { id: usize },
Init { task_len: usize, log_path: PathBuf },
Exit,
TaskStart { id: usize, args: ConversionArgs },
TaskEnd { id: usize },
TaskProgress { id: usize, ratio: f64 },
TaskError { id: usize },
}
#[derive(Debug, Clone)]
pub struct MsgQueue {
inner: Rc<RefCell<Vec<Msg>>>,
inner: Rc<RefCell<Vec<Msg>>>,
}
impl MsgQueue {
fn new() -> MsgQueue {
MsgQueue {
inner: Rc::new(RefCell::new(Vec::new())),
}
}
fn new() -> MsgQueue {
MsgQueue {
inner: Rc::new(RefCell::new(Vec::new())),
}
}
pub fn push(&self, msg: Msg) {
self.inner.borrow_mut().push(msg);
}
pub fn push(&self, msg: Msg) {
self.inner.borrow_mut().push(msg);
}
fn swap_inner(&self, other: &mut Vec<Msg>) {
let mut inner = self.inner.borrow_mut();
mem::swap(&mut *inner, other)
}
fn swap_inner(&self, other: &mut Vec<Msg>) {
let mut inner = self.inner.borrow_mut();
mem::swap(&mut *inner, other)
}
}
struct State {
terminal: Terminal<CrosstermBackend<io::Stdout>>,
log_path: Option<PathBuf>,
task_len: Option<usize>,
ended_tasks: usize,
running_tasks: HashMap<usize, Task>,
has_rendered: bool,
has_errored: bool,
terminal: Terminal<CrosstermBackend<io::Stdout>>,
log_path: Option<PathBuf>,
task_len: Option<usize>,
ended_tasks: usize,
running_tasks: HashMap<usize, Task>,
has_rendered: bool,
has_errored: bool,
}
impl State {
fn new() -> Result<State> {
let terminal = Terminal::new(CrosstermBackend::new(io::stdout()))
.context("Unable to create ui terminal")?;
fn new() -> Result<State> {
let terminal = Terminal::new(CrosstermBackend::new(io::stdout()))
.context("Unable to create ui terminal")?;
Ok(State {
terminal,
log_path: None,
task_len: None,
ended_tasks: 0,
running_tasks: HashMap::new(),
has_rendered: false,
has_errored: false,
})
}
Ok(State {
terminal,
log_path: None,
task_len: None,
ended_tasks: 0,
running_tasks: HashMap::new(),
has_rendered: false,
has_errored: false,
})
}
fn process_msg(&mut self, msg: Msg) -> Result<bool> {
match msg {
Msg::Init { task_len, log_path } => {
self.task_len = Some(task_len);
self.log_path = Some(log_path);
}
Msg::Exit => return Ok(false),
Msg::TaskStart { id, args } => {
self.running_tasks.insert(
id,
Task {
id,
ratio: None,
args,
},
);
}
Msg::TaskEnd { id } => {
self.running_tasks
.remove(&id)
.context("Unable to remove finished task; could't find task")?;
self.ended_tasks += 1;
}
Msg::TaskProgress { id, ratio } => {
let mut task = self
.running_tasks
.get_mut(&id)
.context("Unable to update task progress; could't find task")?;
task.ratio = Some(ratio);
}
Msg::TaskError { id } => {
// TODO
self.running_tasks
.remove(&id)
.context("Unable to remove errored task; could't find task")?;
self.ended_tasks += 1;
self.has_errored = true;
}
}
fn process_msg(&mut self, msg: Msg) -> Result<bool> {
match msg {
Msg::Init { task_len, log_path } => {
self.task_len = Some(task_len);
self.log_path = Some(log_path);
}
Msg::Exit => return Ok(false),
Msg::TaskStart { id, args } => {
self.running_tasks.insert(
id,
Task {
id,
ratio: None,
args,
},
);
}
Msg::TaskEnd { id } => {
self.running_tasks
.remove(&id)
.context("Unable to remove finished task; could't find task")?;
self.ended_tasks += 1;
}
Msg::TaskProgress { id, ratio } => {
let task = self
.running_tasks
.get_mut(&id)
.context("Unable to update task progress; could't find task")?;
task.ratio = Some(ratio);
}
Msg::TaskError { id } => {
// TODO
self.running_tasks
.remove(&id)
.context("Unable to remove errored task; could't find task")?;
self.ended_tasks += 1;
self.has_errored = true;
}
}
Ok(true)
}
Ok(true)
}
fn render(&mut self) -> Result<()> {
use tui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::Text,
widgets::{Block, Borders, Gauge, Paragraph},
};
fn render(&mut self) -> Result<()> {
use tui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::Text,
widgets::{Block, Borders, Gauge, Paragraph},
};
let task_len = if let Some(task_len) = self.task_len {
task_len
} else {
return Ok(());
};
let task_len = if let Some(task_len) = self.task_len {
task_len
} else {
return Ok(());
};
if task_len == 0 {
return Ok(());
}
if task_len == 0 {
return Ok(());
}
let tasks_ended = self.ended_tasks;
let tasks_ended = self.ended_tasks;
let mut running_tasks: Vec<_> = self.running_tasks.values().cloned().collect();
let mut running_tasks: Vec<_> = self.running_tasks.values().cloned().collect();
running_tasks.sort_by_key(|task| task.id);
running_tasks.sort_by_key(|task| task.id);
if !self.has_rendered {
self.terminal.clear().context("Clearing ui failed")?;
self.has_rendered = true;
}
if !self.has_rendered {
self.terminal.clear().context("Clearing ui failed")?;
self.has_rendered = true;
}
let error_text = match self.has_errored {
true => {
let text: Cow<'static, str> = self
.log_path
.as_ref()
.map(|lp| {
let text = format!("Error(s) occurred and were logged to {}", lp.display());
Cow::Owned(text)
})
.unwrap_or_else(|| Cow::Borrowed("Error(s) occurred"));
Some(text)
}
false => None,
};
let error_text = match self.has_errored {
true => {
let text: Cow<'static, str> = self
.log_path
.as_ref()
.map(|lp| {
let text = format!("Error(s) occurred and were logged to {}", lp.display());
Cow::Owned(text)
})
.unwrap_or_else(|| Cow::Borrowed("Error(s) occurred"));
Some(text)
}
false => None,
};
self.terminal
.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref())
.split(f.size());
self.terminal
.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref())
.split(f.size());
let mut task_rect = chunks[0];
let mut task_rect = chunks[0];
if error_text.is_some() {
task_rect.height -= 3;
}
if error_text.is_some() {
task_rect.height -= 3;
}
for (row, task) in running_tasks
.into_iter()
.take(task_rect.height as usize / 2)
.enumerate()
{
f.render_widget(
Gauge::default()
.label(task.args.rel_from_path.to_string_lossy().as_ref())
.gauge_style(
Style::default()
.fg(Color::White)
.bg(Color::Black)
.add_modifier(Modifier::ITALIC),
)
.ratio(task.ratio.unwrap_or(0.0)),
Rect::new(
task_rect.x,
task_rect.y + row as u16 * 2,
task_rect.width,
1,
),
);
}
for (row, task) in running_tasks
.into_iter()
.take(task_rect.height as usize / 2)
.enumerate()
{
f.render_widget(
Gauge::default()
.label(task.args.rel_from_path.to_string_lossy().as_ref())
.gauge_style(
Style::default()
.fg(Color::White)
.bg(Color::Black)
.add_modifier(Modifier::ITALIC),
)
.ratio(task.ratio.unwrap_or(0.0)),
Rect::new(
task_rect.x,
task_rect.y + row as u16 * 2,
task_rect.width,
1,
),
);
}
if let Some(error_text) = error_text {
f.render_widget(
Paragraph::new(Text::raw(error_text)).style(
Style::default()
.fg(Color::Red)
.bg(Color::Black)
.add_modifier(Modifier::BOLD),
),
Rect::new(task_rect.x, task_rect.height + 1, task_rect.width, 2),
);
}
if let Some(error_text) = error_text {
f.render_widget(
Paragraph::new(Text::raw(error_text)).style(
Style::default()
.fg(Color::Red)
.bg(Color::Black)
.add_modifier(Modifier::BOLD),
),
Rect::new(task_rect.x, task_rect.height + 1, task_rect.width, 2),
);
}
f.render_widget(
Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("Overall Progress"),
)
.gauge_style(
Style::default()
.fg(Color::White)
.bg(Color::Black)
.add_modifier(Modifier::ITALIC),
)
.ratio(tasks_ended as f64 / task_len as f64),
chunks[1],
);
})
.context("Rendering ui failed")?;
f.render_widget(
Gauge::default()
.block(
Block::default()
.borders(Borders::ALL)
.title("Overall Progress"),
)
.gauge_style(
Style::default()
.fg(Color::White)
.bg(Color::Black)
.add_modifier(Modifier::ITALIC),
)
.ratio(tasks_ended as f64 / task_len as f64),
chunks[1],
);
})
.context("Rendering ui failed")?;
Ok(())
}
Ok(())
}
}
#[derive(Debug, Clone)]
struct Task {
id: usize,
ratio: Option<f64>,
args: ConversionArgs,
id: usize,
ratio: Option<f64>,
args: ConversionArgs,
}
pub fn init() -> (MsgQueue, impl Future<Output = Result<()>>) {
let queue = MsgQueue::new();
let queue = MsgQueue::new();
let queue_clone = queue.clone();
let fut = async move {
let mut interval = interval(Duration::from_millis(UPDATE_INTERVAL_MILLIS));
let mut wrapped = Some((Vec::new(), State::new()?));
let queue_clone = queue.clone();
let fut = async move {
let mut interval = interval(Duration::from_millis(UPDATE_INTERVAL_MILLIS));
let mut wrapped = Some((Vec::new(), State::new()?));
loop {
interval.tick().await;
loop {
interval.tick().await;
let (mut current_queue, mut state) = wrapped.take().context("`wrapped` is None")?;
let (mut current_queue, mut state) = wrapped.take().context("`wrapped` is None")?;
queue_clone.swap_inner(&mut current_queue);
queue_clone.swap_inner(&mut current_queue);
let render_res = task::spawn_blocking(move || -> Result<_> {
let mut exit = false;
for msg in current_queue.drain(..) {
if !state.process_msg(msg)? {
exit = true;
}
}
let render_res = task::spawn_blocking(move || -> Result<_> {
let mut exit = false;
for msg in current_queue.drain(..) {
if !state.process_msg(msg)? {
exit = true;
}
}
state.render()?;
state.render()?;
if exit {
Ok(None)
} else {
Ok(Some((current_queue, state)))
}
})
.await
.context("Ui update task failed")?
.context("Ui update failed")?;
if exit {
Ok(None)
} else {
Ok(Some((current_queue, state)))
}
})
.await
.context("Ui update task failed")?
.context("Ui update failed")?;
match render_res {
Some(s) => wrapped = Some(s),
None => break,
}
}
match render_res {
Some(s) => wrapped = Some(s),
None => break,
}
}
Result::<_>::Ok(())
};
Result::<_>::Ok(())
};
(queue, fut)
(queue, fut)
}