Skip to content

Commit

Permalink
feat: add enable command
Browse files Browse the repository at this point in the history
Relates to #16
  • Loading branch information
johnallen3d committed Sep 24, 2023
1 parent 84f9740 commit e527175
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub(crate) enum Commands {
/// List current outputs
#[command()]
Outputs,
/// Enable the given output(s).
/// example: `mp-cli enable [only] <output # or name> [...]`
#[command()]
Enable { args: Vec<String> },
/// Display the next song in the queue
#[command()]
Queued,
Expand Down
42 changes: 41 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Client {
pub fn outputs(&mut self) -> eyre::Result<Option<String>> {
let outputs = self.client.outputs()?;
let outputs: Vec<Output> =
outputs.into_iter().map(|p| Output::from(p.name)).collect();
outputs.into_iter().map(Output::from).collect();
let outputs = Outputs { outputs };

let response = match self.format {
Expand All @@ -245,6 +245,46 @@ impl Client {
Ok(Some(response))
}

pub fn enable(
&mut self,
args: Vec<String>,
) -> eyre::Result<Option<String>> {
let mut only = false;
let mut outputs = Vec::new();

for arg in args {
if arg == "only" {
only = true;
} else {
outputs.push(arg);
}
}

if only {
// first disable all outputs
for output in self.client.outputs()? {
self.client.output(output, false)?;
}
}

for name in outputs {
let id: u32 = if let Ok(parsed_id) = name.parse::<u32>() {
parsed_id
} else {
self.client
.outputs()?
.iter()
.find(|&o| o.name == name)
.ok_or_else(|| eyre::eyre!("unknown output: {}", name))?
.id
};

self.client.out_enable(id)?;
}

self.outputs()
}

pub fn queued(&mut self) -> eyre::Result<Option<String>> {
if let Some(song) =
self.client.queue().map_err(|e| eyre::eyre!(e))?.get(0)
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn main() {

Some(Commands::Clear) => mpd.clear(),
Some(Commands::Outputs) => mpd.outputs(),
Some(Commands::Enable { args }) => mpd.enable(args),
Some(Commands::Queued) => mpd.queued(),
Some(Commands::Shuffle) => mpd.shuffle(),
Some(Commands::Lsplaylists) => mpd.lsplaylists(),
Expand Down
46 changes: 40 additions & 6 deletions src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,52 @@ pub struct Outputs {
}

#[derive(Serialize)]
pub struct Output(String);
pub struct Output {
pub id: u32,
pub name: String,
pub enabled: Enabled,
}

#[derive(Serialize)]
#[serde(rename_all(serialize = "lowercase"))]
pub enum Enabled {
Enabled,
Disabled,
}

impl From<String> for Output {
fn from(name: String) -> Self {
Output(name)
impl From<bool> for Enabled {
fn from(value: bool) -> Self {
if value {
Enabled::Enabled
} else {
Enabled::Disabled
}
}
}

impl fmt::Display for Enabled {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Enabled::Enabled => write!(f, "enabled"),
Enabled::Disabled => write!(f, "disabled"),
}
}
}

impl From<mpd::output::Output> for Output {
fn from(inner: mpd::output::Output) -> Self {
Output {
id: inner.id,
name: inner.name,
enabled: Enabled::from(inner.enabled),
}
}
}

impl fmt::Display for Outputs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, output) in self.outputs.iter().enumerate() {
write!(f, "{}={}", index, output.0)?;
for output in &self.outputs {
write!(f, "{}={} ({})", output.id, output.name, output.enabled)?;
}

Ok(())
Expand Down

0 comments on commit e527175

Please sign in to comment.