Skip to content

Commit

Permalink
Create printers to print detailed and grid views
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Jul 9, 2023
1 parent bb83220 commit b79eb95
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
mod cell;
mod grid;
mod table;

pub use cell::Cell;
pub use grid::Grid;
pub use table::Table;
39 changes: 39 additions & 0 deletions src/output/grid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::config::{Args, Conf};
use crate::enums::DetailField;
use crate::fmt::render;
use std::collections::HashMap;

/// The grid view renders the node names in a two dimensional layout to minimise
/// scrolling. It does not support rendering of node metadata.
///
/// The grid view is one of two views supported by `pls`, the other being the
/// [detailed view](crate::output::Table).
///
/// The grid view tries to render all elements in as few lines as possible. Once
/// the number of lines has been minimised, it minimises the column count by
/// making each column take the maximum number of rows.
pub struct Grid {
pub entries: Vec<String>,
}

impl Grid {
/// Create a new instance of `Grid`, taking ownership of the given entries.
pub fn new(entries: Vec<HashMap<DetailField, String>>) -> Self {
Self {
entries: entries
.into_iter()
.map(|mut entry| entry.remove(&DetailField::Name).unwrap_or_default())
.collect(),
}
}

/// Render the grid to STDOUT.
pub fn render(&self, _conf: &Conf, _args: &Args) {
for entry in &self.entries {
print!(
"{} ",
render(entry.get(&DetailField::Name).unwrap_or(&String::default()))
);
}
}
}
31 changes: 31 additions & 0 deletions src/output/table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::config::{Args, Conf};
use crate::enums::DetailField;
use crate::fmt::render;
use std::collections::HashMap;

/// The detailed renders node names, and optionally, chosen node metadata in
/// a tabular layout with one row per node.
///
/// The detailed view is one of two views supported by `pls`, the other being
/// the [grid view](crate::output::Grid).
#[derive(Default)]
pub struct Table {
pub entries: Vec<HashMap<DetailField, String>>,
}

impl Table {
/// Create a new instance of `Table`, taking ownership of the given entries.
pub fn new(entries: Vec<HashMap<DetailField, String>>) -> Self {
Self { entries }
}

/// Render the table to STDOUT.
pub fn render(&self, _conf: &Conf, args: &Args) {
for entry in &self.entries {
for det in &args.details {
print!("{} ", render(entry.get(det).unwrap_or(&String::default())));
}
println!();
}
}
}

0 comments on commit b79eb95

Please sign in to comment.