Skip to content

Commit

Permalink
Rename RichFormatter to TUIControl, rename its implementors, and rena…
Browse files Browse the repository at this point in the history
…me lineprinter::Line to LinePrinter.
  • Loading branch information
PaulJuliusMartinez committed Aug 31, 2021
1 parent a1dd70e commit 849ef97
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
53 changes: 26 additions & 27 deletions src/lineprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::fmt;
use std::fmt::Write;

use crate::flatjson::Value;
use crate::richformatter::{Color, RichFormatter};
use crate::truncate::truncate_right_to_fit;
use crate::truncate::TruncationResult::{DoesntFit, NoTruncation, Truncated};
use crate::tuicontrol::{Color, TUIControl};
use crate::viewer::Mode;

// # Printing out individual lines
Expand Down Expand Up @@ -186,9 +186,9 @@ impl<'a> LineValue<'a> {
}
}

pub struct Line<'a, F: RichFormatter> {
pub struct LinePrinter<'a, TUI: TUIControl> {
pub mode: Mode,
pub formatter: F, // RichFormatter
pub tui: TUI,

// Do I need these?
pub depth: usize,
Expand All @@ -206,13 +206,12 @@ pub struct Line<'a, F: RichFormatter> {
pub value: LineValue<'a>,
}

impl<'a, F: RichFormatter> Line<'a, F> {
impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
pub fn print_line<W: Write>(&self, buf: &mut W) -> fmt::Result {
self.print_focus_and_container_indicators(buf)?;

let label_depth = INDICATOR_WIDTH + self.depth * self.tab_size;
self.formatter
.position_cursor(buf, (1 + label_depth) as u16)?;
self.tui.position_cursor(buf, (1 + label_depth) as u16)?;

let mut available_space = self.width.saturating_sub(label_depth);

Expand Down Expand Up @@ -242,7 +241,7 @@ impl<'a, F: RichFormatter> Line<'a, F> {

fn print_focused_line_indicator<W: Write>(&self, buf: &mut W) -> fmt::Result {
if self.focused {
self.formatter.position_cursor(buf, 1)?;
self.tui.position_cursor(buf, 1)?;
write!(buf, "{}", FOCUSED_LINE)?;
}

Expand All @@ -262,7 +261,7 @@ impl<'a, F: RichFormatter> Line<'a, F> {
}

let container_indicator_col = 1 + self.depth * self.tab_size;
self.formatter
self.tui
.position_cursor(buf, container_indicator_col as u16)?;

match (self.focused, collapsed) {
Expand Down Expand Up @@ -346,10 +345,10 @@ impl<'a, F: RichFormatter> Line<'a, F> {
}

// Actually print out the label!
self.formatter.maybe_fg_color(buf, fg_label_color)?;
self.formatter.maybe_bg_color(buf, bg_label_color)?;
self.tui.maybe_fg_color(buf, fg_label_color)?;
self.tui.maybe_bg_color(buf, bg_label_color)?;
if label_bolded {
self.formatter.bold(buf)?;
self.tui.bold(buf)?;
}

write!(buf, "{}", label_style.left())?;
Expand All @@ -359,7 +358,7 @@ impl<'a, F: RichFormatter> Line<'a, F> {
}
write!(buf, "{}", label_style.right())?;

self.formatter.reset_style(buf)?;
self.tui.reset_style(buf)?;
write!(buf, ": ")?;

used_space += label_style.width();
Expand Down Expand Up @@ -416,7 +415,7 @@ impl<'a, F: RichFormatter> Line<'a, F> {
}

// Print out the value.
self.formatter.fg_color(buf, color)?;
self.tui.fg_color(buf, color)?;
if quoted {
used_space += 1;
buf.write_char('"')?;
Expand All @@ -431,7 +430,7 @@ impl<'a, F: RichFormatter> Line<'a, F> {
}

// Be a good citizen and reset the style.
self.formatter.reset_style(buf)?;
self.tui.reset_style(buf)?;

if self.trailing_comma {
used_space += 1;
Expand All @@ -442,23 +441,23 @@ impl<'a, F: RichFormatter> Line<'a, F> {
}

fn print_truncated_indicator<W: Write>(&self, buf: &mut W) -> fmt::Result {
self.formatter.position_cursor(buf, self.width as u16)?;
self.formatter.fg_color(buf, Color::LightBlack)?;
self.tui.position_cursor(buf, self.width as u16)?;
self.tui.fg_color(buf, Color::LightBlack)?;
write!(buf, ">")
}
}

#[cfg(test)]
mod tests {
use crate::richformatter::test::NoFormatting;
use crate::tuicontrol::test::EmptyControl;

use super::*;

impl<'a, F: Default + RichFormatter> Default for Line<'a, F> {
fn default() -> Line<'a, F> {
Line {
impl<'a, TUI: Default + TUIControl> Default for LinePrinter<'a, TUI> {
fn default() -> LinePrinter<'a, TUI> {
LinePrinter {
mode: Mode::Data,
formatter: F::default(),
tui: TUI::default(),
depth: 0,
width: 100,
tab_size: 2,
Expand All @@ -477,9 +476,9 @@ mod tests {
#[test]
#[ignore]
fn test_focus_indicators() {
let line: Line<'_, NoFormatting> = Line {
let line: LinePrinter<'_, EmptyControl> = LinePrinter {
mode: Mode::Line,
..Line::default()
..LinePrinter::default()
};
let mut buf = String::new();
line.print_line(&mut buf);
Expand All @@ -489,7 +488,7 @@ mod tests {

#[test]
fn test_fill_label_basic() -> std::fmt::Result {
let mut line: Line<'_, NoFormatting> = Line { ..Line::default() };
let mut line: LinePrinter<'_, EmptyControl> = LinePrinter::default();
line.label = Some(LineLabel::Key {
quoted: true,
key: "hello",
Expand Down Expand Up @@ -525,7 +524,7 @@ mod tests {

#[test]
fn test_fill_label_not_enough_space() -> std::fmt::Result {
let mut line: Line<'_, NoFormatting> = Line { ..Line::default() };
let mut line: LinePrinter<'_, EmptyControl> = LinePrinter::default();
line.label = Some(LineLabel::Key {
quoted: true,
key: "hello",
Expand Down Expand Up @@ -591,7 +590,7 @@ mod tests {

#[test]
fn test_fill_value_basic() -> std::fmt::Result {
let mut line: Line<'_, NoFormatting> = Line { ..Line::default() };
let mut line: LinePrinter<'_, EmptyControl> = LinePrinter::default();
let color = Color::Black;

line.value = LineValue::Value {
Expand Down Expand Up @@ -624,7 +623,7 @@ mod tests {

#[test]
fn test_fill_value_not_enough_space() -> std::fmt::Result {
let mut line: Line<'_, NoFormatting> = Line { ..Line::default() };
let mut line: LinePrinter<'_, EmptyControl> = LinePrinter::default();
let color = Color::Black;

// QUOTED VALUE
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ mod flatjson;
mod input;
mod jless;
mod lineprinter;
mod richformatter;
mod screenwriter;
mod truncate;
mod tuicontrol;
mod types;
mod viewer;

Expand Down
6 changes: 3 additions & 3 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use unicode_width::UnicodeWidthStr;
use crate::flatjson::{ContainerType, Index, OptionIndex, Row, Value};
use crate::jless::MAX_BUFFER_SIZE;
use crate::lineprinter as lp;
use crate::richformatter::{Color as TUIColor, ColorFormatter};
use crate::truncate::TruncationResult::{DoesntFit, NoTruncation, Truncated};
use crate::truncate::{truncate_left_to_fit, truncate_right_to_fit};
use crate::tuicontrol::{Color as TUIColor, ColorControl};
use crate::types::TTYDimensions;
use crate::viewer::{JsonViewer, Mode};

Expand Down Expand Up @@ -249,9 +249,9 @@ impl ScreenWriter {
}
}

let line = lp::Line {
let line = lp::LinePrinter {
mode: viewer.mode,
formatter: ColorFormatter {},
tui: ColorControl {},

depth,
width: self.dimensions.width as usize,
Expand Down
12 changes: 6 additions & 6 deletions src/richformatter.rs → src/tuicontrol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Color {
}
}

pub trait RichFormatter {
pub trait TUIControl {
fn position_cursor<W: Write>(&self, buf: &mut W, col: u16) -> Result;
fn fg_color<W: Write>(&self, buf: &mut W, color: Color) -> Result;
fn bg_color<W: Write>(&self, buf: &mut W, color: Color) -> Result;
Expand All @@ -67,9 +67,9 @@ pub trait RichFormatter {
}

#[derive(Default)]
pub struct ColorFormatter {}
pub struct ColorControl {}

impl RichFormatter for ColorFormatter {
impl TUIControl for ColorControl {
fn position_cursor<W: Write>(&self, buf: &mut W, col: u16) -> Result {
write!(buf, "\x1b[{}G", col)
}
Expand All @@ -96,9 +96,9 @@ pub mod test {
use super::*;

#[derive(Default)]
pub struct NoFormatting {}
pub struct EmptyControl {}

impl RichFormatter for NoFormatting {
impl TUIControl for EmptyControl {
fn position_cursor<W: Write>(&self, _buf: &mut W, _col: u16) -> Result {
Ok(())
}
Expand All @@ -123,7 +123,7 @@ pub mod test {
#[derive(Default)]
pub struct VisibleEscapes {}

impl RichFormatter for VisibleEscapes {
impl TUIControl for VisibleEscapes {
fn position_cursor<W: Write>(&self, buf: &mut W, col: u16) -> Result {
write!(buf, "_COL({})_", col)
}
Expand Down

0 comments on commit 849ef97

Please sign in to comment.