🏡 index : ~doyle/sonos-cli.git

author Jordan Doyle <jordan@doyle.la> 2020-02-22 19:53:15.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2020-02-22 19:53:15.0 +00:00:00
commit
0ed4c77dcd502f1475faf39bfe4c17da0b7daf02 [patch]
tree
429e0f818c3d39d68764165fca5e635330f43191
parent
c7d94479c7022b7f73225b708b22416f5991e9ed
download
0ed4c77dcd502f1475faf39bfe4c17da0b7daf02.tar.gz

Show nice volume prompt



Diff

 src/main.rs | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 8f7cbbe..085f347 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -155,7 +155,7 @@ fn main() {
            if let Some(volume) = sub.value_of("VOLUME") {
                speaker.set_volume(volume.parse().unwrap());
            } else {
                info!("{}", speaker.volume().unwrap());
                info!("{}", Volume::new(speaker.volume().unwrap(), speaker.muted().unwrap()));
            }
        },
        ("seek", Some(sub)) => {
@@ -299,12 +299,14 @@ impl std::fmt::Display for Track {
#[derive(Serialize, Deserialize, Debug)]
struct Volume {
    volume: u8,
    muted: bool,
}

impl Volume {
    pub fn new(vol: u8) -> Volume {
    pub fn new(vol: u8, muted: bool) -> Volume {
        Self {
            volume: vol,
            muted,
        }
    }
}
@@ -314,9 +316,15 @@ impl std::fmt::Display for Volume {
        const MAX_VOLUME: usize = 100;
        const PROG_BAR_LEN: usize = 25;

        write!(f, "\u{1F50A}  {}/{}", self.volume, MAX_VOLUME)?;
        let pictogram = if self.muted {
            "\u{1F507}"
        } else {
            "\u{1F50A}"
        };

        let percent = (self.volume as usize / MAX_VOLUME) * PROG_BAR_LEN;
        write!(f, "{} {}/{}", pictogram, self.volume, MAX_VOLUME)?;

        let percent = (self.volume as usize * PROG_BAR_LEN) / MAX_VOLUME;

        write!(f, " [{}{}]", "\u{2587}".repeat(percent), "-".repeat(PROG_BAR_LEN - percent))
    }