Add volume display

This commit is contained in:
Erwin Boskma 2022-01-14 10:06:26 +01:00
parent 1cc0390432
commit d296105443
Signed by: erwin
GPG key ID: 270B20D17394F7E5
6 changed files with 328 additions and 26 deletions

2
.envrc
View file

@ -1 +1,3 @@
use flake
dotenv_if_exists

237
Cargo.lock generated
View file

@ -2,6 +2,36 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "anyhow"
version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84450d0b4a8bd1ba4144ce8ce718fbc5d071358b1e5384bace6536b3d1f2d5b3"
[[package]]
name = "atty"
version = "0.2.14"
@ -19,6 +49,21 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "321629d8ba6513061f26707241fa9bc89524ff1cd7a915a97ef0c62c666ce1b6"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "base64"
version = "0.13.0"
@ -86,6 +131,33 @@ dependencies = [
"syn",
]
[[package]]
name = "color-eyre"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d6ec7641ff3474b7593009c809db602c414cd97c7d47a78ed004162b74ff96c"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]]
name = "core-foundation"
version = "0.9.2"
@ -111,6 +183,16 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "eyre"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "221239d1d5ea86bf5d6f91c9d6bc3646ffe471b08ff9b0f91c44f115ac969d2b"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -201,6 +283,12 @@ dependencies = [
"wasi",
]
[[package]]
name = "gimli"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4"
[[package]]
name = "h2"
version = "0.3.7"
@ -224,10 +312,14 @@ dependencies = [
name = "ha-now-playing"
version = "0.2.0"
dependencies = [
"anyhow",
"clap",
"color-eyre",
"reqwest",
"serde",
"serde_json",
"tracing",
"tracing-subscriber",
]
[[package]]
@ -336,6 +428,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "indexmap"
version = "1.7.0"
@ -388,6 +486,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata",
]
[[package]]
name = "matches"
version = "0.1.9"
@ -406,6 +513,16 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "miniz_oxide"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
dependencies = [
"adler",
"autocfg",
]
[[package]]
name = "mio"
version = "0.7.14"
@ -465,6 +582,15 @@ dependencies = [
"libc",
]
[[package]]
name = "object"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.8.0"
@ -513,6 +639,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "owo-colors"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20448fd678ec04e6ea15bbe0476874af65e98a01515d667aa49f1434dc44ebf4"
[[package]]
name = "percent-encoding"
version = "2.1.0"
@ -634,6 +766,30 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -678,6 +834,12 @@ dependencies = [
"winreg",
]
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "ryu"
version = "1.0.5"
@ -760,12 +922,27 @@ dependencies = [
"serde",
]
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]]
name = "slab"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
[[package]]
name = "smallvec"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "socket2"
version = "0.4.2"
@ -825,6 +1002,15 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "thread_local"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
dependencies = [
"once_cell",
]
[[package]]
name = "tinyvec"
version = "1.5.1"
@ -894,9 +1080,21 @@ checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f480b8f81512e825f337ad51e94c1eb5d3bbdf2b363dcd01e2b19a9ffe3f8e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.21"
@ -906,6 +1104,45 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "tracing-error"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-log"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6923477a48e41c1951f1999ef8bb5a3023eb723ceadafe78ffb65dc366761e3"
dependencies = [
"lazy_static",
"log",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d81bfa81424cc98cb034b837c985b7a290f592e5b4322f353f94a0ab0f9f594"
dependencies = [
"ansi_term",
"lazy_static",
"matchers",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]
[[package]]
name = "try-lock"
version = "0.2.3"

View file

@ -6,10 +6,14 @@ authors = ["Erwin Boskma <erwin@datarift.nl>"]
edition = "2021"
[dependencies]
anyhow = "1.0.52"
clap = "3.0.0-beta.2"
color-eyre = "0.6.0"
reqwest = { version = "0.11.4", features = ["blocking", "json"] }
serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64"
tracing = "0.1.29"
tracing-subscriber = { version = "0.3.5", features = ["env-filter"] }
[profile.release]
opt-level = "z"

View file

@ -41,9 +41,33 @@
"inputs": {
"naersk": "naersk",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay",
"utils": "utils"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": [
"utils"
],
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1642128126,
"narHash": "sha256-av8JUACdrTfQYl/ftZJvKpZEmZfa0avCq7tt5Usdoq0=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "ce4ef6f2d74f2b68f7547df1de22d1b0037ce4ad",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"utils": {
"locked": {
"lastModified": 1637014545,

View file

@ -7,15 +7,22 @@
url = "github:nmattia/naersk/master";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "utils";
};
};
outputs = { self, nixpkgs, utils, naersk }:
outputs = { self, nixpkgs, utils, naersk, rust-overlay }:
utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
naersk-lib = pkgs.callPackage naersk { };
buildInputs = [
(pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml)
pkgs.openssl
];
nativeBuildInputs = [
@ -42,9 +49,8 @@
};
devShell = with pkgs; mkShell {
inherit nativeBuildInputs;
buildInputs = [ cargo cargo-edit cargo-diet cargo-feature cargo-outdated rustc rustfmt pre-commit rustPackages.clippy ] ++ buildInputs;
nativeBuildInputs = [ jq ] ++ nativeBuildInputs;
buildInputs = [ cargo-edit cargo-expand cargo-diet cargo-feature cargo-outdated pre-commit ] ++ buildInputs;
RUST_SRC_PATH = rustPlatform.rustLibSrc;
};

