use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use crate::command::Alignment;
#[must_use]
pub fn str_display_width(s: &str) -> usize {
UnicodeWidthStr::width(s)
}
#[must_use]
pub fn truncate_to_display_width(s: &str, max_width: usize) -> &str {
let mut current_width = 0;
for (byte_idx, ch) in s.char_indices() {
let ch_w = UnicodeWidthChar::width(ch).unwrap_or(0);
if current_width + ch_w > max_width {
return &s[..byte_idx];
}
current_width += ch_w;
}
s
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PaperWidth {
#[default]
Mm80,
Mm58,
Custom(usize),
}
impl PaperWidth {
#[must_use]
pub const fn columns(&self) -> usize {
match self {
Self::Mm80 => 48,
Self::Mm58 => 32,
Self::Custom(cols) => *cols,
}
}
#[must_use]
pub const fn dots(&self) -> u32 {
match self {
Self::Mm80 => 576,
Self::Mm58 => 384,
Self::Custom(cols) => (*cols as u32) * 12,
}
}
}
#[must_use]
pub fn format_two_column(left: &str, right: &str, total_width: usize) -> String {
let left_count = str_display_width(left);
let right_count = str_display_width(right);
if left_count + right_count < total_width {
let spaces = total_width - (left_count + right_count);
format!("{left}{}{right}", " ".repeat(spaces))
} else if right_count >= total_width {
truncate_to_display_width(right, total_width).to_string()
} else {
let max_left = total_width.saturating_sub(right_count + 1);
let left_truncated: String = if max_left > 1 {
let take_w = max_left - 1;
let mut s = truncate_to_display_width(left, take_w).to_string();
s.push('…');
s
} else {
truncate_to_display_width(left, max_left).to_string()
};
let spaces = total_width.saturating_sub(str_display_width(&left_truncated) + right_count);
format!("{left_truncated}{}{right}", " ".repeat(spaces))
}
}
#[must_use]
pub fn format_three_column(left: &str, center: &str, right: &str, total_width: usize) -> String {
let left_count = str_display_width(left);
let center_count = str_display_width(center);
let right_count = str_display_width(right);
let text_len = left_count + center_count + right_count;
if text_len + 2 <= total_width {
let remaining = total_width - text_len;
let left_spaces = remaining / 2;
let right_spaces = remaining - left_spaces;
format!(
"{left}{}{center}{}{right}",
" ".repeat(left_spaces),
" ".repeat(right_spaces)
)
} else {
format_two_column(left, right, total_width)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ColumnWidth {
Fixed(usize),
Fraction(f32),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TableColumn {
pub width: ColumnWidth,
pub alignment: Alignment,
}
impl TableColumn {
#[must_use]
pub const fn new(width: ColumnWidth, alignment: Alignment) -> Self {
Self { width, alignment }
}
#[must_use]
pub const fn fixed(width: usize, alignment: Alignment) -> Self {
Self::new(ColumnWidth::Fixed(width), alignment)
}
#[must_use]
pub const fn fraction(fraction: f32, alignment: Alignment) -> Self {
Self::new(ColumnWidth::Fraction(fraction), alignment)
}
#[must_use]
pub const fn left(width: ColumnWidth) -> Self {
Self::new(width, Alignment::Left)
}
#[must_use]
pub const fn right(width: ColumnWidth) -> Self {
Self::new(width, Alignment::Right)
}
#[must_use]
pub const fn center(width: ColumnWidth) -> Self {
Self::new(width, Alignment::Center)
}
}
#[must_use]
pub fn resolve_column_widths(columns: &[TableColumn], total_width: usize) -> Vec<usize> {
if columns.is_empty() || total_width == 0 {
return Vec::new();
}
let mut widths = vec![0; columns.len()];
let mut fixed_sum = 0;
let mut total_frac = 0.0f32;
for (i, col) in columns.iter().enumerate() {
match col.width {
ColumnWidth::Fixed(w) => {
widths[i] = w;
fixed_sum += w;
}
ColumnWidth::Fraction(f) => {
total_frac += f.max(0.0);
}
}
}
if fixed_sum >= total_width {
let scale = (total_width as f32) / (fixed_sum as f32);
let mut new_sum = 0;
for (i, col) in columns.iter().enumerate() {
if matches!(col.width, ColumnWidth::Fixed(_)) {
widths[i] = ((widths[i] as f32) * scale).floor() as usize;
new_sum += widths[i];
} else {
widths[i] = 0;
}
}
if new_sum < total_width {
let remainder = total_width - new_sum;
if let Some(max_idx) = widths
.iter()
.enumerate()
.max_by_key(|&(_, w)| w)
.map(|(i, _)| i)
{
widths[max_idx] += remainder;
}
}
return widths;
}
let remaining = total_width - fixed_sum;
let mut allocated_frac = 0;
if total_frac > 0.0 {
for (i, col) in columns.iter().enumerate() {
if let ColumnWidth::Fraction(f) = col.width {
let norm_f = f / total_frac;
let w = ((remaining as f32) * norm_f).round() as usize;
widths[i] = w;
allocated_frac += w;
}
}
}
let total_allocated = fixed_sum + allocated_frac;
if total_allocated < total_width {
let remainder = total_width - total_allocated;
if let Some(target_idx) = columns
.iter()
.enumerate()
.filter(|(_, c)| matches!(c.width, ColumnWidth::Fraction(_)))
.max_by_key(|(i, _)| widths[*i])
.map(|(i, _)| i)
.or_else(|| {
widths
.iter()
.enumerate()
.max_by_key(|&(_, w)| w)
.map(|(i, _)| i)
})
{
widths[target_idx] += remainder;
}
} else if total_allocated > total_width {
let excess = total_allocated - total_width;
if let Some(target_idx) = widths
.iter()
.enumerate()
.max_by_key(|&(_, w)| w)
.map(|(i, _)| i)
{
widths[target_idx] = widths[target_idx].saturating_sub(excess);
}
}
widths
}
#[must_use]
pub fn wrap_text_to_width(text: &str, max_width: usize) -> Vec<String> {
if max_width == 0 {
return vec![String::new()];
}
let trimmed = text.trim();
if trimmed.is_empty() {
return vec![String::new()];
}
let mut lines = Vec::new();
for raw_line in text.split('\n') {
let line_trimmed = raw_line.trim();
if line_trimmed.is_empty() {
lines.push(String::new());
continue;
}
let mut current_line = String::new();
let mut current_len = 0;
for word in line_trimmed.split_whitespace() {
let word_len = str_display_width(word);
if word_len > max_width {
if !current_line.is_empty() {
lines.push(std::mem::take(&mut current_line));
current_len = 0;
}
let mut remaining_word = word;
while !remaining_word.is_empty() {
let chunk = truncate_to_display_width(remaining_word, max_width);
let chunk_len = str_display_width(chunk);
if chunk_len == 0 {
break;
}
if chunk_len == max_width {
lines.push(chunk.to_string());
} else {
current_line = chunk.to_string();
current_len = chunk_len;
}
remaining_word = &remaining_word[chunk.len()..];
}
continue;
}
if current_line.is_empty() {
current_line.push_str(word);
current_len = word_len;
} else if current_len + 1 + word_len <= max_width {
current_line.push(' ');
current_line.push_str(word);
current_len += 1 + word_len;
} else {
lines.push(std::mem::take(&mut current_line));
current_line.push_str(word);
current_len = word_len;
}
}
if !current_line.is_empty() {
lines.push(current_line);
}
}
if lines.is_empty() {
lines.push(String::new());
}
lines
}
#[must_use]
pub fn format_table_row(
cells: &[&str],
columns: &[TableColumn],
total_width: usize,
) -> Vec<String> {
let widths = resolve_column_widths(columns, total_width);
if widths.is_empty() {
return Vec::new();
}
let mut col_lines: Vec<Vec<String>> = Vec::with_capacity(columns.len());
let mut max_height = 1;
for (i, &col_width) in widths.iter().enumerate() {
let cell_text = cells.get(i).copied().unwrap_or("");
let wrapped = wrap_text_to_width(cell_text, col_width);
max_height = max_height.max(wrapped.len());
col_lines.push(wrapped);
}
let mut result_lines = Vec::with_capacity(max_height);
for h in 0..max_height {
let mut line_str = String::with_capacity(total_width);
for (i, col) in columns.iter().enumerate() {
let width = widths[i];
let text = col_lines[i].get(h).map(String::as_str).unwrap_or("");
let padded = align_text(text, width, col.alignment);
line_str.push_str(&padded);
}
result_lines.push(line_str);
}
result_lines
}
#[must_use]
pub fn align_text(text: &str, width: usize, alignment: Alignment) -> String {
let count = str_display_width(text);
if count >= width {
return truncate_to_display_width(text, width).to_string();
}
let diff = width - count;
match alignment {
Alignment::Left => format!("{text}{}", " ".repeat(diff)),
Alignment::Right => format!("{}{text}", " ".repeat(diff)),
Alignment::Center => {
let left_pad = diff / 2;
let right_pad = diff - left_pad;
format!("{}{text}{}", " ".repeat(left_pad), " ".repeat(right_pad))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_two_column_standard() {
let row = format_two_column("Coffee", "$3.50", 20);
assert_eq!(row.chars().count(), 20);
assert_eq!(row, "Coffee $3.50");
}
#[test]
fn test_format_two_column_overflow() {
let row = format_two_column("Very Long Artisan Special Roast Coffee", "$3.50", 20);
assert_eq!(row.chars().count(), 20);
assert!(row.ends_with("$3.50"));
}
#[test]
fn test_align_text() {
assert_eq!(align_text("HI", 6, Alignment::Center), " HI ");
assert_eq!(align_text("HI", 6, Alignment::Left), "HI ");
assert_eq!(align_text("HI", 6, Alignment::Right), " HI");
}
#[test]
fn test_wrap_text_to_width() {
let text = "Double Truffle Wagyu Smash Burger";
let lines = wrap_text_to_width(text, 15);
assert_eq!(lines, vec!["Double Truffle", "Wagyu Smash", "Burger"]);
let long_word = "Supercalifragilistic";
let lines = wrap_text_to_width(long_word, 8);
assert_eq!(lines, vec!["Supercal", "ifragili", "stic"]);
}
#[test]
fn test_resolve_column_widths() {
let cols = [
TableColumn::fixed(4, Alignment::Left),
TableColumn::fraction(0.5, Alignment::Left),
TableColumn::fraction(0.25, Alignment::Right),
TableColumn::fraction(0.25, Alignment::Right),
];
let widths = resolve_column_widths(&cols, 48);
assert_eq!(widths.len(), 4);
assert_eq!(widths[0], 4);
let sum: usize = widths.iter().sum();
assert_eq!(sum, 48);
}
#[test]
fn test_format_table_row_multiline_sync() {
let cols = [
TableColumn::fixed(4, Alignment::Left),
TableColumn::fixed(16, Alignment::Left),
TableColumn::fixed(8, Alignment::Right),
];
let cells = ["2x", "Double Truffle Wagyu Burger", "$24.00"];
let lines = format_table_row(&cells, &cols, 28);
assert_eq!(lines.len(), 2);
assert_eq!(lines[0], "2x Double Truffle $24.00");
assert_eq!(lines[1], " Wagyu Burger ");
for line in &lines {
assert_eq!(str_display_width(line), 28);
}
}
#[test]
fn test_resolve_proportional_fractions_sum_under_one() {
let cols = [
TableColumn::fraction(0.3, Alignment::Left),
TableColumn::fraction(0.3, Alignment::Right),
];
let widths = resolve_column_widths(&cols, 48);
assert_eq!(widths, vec![24, 24]);
}
#[test]
fn test_wrap_text_with_explicit_newlines() {
let text = "Double Burger\n- Rare\n- Extra Pickles";
let lines = wrap_text_to_width(text, 20);
assert_eq!(lines, vec!["Double Burger", "- Rare", "- Extra Pickles"]);
}
#[test]
fn test_cjk_fullwidth_character_display_width() {
assert_eq!(str_display_width("ラーメン"), 8);
let aligned = align_text("牛丼", 8, Alignment::Left);
assert_eq!(str_display_width(&aligned), 8);
assert_eq!(aligned, "牛丼 ");
let row = format_two_column("1x 抹茶ラテ", "¥450", 20);
assert_eq!(str_display_width(&row), 20);
}
#[test]
fn test_arabic_text_display_width() {
assert_eq!(str_display_width("قهوة"), 4);
assert_eq!("شُكْرًا".chars().count(), 7);
assert_eq!(str_display_width("شُكْرًا"), 4);
let row = format_two_column("قهوة عربية", "1.500 KWD", 24);
assert_eq!(str_display_width(&row), 24);
let arabic_menu = "شاورما دجاج مع ثوم ومخلل وبطاطا";
let wrapped = wrap_text_to_width(arabic_menu, 16);
for line in &wrapped {
assert!(str_display_width(line) <= 16);
}
}
}