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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use ansi_term::Colour::{Blue, Cyan, Green, Red, Yellow};
use ansi_term::Style;
use std::{f32::EPSILON, fs::read_to_string, process};
pub mod app;

/// This function takes a `printr` object and runs it through all the associated methods so
/// that all the arguments, flags and switches are accounted for.
pub fn run(printr: &mut Printr) {
    printr.run_all_handles()
}

#[derive(Debug, PartialEq)]
struct Config {
    // if `-E` is supplied, this will be `false`
    interpretations: bool,
    // if `-n` is supplied, this will be `true`
    newline: bool,
    // if `-s` is supplied, this will become `true`
    spaces: bool,
    // the color of the output, will be automatically guessed from the context if not supplied
    // can be set to `None` for plain output
    // the possible values are red (-1), blue (0), green (1), yellow, cyan, None
    color: Option<Color>,
    // the formatting to be applied to the output string
    format: Option<Format>,
    // whether the output should be completely plain
    plain: bool,
}

impl Config {
    fn new(
        interpretations: bool,
        newline: bool,
        spaces: bool,
        plain: bool,
        color: Option<Color>,
        format: Option<Format>,
    ) -> Self {
        Self {
            interpretations,
            newline,
            spaces,
            plain,
            color,
            format,
        }
    }
}

/// The main struct that is responsible for generating the output string that will be
/// displayed to the end user.
#[derive(Debug, PartialEq)]
pub struct Printr {
    // the input `STRING`, if the `-f` is supplied, this will contain the contents of the file
    string: Vec<String>,
    // the final sentiment of the `string`
    sentiment: Option<Sentiment>,
    // the output string that will be displayed
    output_string: Option<String>,
    // configuration
    config: Config,
}

