Skip to content

Commit

Permalink
refactor: split display stuff into multiple modules
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Aug 26, 2024
1 parent bf79f2d commit 17d6c5e
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 262 deletions.
259 changes: 0 additions & 259 deletions src/display.rs

This file was deleted.

107 changes: 107 additions & 0 deletions src/harness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use std::io::Write;
use std::num::NonZeroUsize;
use std::path::Path;
use std::process::ExitStatus;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use crate::cli::CommandResult;
use crate::path_style::{DPath, PathStyle};

pub struct Config {
path_style: PathStyle,
multithreaded: bool,
line_prefix_width: usize,
no_header: bool,
result: CommandResult,

need_linesplit: AtomicBool,
}

impl Config {
pub const fn new(
path_style: PathStyle,
threads: NonZeroUsize,
line_prefix_width: usize,
no_header: bool,
result: CommandResult,
) -> Self {
Self {
path_style,
multithreaded: threads.get() > 1,
line_prefix_width,
no_header,
result,

need_linesplit: AtomicBool::new(false),
}
}

pub const fn create<'a>(&'a self, path: &'a Path) -> Harness<'a> {
Harness { config: self, path }
}
}

pub struct Harness<'a> {
config: &'a Config,
path: &'a Path,
}

impl<'a> Harness<'a> {
const fn path(&self) -> DPath<'a> {
self.config.path_style.path(self.path)
}

pub fn inherit_header(&self) {
if self.config.multithreaded || self.config.no_header {
return;
}
let need_linesplit = self.config.need_linesplit.swap(true, Ordering::Relaxed);
if need_linesplit {
println!();
}
println!("{}", self.path());
}

pub fn line_prefix(&self) -> String {
let width = self.config.line_prefix_width;
format!("{:width$} ", self.path())
}

pub fn collect(&self, stdout: &[u8], stderr: &[u8]) {
let any_output = !stdout.is_empty() || !stderr.is_empty();

let need_linesplit = self
.config
.need_linesplit
.swap(any_output, Ordering::Relaxed);
if need_linesplit || any_output {
println!();
}
if let Some(last) = stdout.last() {
if !self.config.no_header {
println!("{} Stdout:", self.path());
}
std::io::stdout().write_all(stdout).unwrap();
if *last != b'\n' {
std::io::stdout().write_all(b"\n").unwrap();
}
}
if let Some(last) = stderr.last() {
if !self.config.no_header {
eprintln!("{} Stderr:", self.path());
}
std::io::stderr().write_all(stderr).unwrap();
if *last != b'\n' {
std::io::stderr().write_all(b"\n").unwrap();
}
}
}

pub fn result(&self, took: Duration, status: ExitStatus) {
if self.config.result.print(status.success()) {
let took = crate::took::Took(took);
println!("took {took} {status} in {}", self.path());
}
}
}
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ mod byte_lines;
mod check_dir_is_project;
mod cli;
mod command;
mod display;
mod harness;
mod path_style;
mod shortened_path;
mod took;
mod walk;

fn main() {
Expand All @@ -27,7 +29,7 @@ fn main() {
matches.recursive,
);

let path_style = display::PathStyle::new(matches.path_style, matches.base_dir);
let path_style = path_style::PathStyle::new(matches.path_style, matches.base_dir);

if matches.command.is_empty() {
for path in rx {
Expand All @@ -38,7 +40,7 @@ fn main() {
}
}
} else {
let harness = display::HarnessConfig::new(
let harness = harness::Config::new(
path_style,
threads,
matches.line_prefix_width,
Expand Down
Loading

0 comments on commit 17d6c5e

Please sign in to comment.