1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
mod builder;
mod format;

pub use format::prelude::*;

use super::data::prelude::*;
use crate::error::prelude::*;
use std::fmt;

use builder::StatusOutputBuilder;

const OVERFLOW_STR: &str = "...";

pub struct StatusOutput {
    data:   CmusData,
    format: Format,
}

impl StatusOutput {
    pub fn builder() -> StatusOutputBuilder {
        StatusOutputBuilder::default()
    }

    fn get_format_text_for_parts<'a>(
        &self,
        parts: Vec<&'a FormatPart>,
    ) -> String {
        parts
            .iter()
            .filter_map(|part| self.get_format_text(part))
            .collect::<Vec<String>>()
            .join("")
    }

    fn get_format_text(&self, part: &FormatPart) -> Option<String> {
        match part {
            FormatPart::Text(text) => Some(text.to_string()),

            FormatPart::Title => self.data.get_title(),

            FormatPart::Status => Some(self.data.get_status().to_string()),

            // TODO: Deprecated
            FormatPart::MatchStatus(playback_status, text) => {
                if self.data.is_status(playback_status) {
                    Some(text.to_string())
                } else {
                    None
                }
            }

            FormatPart::Truncate(format_part_inner, max) => {
                let max = *max;
                self.get_format_text(format_part_inner.as_ref())
                    .map(|text| {
                        let mut text = text.to_string();
                        if text.len() > max {
                            let overflow_str_len = OVERFLOW_STR.len();
                            if max >= overflow_str_len * 2 {
                                text.truncate(max - overflow_str_len);
                                text.push_str(OVERFLOW_STR);
                            } else {
                                text.truncate(max);
                            }
                        }
                        text
                    })
            }

            FormatPart::HtmlEscape(format_part_inner) => self
                .get_format_text(format_part_inner.as_ref())
                .map(|text| htmlescape::encode_minimal(text.as_str())),

            FormatPart::ProgressBar(bar_config) => {
                if let Some(time) = self.data.get_time() {
                    let width = bar_config.inner_width();
                    let percent_complete = time.completion_percentage();
                    let characters =
                        (width as f32 * percent_complete).round() as usize;
                    Some(bar_config.text_with_filled(characters))
                } else {
                    None
                }
            }

            FormatPart::Container(format_parts_inner) => Some(
                self.get_format_text_for_parts(
                    format_parts_inner
                        .iter()
                        .map(std::ops::Deref::deref)
                        .collect(),
                ),
            ),

            FormatPart::If(expression, format_part_inner) => {
                if self.is_expression_true(expression) {
                    self.get_format_text(format_part_inner)
                } else {
                    None
                }
            }
        }
    }

    fn is_expression_true(&self, expression: &FormatExpression) -> bool {
        match expression {
            FormatExpression::True => true,
            FormatExpression::False => false,

            FormatExpression::And(expr_one, expr_two) => {
                self.is_expression_true(expr_one)
                    && self.is_expression_true(expr_two)
            }

            FormatExpression::Or(expr_one, expr_two) => {
                self.is_expression_true(expr_one)
                    || self.is_expression_true(expr_two)
            }

            FormatExpression::Not(expr) => !self.is_expression_true(expr),

            FormatExpression::IsStatus(playback_status) => {
                self.data.is_status(&playback_status)
            }
        }
    }
}

impl fmt::Display for StatusOutput {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            self.get_format_text_for_parts(self.format.iter().collect())
        )
    }
}