impl Printr {
    /// Create a new object of this struct.
    ///
    /// **NOTE:** None of the handles are run at this point, so the output string will be
    /// unformatted. Refer to [`run_all_handles`](struct.Printr.html#method.run_all_handles).
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        interpretations: bool,
        newline: bool,
        plain: bool,
        spaces: bool,
        file: Option<String>,
        color: Option<Color>,
        string: Option<Vec<String>>,
        format: Option<Format>,
    ) -> Self {
        let string = match file {
            Some(f) => {
                let contents = read_to_string(&f).unwrap_or_else(|err| {
                    eprintln!("Could not find file: {}", err);
                    process::exit(1);
                });
                vec![contents]
            }
            None => match string {
                Some(s) => s,
                None => vec![String::new()],
            },
        };
        let config = Config::new(interpretations, newline, spaces, plain, color, format);
        Self {
            string,
            sentiment: None,
            config,
            output_string: None,
        }
    }
    /// We perform sentiment analysis.
    pub fn determine_sentiment(&mut self) {
        let sentiment = Sentiment::new(self.string.clone());
        self.sentiment = Some(sentiment);
    }
    /// We handle the `-s` option here.
    pub fn handle_spaces(&mut self) {
        if self.config.spaces {
            self.output_string = Some(self.string.join(""))
        } else {
            self.output_string = Some(self.string.join(" "))
        }
    }
    /// We handle the `-e` and `-E` options here.
    pub fn handle_interpretations(&mut self) {
        if self.config.interpretations {
            self.output_string = Some(self.output_string.clone().unwrap().replace(r"\\", r"\"));
        }
    }
    /// We determine the color that should be applied to the output.
    pub fn determine_color(&mut self) {
        if !self.config.plain && self.config.color.is_none() {
            self.config.color = Some(determine_color(&self.sentiment.clone().unwrap()));
        }
    }
    /// We handle the `-c` option here.
    pub fn handle_coloring(&mut self) {
        self.output_string = match self.config.color {
            Some(Color::Blue) => Some(Blue.paint(self.output_string.clone().unwrap()).to_string()),
            Some(Color::Red) => Some(Red.paint(self.output_string.clone().unwrap()).to_string()),
            Some(Color::Green) => {
                Some(Green.paint(self.output_string.clone().unwrap()).to_string())
            }
            Some(Color::Yellow) => Some(
                Yellow
                    .paint(self.output_string.clone().unwrap())
                    .to_string(),
            ),
            Some(Color::Cyan) => Some(Cyan.paint(self.output_string.clone().unwrap()).to_string()),
            None => self.output_string.clone(),
        };
    }
    /// We handle the `-f` option here.#
    pub fn handle_formatting(&mut self) {
        self.output_string = match self.config.format {
            Some(Format::Bold) => Some(
                Style::new()
                    .bold()
                    .paint(self.output_string.clone().unwrap())
                    .to_string(),
            ),
            Some(Format::Underline) => Some(
                Style::new()
                    .underline()
                    .paint(self.output_string.clone().unwrap())
                    .to_string(),
            ),
            Some(Format::Dimmed) => Some(
                Style::new()
                    .dimmed()
                    .paint(self.output_string.clone().unwrap())
                    .to_string(),
            ),
            Some(Format::Strikethrough) => Some(
                Style::new()
                    .strikethrough()
                    .paint(self.output_string.clone().unwrap())
                    .to_string(),
            ),
            None => self.output_string.clone(),
        }
    }
    /// We handle the `-n` option here.
    pub fn handle_newline(&mut self) {
        if !self.config.newline {
            self.output_string = Some(format!("{}\n", self.output_string.clone().unwrap()));
        }
    }
    /// This runs all the above functions so that all the switches and flags are accounted
    /// for.
    pub fn run_all_handles(&mut self) {
        self.determine_sentiment();
        self.handle_spaces();
        self.handle_interpretations();
        self.determine_color();
        self.handle_coloring();
        self.handle_newline();
        self.handle_formatting();
    }
    /// This method will return the final string that should be displayed.
    pub fn get_output_string(self) -> String {
        match self.output_string {
            Some(s) => s,
            None => "".to_string(),
        }
    }
}

fn determine_color(sentiment: &Sentiment) -> Color {
    let polarity = sentiment.clone().get_polarity();
    if polarity == 1 {
        Color::Green
    } else if polarity == -1 {
        Color::Red
    } else {
        Color::Blue
    }
}

#[derive(Debug, PartialEq, Clone)]
struct Sentiment(
    // the positivity score of the sentence being analysed
    f32,
    // the negativity score of the sentence being analysed
    f32,
);

impl Sentiment {
    fn new(string: Vec<String>) -> Self {
        let string = string.join(" ");
        let analyser = sentiment::analyze(string);
        Self(analyser.positive.score, analyser.negative.score)
    }
    fn get_polarity(self) -> i8 {
        if (self.0 - self.1).abs() < EPSILON {
            0
        } else if self.0 > self.1 {
            1
        } else {
            -1
        }
    }
}

/// The colors in which the output can be displayed in.
#[derive(Debug, PartialEq)]
pub enum Color {
    Red,
    Blue,
    Green,
    Yellow,
    Cyan,
}

/// The formats in which the output can be displayed in.
#[derive(Debug, PartialEq)]
pub enum Format {
    Bold,
    Underline,
    Strikethrough,
    Dimmed,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{error::Error, io::Write};
    use tempfile::NamedTempFile;

    // we keep using the word "zealous" here since we know it's
    // [afinn](https://github.com/fnielsen/afinn) score is 2.0 and can be used consistently for
    // tests

    #[test]
    fn test_new_being_created_with_filename_supplied() -> Result<(), Box<dyn Error>> {
        let mut file = NamedTempFile::new()?;
        let content = "zealous";
        write!(file, "{}", content)?;
        let mut printr = Printr::new(
            true,
            true,
            false,
            false,
            Some(file.path().to_str().unwrap().to_string()),
            None,
            None,
            None,
        );
        printr.run_all_handles();
        assert_eq!(
            printr,
            Printr {
                string: vec![content.to_string()],
                sentiment: Some(Sentiment(2.0, 0.0)),
                output_string: Some("\u{1b}[32mzealous\u{1b}[0m".to_string()),
                config: Config {
                    color: Some(Color::Green),
                    format: None,
                    interpretations: true,
                    newline: true,
                    plain: false,
                    spaces: false
                }
            }
        );
        Ok(())
    }

    #[test]
    fn test_new_being_created_with_input_string_supplied() -> Result<(), Box<dyn Error>> {
        let content = String::from("zealous");
        let mut printr = Printr::new(
            true,
            true,
            false,
            false,
            None,
            None,
            Some(vec![content.clone()]),
            Some(Format::Bold),
        );
        printr.run_all_handles();
        assert_eq!(
            printr,
            Printr {
                string: vec![content.clone()],
                sentiment: Some(Sentiment(2.0, 0.0)),
                output_string: Some("\u{1b}[1m\u{1b}[32mzealous\u{1b}[0m\u{1b}[0m".to_string()),
                config: Config {
                    interpretations: true,
                    newline: true,
                    color: Some(Color::Green),
                    spaces: false,
                    plain: false,
                    format: Some(Format::Bold),
                }
            }
        );
        Ok(())
    }
}