use anyhow::{Result, bail};
use crate::cli::{DisplayCommand, OnOff};
use crate::executor::Executor;
use crate::ipc::{Request, Response};
pub async fn run(exec: &Executor, sub: &DisplayCommand, json: bool) -> Result<()> {
let request = match sub {
DisplayCommand::Brightness { value } => match value {
Some(v) => Request::BrightnessSet { value: *v },
None => Request::BrightnessGet,
},
DisplayCommand::ColorTemp { value } => match value {
Some(v) => Request::ColorTempSet { value: *v },
None => Request::ColorTempGet,
},
DisplayCommand::Rotation => Request::RotationGet,
DisplayCommand::AutoBrightness { state } => match state {
OnOff::On => Request::AutoBrightnessOn,
OnOff::Off => Request::AutoBrightnessOff,
},
};
let response = exec.send_request(request).await?;
let data = match &response {
Response::Ok { data } => data,
Response::Error { message } => bail!("{message}"),
Response::TvDisconnected { message } => bail!("{message}"),
};
match sub {
DisplayCommand::Brightness { value } => match value {
Some(v) => {
if json {
println!("{}", serde_json::to_string_pretty(data)?);
} else {
println!("Brightness set to {v}.");
}
}
None => {
if json {
println!("{}", serde_json::to_string_pretty(data)?);
} else {
let val = data.get("brightness").and_then(|v| v.as_u64()).unwrap_or(0);
println!("Brightness: {val}");
}
}
},
DisplayCommand::ColorTemp { value } => match value {
Some(v) => {
if json {
println!("{}", serde_json::to_string_pretty(data)?);
} else {
println!("Color temperature set to {v}.");
}
}
None => {
if json {
println!("{}", serde_json::to_string_pretty(data)?);
} else {
let val = data.get("color_temp").and_then(|v| v.as_i64()).unwrap_or(0);
println!("Color temperature: {val}");
}
}
},
DisplayCommand::Rotation => {
if json {
println!("{}", serde_json::to_string_pretty(data)?);
} else {
let rot = data
.get("rotation")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
println!("Rotation: {rot}");
}
}
DisplayCommand::AutoBrightness { state } => {
if json {
println!("{}", serde_json::to_string_pretty(data)?);
} else {
let word = match state {
OnOff::On => "enabled",
OnOff::Off => "disabled",
};
println!("Auto-brightness {word}.");
}
}
}
Ok(())
}