Skip to content

Commit

Permalink
remove type_name magic from grid formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Dec 28, 2023
1 parent 31d2638 commit 11e0fd7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
32 changes: 32 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -574,6 +600,9 @@ impl ArrayValue for Boxed {
fn proxy() -> Self {
Boxed(Array::<f64>::new(0, []).into())
}
fn empty_list_inner() -> &'static str {
"□"
}
}

impl ArrayValue for Complex {
Expand All @@ -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
Expand Down
79 changes: 31 additions & 48 deletions src/grid_fmt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Pretty printing Uiua arrays

use std::{
any::type_name,
f64::{
consts::{PI, TAU},
INFINITY,
Expand Down Expand Up @@ -156,34 +155,21 @@ impl<T: GridFmt + ArrayValue> GridFmt for Array<T> {
if self.shape.is_empty() {
return self.data[0].fmt_grid(boxed);
}
let stringy = type_name::<T>() == type_name::<char>();
let boxy = type_name::<T>() == type_name::<Boxed>();
let complexy = type_name::<T>() == type_name::<Complex>();
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
let mut metagrid = Metagrid::new();

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();
Expand Down Expand Up @@ -218,15 +204,21 @@ impl<T: GridFmt + ArrayValue> GridFmt for Array<T> {
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();
Expand Down Expand Up @@ -260,13 +252,7 @@ impl<T: GridFmt + ArrayValue> GridFmt for Array<T> {
}
}

fn fmt_array<T: GridFmt + ArrayValue>(
shape: &[usize],
data: &[T],
stringy: bool,
boxed: bool,
metagrid: &mut Metagrid,
) {
fn fmt_array<T: GridFmt + ArrayValue>(shape: &[usize], data: &[T], metagrid: &mut Metagrid) {
if data.is_empty() {
let mut shape_row = Vec::new();
for (i, dim) in shape.iter().enumerate() {
Expand All @@ -287,17 +273,14 @@ fn fmt_array<T: GridFmt + ArrayValue>(
}
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::<String>()
.chars()
.map(format_char_inner)
.collect();
row.push(vec![s.chars().collect()]);
} else {
for (i, val) in data.iter().enumerate() {
Expand All @@ -324,7 +307,7 @@ fn fmt_array<T: GridFmt + ArrayValue>(
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() {
Expand Down

0 comments on commit 11e0fd7

Please sign in to comment.