rain 1.0.1

Visualize vertical data inside your terminal.
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! # Visualize vertical data inside your terminal
//!
//! This library helps you to display line based data vertically within your
//! terminal. The color of the actual graph represents its value, whereas blue
//! is low and red is high. These color bounds will be calculated automatically
//! during runtime. Beside this, the terminal dimensions are adapted during
//! runtime, too. If no data was added to a line, their terminal line is dashed.
//!
//! # Example usage
//! ```
//! use rain::Graph;
//!
//! // Get a drawing area
//! let mut graph = Graph::new();
//!
//! // Get some line identifiers
//! let l1 = "Line 1";
//! let l2 = "Line 1";
//! let l3 = "Line 1";
//!
//! // Add some values and print
//! assert!(graph.add(l1, 0).is_ok());
//! assert!(graph.add(l2, 0).is_ok());
//! graph.print();
//!
//! // Add more values and print
//! assert!(graph.add(l2, 5).is_ok());
//! assert!(graph.add(l3, 10).is_ok());
//! graph.print();
//!
//! // Remove a line and print
//! assert!(graph.remove(l1).is_ok());
//! graph.print();
//! ```
#![deny(missing_docs)]

use failure::{bail, Fallible};
use log::{debug, info, warn, LevelFilter};
use std::{cmp::max, convert, fmt, iter, u8};
use termion::color::{self, Fg, LightBlack, Reset};

/// The graph drawing structure
pub struct Graph<V> {
    lines_to_be_removed: Vec<String>,
    columns: Vec<Column<V>>,
    prefix_len: usize,
}