View file

@ -1,7 +1,10 @@
use std::{io::Read, path::PathBuf};
use clap::Parser;
use color_eyre::Report;
use serde_json::json;
use tracing::{debug, error};
use tracing_subscriber::EnvFilter;
/// Bert
#[derive(Parser, Debug)]
@ -27,6 +30,9 @@ struct Opts {
#[clap(short, long)]
insecure: bool,
#[clap(short, long)]
debug: bool,
#[clap(subcommand)]
cmd: Option<Command>,
}
@ -40,19 +46,27 @@ enum Command {
/// Lower volume
VolumeDown,
}
fn main() {
if let Err(e) = do_main() {
error!("Fatal error: {}", e);
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
fn do_main() -> Result<(), Box<dyn std::error::Error>> {
let opts = Opts::parse();
let Opts {
host,
entity,
insecure,
debug,
cmd,
token,
token_file,
} = opts;
setup(debug)?;
let token = if let Some(token) = token {
Some(token)
} else if let Some(token_file) = token_file {
@ -78,6 +92,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// println!("{:#?}", response);
Ok(())
}
fn setup(debug: bool) -> Result<(), Report> {
if debug {
std::env::set_var("RUST_LOG", "debug");
}
color_eyre::install()?;
tracing_subscriber::fmt::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_writer(std::io::stderr)
.init();
Ok(())
}
fn get_now_playing(
host: String,
entity: String,
@ -100,30 +128,31 @@ fn get_now_playing(
.send()?
.json::<serde_json::Value>()?;
debug!("Response: {:#?}", response);
if response["state"] == "playing" {
let attributes = &response["attributes"];
let maybe_channel = attributes["media_channel"].as_str();
let now_playing = if let Some(channel) = maybe_channel {
format!(
"[{}] {} - {}",
channel,
attributes["media_artist"]
.as_str()
.map_or("No artist", |artist| { artist }),
attributes["media_title"]
.as_str()
.map_or("No title", |title| { title }),
)
let artist = attributes["media_artist"]
.as_str()
.map_or("No artist", |artist| artist);
let title = attributes["media_title"]
.as_str()
.map_or("No title", |title| title);
let volume_raw = attributes["volume_level"]
.as_f64()
.map_or(-1., |volume| volume);
let volume = if volume_raw >= 0. {
format!("[{:3.0}%] ", volume_raw * 100.)
} else {
format!(
"{} - {}",
attributes["media_artist"]
.as_str()
.map_or("No artist", |artist| artist),
attributes["media_title"]
.as_str()
.map_or("No title", |title| title),
)
String::new()
};
let now_playing = if let Some(channel) = maybe_channel {
format!("{volume}[{channel}] {artist} - {title}")
} else {
format!("{volume}{artist} - {title}")
};
println!("{}", now_playing);