diff --git a/src/array.rs b/src/array.rs index 6526bdf1e..03851f1d6 100644 --- a/src/array.rs +++ b/src/array.rs @@ -501,10 +501,26 @@ pub trait ArrayValue: fn format_delims() -> (&'static str, &'static str) { ("[", "]") } + /// Marker for empty lists in grid formatting + fn empty_list_inner() -> &'static str { + "" + } /// Separator for formatting fn format_sep() -> &'static str { " " } + /// Delimiters for grid formatting + fn grid_fmt_delims(boxed: bool) -> (char, char) { + if boxed { + ('⟦', '⟧') + } else { + ('[', ']') + } + } + /// Whether to compress all items of a list when grid formatting + fn compress_list_grid() -> bool { + false + } } impl ArrayValue for f64 { @@ -560,6 +576,16 @@ impl ArrayValue for char { fn proxy() -> Self { ' ' } + fn grid_fmt_delims(boxed: bool) -> (char, char) { + if boxed { + ('⌜', '⌟') + } else { + ('"', '"') + } + } + fn compress_list_grid() -> bool { + true + } } impl ArrayValue for Boxed { @@ -574,6 +600,9 @@ impl ArrayValue for Boxed { fn proxy() -> Self { Boxed(Array::::new(0, []).into()) } + fn empty_list_inner() -> &'static str { + "□" + } } impl ArrayValue for Complex { @@ -590,6 +619,9 @@ impl ArrayValue for Complex { fn proxy() -> Self { Complex::new(0.0, 0.0) } + fn empty_list_inner() -> &'static str { + "ℂ" + } } /// Trait for comparing array elements diff --git a/src/grid_fmt.rs b/src/grid_fmt.rs index a262732fb..d193d5a1c 100644 --- a/src/grid_fmt.rs +++ b/src/grid_fmt.rs @@ -1,7 +1,6 @@ //! Pretty printing Uiua arrays use std::{ - any::type_name, f64::{ consts::{PI, TAU}, INFINITY, @@ -156,26 +155,13 @@ impl GridFmt for Array { if self.shape.is_empty() { return self.data[0].fmt_grid(boxed); } - let stringy = type_name::() == type_name::(); - let boxy = type_name::() == type_name::(); - let complexy = type_name::() == type_name::(); if *self.shape == [0] { - return if stringy { - if boxed { - vec![vec!['⌜', '⌟']] - } else { - vec![vec!['"', '"']] - } - } else { - let (left, right) = if boxed { ('⟦', '⟧') } else { ('[', ']') }; - if boxy { - vec![vec![left, '□', right]] - } else if complexy { - vec![vec![left, 'ℂ', right]] - } else { - vec![vec![left, right]] - } - }; + let (left, right) = T::grid_fmt_delims(boxed); + let inner = T::empty_list_inner(); + let mut row = vec![left]; + row.extend(inner.chars()); + row.push(right); + return vec![row]; } // Fill the metagrid @@ -183,7 +169,7 @@ impl GridFmt for Array { let mut grid: Grid = Grid::new(); - fmt_array(&self.shape, &self.data, stringy, boxed, &mut metagrid); + fmt_array(&self.shape, &self.data, &mut metagrid); // Determine max row heights and column widths let metagrid_width = metagrid.iter().map(|row| row.len()).max().unwrap(); let metagrid_height = metagrid.len(); @@ -218,15 +204,21 @@ impl GridFmt for Array { grid.extend(subrows); } // Outline the grid - let row_count = grid.len(); - if row_count == 1 && self.rank() == 1 { - // Add brackets to vectors - if !stringy { - let (left, right) = if boxed { ('⟦', '⟧') } else { ('[', ']') }; - grid[0].insert(0, left); - grid[0].push(right); - } + let grid_row_count = grid.len(); + if grid_row_count == 1 && self.rank() == 1 { + // Add brackets to lists + let (left, right) = T::grid_fmt_delims(boxed); + grid[0].insert(0, left); + grid[0].push(right); } else { + if T::compress_list_grid() { + // Add quotes arround char array rows + let (left, right) = T::grid_fmt_delims(false); + for row in grid.iter_mut() { + row.insert(0, left); + row.push(right); + } + } // Add corners to non-vectors let width = grid[0].len(); let height = grid.len(); @@ -260,13 +252,7 @@ impl GridFmt for Array { } } -fn fmt_array( - shape: &[usize], - data: &[T], - stringy: bool, - boxed: bool, - metagrid: &mut Metagrid, -) { +fn fmt_array(shape: &[usize], data: &[T], metagrid: &mut Metagrid) { if data.is_empty() { let mut shape_row = Vec::new(); for (i, dim) in shape.iter().enumerate() { @@ -287,17 +273,14 @@ fn fmt_array( } if rank == 1 { let mut row = Vec::with_capacity(shape[0]); - if stringy { - let mut s = String::new(); - s.extend(data.iter().map(|c| c.to_string())); - let mut s: String = s.chars().map(format_char_inner).collect(); - if boxed { - s.insert(0, '⌜'); - s.push('⌟'); - } else { - s.insert(0, '"'); - s.push('"'); - } + if T::compress_list_grid() { + let s: String = data + .iter() + .map(|c| c.to_string()) + .collect::() + .chars() + .map(format_char_inner) + .collect(); row.push(vec![s.chars().collect()]); } else { for (i, val) in data.iter().enumerate() { @@ -324,7 +307,7 @@ fn fmt_array( metagrid.push(vec![vec![vec![' ']]; metagrid.last().unwrap().len()]); } } - fmt_array(shape, cell, stringy, false, metagrid); + fmt_array(shape, cell, metagrid); if i * cell_size > 1000 { let mut elipses_row = Vec::new(); for prev_grid in metagrid.last().unwrap() {