aboutsummaryrefslogtreecommitdiff
path: root/src/output.rs
blob: 37daf923e4d1ce44ebdc18570389554e0454e869 (plain)
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
// output.rs

// Copyright (C) 2020 The Nitrocli Developers
// SPDX-License-Identifier: GPL-3.0-or-later

//! Defines data types that can be formatted in different output formats.

use std::collections;
use std::fmt;

use anyhow::Context as _;

use crate::args;
use crate::Context;

/// A trait for objects that can be printed as nitrocli’s output.
pub trait Output {
  /// Formats this object using the given output format.
  fn format(&self, format: args::OutputFormat) -> anyhow::Result<String>;

  /// Prints this object to the output set in the given context using the output format set in the
  /// context configuration.
  ///
  /// The default implementation for this method prints the return value of `format` to
  /// `ctx.stdout`.
  fn print(&self, ctx: &mut Context<'_>) -> anyhow::Result<()> {
    println!(
      ctx,
      "{}",
      self
        .format(ctx.config.output_format.unwrap_or(args::OutputFormat::Text))?
        .trim_end()
    )
    .map_err(From::from)
  }
}

/// A single object.
pub struct Value<T: fmt::Display + serde::Serialize> {
  key: String,
  value: T,
}

/// A list of objects of the same type that is displayed as a table with a fallback message for an
/// empty list.
pub struct Table<T: TableItem> {
  key: String,
  items: Vec<T>,
  empty_message: String,
}

/// A trait for objects that can be displayed in a table.
pub trait TableItem: serde::Serialize {
  /// Returns the column headers for this type of table items.
  fn headers() -> Vec<&'static str>;
  /// Returns the values of the column for this table item.
  fn values(&self) -> Vec<String>;
}

/// A helper struct for building text reprensetations of objects.
pub struct TextObject {
  name: String,
  items: Vec<(usize, String, String)>,
}

impl<T: fmt::Display + serde::Serialize> Value<T> {
  pub fn new(key: impl Into<String>, value: T) -> Value<T> {
    Value {
      key: key.into(),
      value,
    }
  }
}

impl<T: fmt::Display + serde::Serialize> Output for Value<T> {
  fn format(&self, format: args::OutputFormat) -> anyhow::Result<String> {
    match format {
      args::OutputFormat::Json => get_json(&self.key, &self.value),
      args::OutputFormat::Text => Ok(self.value.to_string()),
    }
  }
}

impl<T: TableItem> Table<T> {
  pub fn new(key: impl Into<String>, empty_message: impl Into<String>) -> Table<T> {
    Table {
      key: key.into(),
      items: Vec::new(),
      empty_message: empty_message.into(),
    }
  }

  pub fn push(&mut self, item: T) {
    self.items.push(item);
  }

  pub fn append(&mut self, vec: &mut Vec<T>) {
    self.items.append(vec);
  }
}

impl<T: TableItem> Output for Table<T> {
  fn format(&self, format: args::OutputFormat) -> anyhow::Result<String> {
    match format {
      args::OutputFormat::Json => get_json(&self.key, &self.items),
      args::OutputFormat::Text => {
        if self.items.is_empty() {
          Ok(self.empty_message.clone())
        } else {
          let headers = T::headers().into_iter().map(ToOwned::to_owned).collect();
          let values = self.items.iter().map(TableItem::values);
          Ok(print_table(headers, values))
        }
      }
    }
  }
}

fn print_table<I>(headers: Vec<String>, iter: I) -> String
where
  I: Iterator<Item = Vec<String>>,
{
  let mut values = Vec::new();
  values.push(headers);
  values.extend(iter);
  let n = values.iter().map(Vec::len).min().unwrap_or_default();
  let lens: Vec<_> = (0..n)
    .map(|idx| {
      values
        .iter()
        .map(|v| v[idx].len())
        .max()
        .unwrap_or_default()
    })
    .collect();
  values
    .iter()
    .map(|v| print_table_line(&lens, &v))
    .collect::<Vec<_>>()
    .join("\n")
}

fn print_table_line(lens: &[usize], values: &[String]) -> String {
  lens
    .iter()
    .zip(values)
    .map(|(width, value)| format!("{:width$}", value, width = width))
    .collect::<Vec<_>>()
    .join("\t")
}

impl TextObject {
  pub fn new(name: impl Into<String>) -> TextObject {
    TextObject {
      name: name.into(),
      items: Vec::new(),
    }
  }

  pub fn push_line(&mut self, key: impl Into<String>, value: impl Into<String>) {
    self.items.push((1, key.into(), value.into()));
  }

  pub fn push_object(&mut self, o: TextObject) {
    self.push_line(o.name, "");
    for (indent, key, value) in o.items {
      self.items.push((1 + indent, key, value));
    }
  }
}

impl fmt::Display for TextObject {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    writeln!(f, "{}:", self.name)?;
    let max_len = self
      .items
      .iter()
      .map(|(indent, key, _)| indent * 2 + key.len())
      .max()
      .unwrap_or(0);
    for (indent, key, value) in &self.items {
      let prefix = " ".repeat(indent * 2);
      let padding = " ".repeat(max_len - key.len() - indent * 2);
      writeln!(f, "{}{}:{} {}", prefix, key, padding, value)?;
    }
    Ok(())
  }
}

fn get_json<T: serde::Serialize + ?Sized>(key: &str, value: &T) -> anyhow::Result<String> {
  let mut map = collections::HashMap::new();
  let _ = map.insert(key, value);
  serde_json::to_string_pretty(&map).context("Could not serialize output to JSON")
}