-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create printers to print detailed and grid views
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(); | ||
} | ||
} | ||
} |