Skip to content

Commit

Permalink
test: adjustments to ansi ending seq check
Browse files Browse the repository at this point in the history
  • Loading branch information
nidnogg committed Jan 30, 2025
1 parent e8416fe commit c5db770
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 97 deletions.
8 changes: 4 additions & 4 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// Truncate a string, ignoring ANSI graphics, but preserving trailing ANSI
/// graphics past the truncation point.
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
pub fn truncate(s: &str, n: usize) -> String {
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
#[derive(Debug, PartialEq, Eq)]
enum State {
Normal,
Expand Down Expand Up @@ -52,13 +52,13 @@ pub fn truncate(s: &str, n: usize) -> String {
}
}

// Ensure all ANSI sequences are properly closed
if !out.ends_with("\x1b[0m") {
if state == Ansi {
out.push_str("\x1b[0m");
}

out
}

#[cfg(test)]
mod test {
use rstest::rstest;
Expand Down
99 changes: 6 additions & 93 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::ansi;
pub struct Table {
rows: Vec<Row>,
format: TableFormat,
width: Option<usize>,
}

#[derive(Clone)]
Expand All @@ -15,36 +14,21 @@ pub struct Row {
#[derive(Clone)]
pub struct Cell {
content: String,
align: Alignment,
padding: (usize, usize),
}

#[derive(Clone)]
pub struct TableFormat {
column_separator: &'static str,
row_separator: &'static str,
padding: usize,
borders: Borders,
}

#[derive(Clone)]
pub struct Borders {
top: bool,
bottom: bool,
left: bool,
right: bool,
internal_h: bool,
internal_v: bool,
}

#[derive(Clone, Copy)]
pub enum Alignment {
Left,
Center,
Right,
}

// Constants using static str instead of String
pub mod format {
use super::*;

Expand All @@ -54,34 +38,10 @@ pub mod format {
pub static FORMAT_CLEAN: &TableFormat = &TableFormat {
column_separator: "",
row_separator: "",
padding: 1,
borders: Borders {
top: false,
bottom: false,
left: false,
right: false,
internal_h: false,
internal_v: false,
},
};

pub static FORMAT_BORDERS: &TableFormat = &TableFormat {
column_separator: "|",
row_separator: "-",
padding: 1,
borders: Borders {
top: true,
bottom: true,
left: true,
right: true,
internal_h: true,
internal_v: true,
},
borders: Borders { internal_h: false },
};
}
}

// Export the macro at the crate root
#[macro_export]
macro_rules! row {
($($x:expr),* $(,)?) => {
Expand All @@ -98,41 +58,30 @@ impl Table {
Table {
rows: Vec::new(),
format: format::consts::FORMAT_CLEAN.clone(),
width: None,
}
}

pub fn set_format(&mut self, format: &TableFormat) {
self.format = format.clone();
}

pub fn set_width(&mut self, width: Option<usize>) {
self.width = width;
}

pub fn add_row(&mut self, row: Row) {
self.rows.push(row);
}

pub fn print<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
let column_widths = self.get_column_widths();

// Print all rows with proper row separation
for (i, row) in self.rows.iter().enumerate() {
// Print the current row
self.print_row(writer, row, &column_widths)?;

// Add row separator if needed and not the last row
if self.format.borders.internal_h && i < self.rows.len() - 1 {
// Add row separator line here if needed
self.print_separator(writer, &column_widths)?;
}
}

Ok(())
}

// Helper method to print row separators if needed
fn print_separator<W: std::io::Write>(
&self,
writer: &mut W,
Expand Down Expand Up @@ -175,21 +124,6 @@ impl Table {
}
}

// Adjust for terminal width if set
if let Some(term_width) = self.width {
let total_width: usize = widths.iter().sum();
let separator_width = self.format.column_separator.len() * (widths.len() - 1);

let available_width = term_width.saturating_sub(separator_width);

if total_width > available_width {
let ratio = available_width as f64 / total_width as f64;
for width in &mut widths {
*width = (*width as f64 * ratio).floor() as usize;
}
}
}

widths
}

Expand Down Expand Up @@ -223,17 +157,12 @@ impl Table {
let column_width = *widths.get(cell_idx).unwrap_or(&0);
let available_space = column_width as i32 - padded_visible as i32;

let (align_left, align_right) = if available_space >= 0 {
match cell.align {
Alignment::Left => (0, available_space as usize),
Alignment::Right => (available_space as usize, 0),
Alignment::Center => {
let left = (available_space as usize) / 2;
(left, available_space as usize - left)
}
}
// Simple always-left alignment:
let align_left = 0;
let align_right = if available_space > 0 {
available_space as usize
} else {
(0, 0)
0
};

let mut line = String::new();
Expand Down Expand Up @@ -266,20 +195,9 @@ impl Cell {
pub fn new<S: Into<String>>(content: S) -> Self {
Cell {
content: content.into(),
align: Alignment::Left,
padding: (1, 1),
}
}

pub fn align(mut self, align: Alignment) -> Self {
self.align = align;
self
}

pub fn padding(mut self, left: usize, right: usize) -> Self {
self.padding = (left, right);
self
}
}

fn strip_ansi_width(s: &str) -> usize {
Expand All @@ -301,8 +219,3 @@ fn strip_ansi_width(s: &str) -> usize {
}
visible_len
}

// Terminal width detection
pub fn get_terminal_width() -> Option<usize> {
termsize::get().map(|size| size.cols as usize)
}

0 comments on commit c5db770

Please sign in to comment.