impl<V> Graph<V>
where
    V: Clone + Default + Ord + PartialEq + fmt::Debug,
    f64: convert::From<V>,
{
    /// Create a new `Graph` for drawing
    ///
    /// # Example
    /// ```
    /// use rain::Graph;
    ///
    /// let _: Graph<u8> = Graph::new();
    /// ```
    pub fn new() -> Self {
        Self::with_prefix_length(8)
    }

    /// Create a new `Graph` for drawing with a custom length of the identifier
    /// (prefix)
    ///
    /// # Example
    /// ```
    /// use rain::Graph;
    ///
    /// let _: Graph<u8> = Graph::with_prefix_length(25);
    /// ```
    pub fn with_prefix_length(length: usize) -> Self {
        Graph {
            lines_to_be_removed: vec![],
            columns: vec![],
            prefix_len: length + 3,
        }
    }

    /// Set the global log level for reporting
    pub fn set_log_level(self, level: LevelFilter) -> Self {
        // Setup the logger if not already set
        if mowl::init_with_level(level).is_err() {
            warn!("Logger already set.");
        } else {
            info!("Log level set to: {:?}", level);
        }
        self
    }

    /// Add a data value to the graph by some identifier which can be displayed
    /// somehow.
    ///
    /// # Example
    /// ```
    /// use rain::Graph;
    ///
    /// let mut graph = Graph::new();
    /// let line = graph.add("Line 1", 0).unwrap();
    ///
    /// assert_eq!(line, "Line 1");
    /// ```
    pub fn add<T>(&mut self, identifier: T, value: V) -> Fallible<T>
    where
        T: fmt::Display,
    {
        // Get a line name string from the identifier
        let line_name = format!("{}", identifier);
        debug!("Adding value {:?} to line '{}'", value, line_name);

        // Just add the value if the line already exist
        let add_new_line = {
            if let Some(line) = self.line_already_existing(&line_name) {
                debug!("Line already exist, just adding the value");
                line.add_value(value.clone());
                false
            } else {
                true
            }
        };

        // Add a new line and set the column as used
        if add_new_line {
            debug!("Adding new line");
            let column = self.get_next_free_column();
            let mut line = Line::new(&line_name);
            line.add_value(value);
            *column = Column::Used(line);
        }

        Ok(identifier)
    }

    /// Remove a line from the graph
    ///
    /// # Example
    /// ```
    /// use rain::Graph;
    ///
    /// let mut graph = Graph::new();
    /// let line = graph.add("Line 1", 0).unwrap();
    ///
    /// let removed_line = graph.remove(line).unwrap();
    /// assert_eq!(removed_line, "Line 1");
    /// ```
    pub fn remove<T>(&mut self, identifier: T) -> Fallible<T>
    where
        T: fmt::Display,
    {
        // Check if the line exists
        let line_name = format!("{}", identifier);
        if self.line_already_existing(&line_name).is_none() {
            bail!("Line does not exist and can not be removed");
        }

        // Just push the line into a temporarily vector
        self.lines_to_be_removed.push(line_name);

        Ok(identifier)
    }

    /// Prints the graph
    ///
    /// # Example
    /// ```
    /// use rain::Graph;
    ///
    /// let mut graph = Graph::new();
    ///
    /// assert!(graph.add("Line 1", 0).is_ok());
    /// assert!(graph.add("Line 2", 0).is_ok());
    ///
    /// graph.print();
    /// ```
    pub fn print(&mut self) -> Fallible<()> {
        /// Prints the fillchar to the terminal
        fn fillchar() -> String {
            format!("{}{}", Fg(LightBlack), Fg(Reset))
        }

        // Do the actual printing per column
        let start_ch = "";
        let line_chr = "";
        let nodata_c = "";
        let end_char = "";
        let col_width = 2;

        let (width, _) = termion::terminal_size()?;

        let mut cursor = self.prefix_len as u16;
        let end_cursor = if width % 2 == 0 { width - 2 } else { width - 1 };

        // A string representation for a row to be printed
        struct Row {
            content: String,
            prefix: Option<String>,
        }
        let mut row = Row {
            content: String::with_capacity(width as usize),
            prefix: None,
        };

        // Returns the maximum or minimum value of all available values
        macro_rules! get_value {
            ($($p:ident)*) => (
                $(self.columns.iter().filter_map(|c| {
                    match *c {
                        Column::Used(ref line) => line.values.iter().$p(),
                        _ => None,
                    }
                }).$p().cloned().unwrap_or_default())*
            )
        }

        // Get the current minimum and maximum values from all lines
        let (min, max) = (get_value!(min), get_value!(max));

        // Gather all columns together
        for column in &mut self.columns {
            // Check if we an print more columns
            if end_cursor < cursor + col_width {
                row.content += "";
                cursor += 1;
                break;
            }

            // Column can be printed
            let free_column = match *column {
                Column::Used(ref mut line) => {
                    // Get a row prefix format and keep three characters left
                    let mut row_prefix = format!(
                        "{:>w$.*}",
                        self.prefix_len - 3,
                        line.name,
                        w = self.prefix_len - 3
                    );

                    // Get the character to be printed
                    let (c, free_column) = if line.started {
                        // Check if the line is done an can be used later on
                        if self.lines_to_be_removed.contains(&line.name) {
                            row_prefix += "";
                            row.prefix = Some(row_prefix);
                            (end_char, true)
                        } else {
                            (
                                if line.got_data { line_chr } else { nodata_c },
                                false,
                            )
                        }
                    } else {
                        row_prefix += "";
                        row.prefix = Some(row_prefix);
                        line.started = true;
                        (start_ch, false)
                    };

                    // Get the rgb value for the character
                    let value = line.values.last().cloned().unwrap_or_default();
                    let (r, g, b) =
                        Self::rgb(min.clone(), max.clone(), value.clone());

                    row.content += &format!(
                        "{}{}{}",
                        Fg(color::Rgb(r, g, b)),
                        c,
                        Fg(Reset)
                    );
                    row.content += &fillchar();

                    // Reset the line indicator for the data
                    line.got_data = false;

                    free_column
                }
                Column::Free => {
                    row.content += &fillchar();
                    row.content += &fillchar();
                    false
                }
            };
            if free_column {
                *column = Column::Free;
            }

            cursor += col_width;
        }

        // Fill rest of the screen
        for _ in cursor..width {
            row.content += &fillchar();
        }

        // Print the row including the prefix if set
        let prefix_string = match row.prefix {
            Some(prefix) => prefix,
            _ => iter::repeat(' ').take(self.prefix_len).collect::<String>(),
        };
        println!("{}{}", prefix_string, row.content);

        // Cleanup lines to be removed
        self.lines_to_be_removed.clear();
        Ok(())
    }

    /// Print only if new data is available. Returns an indicator if somethings
    /// was printed or not.
    ///
    /// # Example
    /// ```
    /// use rain::Graph;
    ///
    /// let mut graph = Graph::new();
    ///
    /// assert!(graph.add("Line 1", 0).is_ok());
    /// assert!(graph.add("Line 2", 0).is_ok());
    ///
    /// graph.print_if_new_data();
    /// graph.print_if_new_data();
    /// ```
    pub fn print_if_new_data(&mut self) -> Fallible<bool> {
        if !self.lines_to_be_removed.is_empty()
            || self
                .columns
                .iter()
                .filter(|c| match **c {
                    Column::Used(ref line) if line.got_data => true,
                    _ => false,
                })
                .count()
                > 0
        {
            self.print()?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Get the next free column and set the column as used
    fn get_next_free_column(&mut self) -> &mut Column<V> {
        let free_column_count = self
            .columns
            .iter_mut()
            .filter(|c| **c == Column::Free)
            .count();

        if free_column_count == 0 {
            self.columns.push(Column::Free);
            self.columns.iter_mut().rev().next().unwrap()
        } else {
            self.columns
                .iter_mut()
                .find(|c| **c == Column::Free)
                .unwrap()
        }
    }

    // Returns a line if the name already exist within all columns
    fn line_already_existing(
        &mut self,
        line_name: &str,
    ) -> Option<&mut Line<V>> {
        let line_string = line_name.to_owned();
        self.columns
            .iter_mut()
            .filter_map(|c| match *c {
                Column::Used(ref mut line) if line_string == line.name => {
                    Some(line)
                }
                _ => None,
            })
            .next()
    }

    fn rgb(minimum: V, maximum: V, value: V) -> (u8, u8, u8) {
        // Lightens up the colors
        let soft_scale = 125;
        if minimum == maximum {
            return (soft_scale, soft_scale, u8::MAX);
        }

        // Calculate a RGB value over the complete gradient
        let minimum = f64::from(minimum);
        let maximum = f64::from(maximum);
        let value = f64::from(value);
        let ratio = 2f64 * (value - minimum) / (maximum - minimum);
        let mut b = max(0, (255f64 * (1f64 - ratio)) as i64) as u8;
        let mut r = max(0, (255f64 * (ratio - 1f64)) as i64) as u8;
        let mut g = (255 - b - r) as u8;

        // Lighten up the values
        b = b.saturating_add(soft_scale);
        r = r.saturating_add(soft_scale);
        g = g.saturating_add(soft_scale);

        (r, g, b)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
/// Representation of a set of data `Point` values
struct Line<V> {
    got_data: bool,
    name: String,
    started: bool,
    values: Vec<V>,
}

impl<V> Line<V> {
    /// Creates a new `Line`
    fn new(name: &str) -> Self {
        Line {
            got_data: false,
            name: name.to_owned(),
            started: false,
            values: vec![],
        }
    }

    /// Adds a value to a line
    fn add_value(&mut self, value: V) {
        self.values.push(value);
        self.got_data = true;
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
/// Specifies if a column can be used or not
enum Column<V> {
    /// Column free for usage
    Free,

    /// Column already in use
    Used(Line<V>),
}