Align columns: Difference between revisions
Content added Content deleted
(Added Wren) |
m (→{{header|Rust}}: Applied rustfmt, updated trim_right_matches to trim_end_matches) |
||
Line 6,798: | Line 6,798: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<lang rust>use std::iter::{ |
<lang rust>use std::iter::{repeat, Extend}; |
||
enum AlignmentType { |
enum AlignmentType { |
||
Left, |
|||
Center, |
|||
Right, |
|||
} |
|||
fn get_column_widths(text: &str) -> Vec<usize> { |
fn get_column_widths(text: &str) -> Vec<usize> { |
||
let mut widths = Vec::new(); |
let mut widths = Vec::new(); |
||
for line in text |
for line in text |
||
⚫ | |||
.map(|s| s.trim_matches(' ').trim_end_matches('$')) |
|||
{ |
|||
let lens = line.split('$').map(|s| s.chars().count()); |
let lens = line.split('$').map(|s| s.chars().count()); |
||
for |
for (idx, len) in lens.enumerate() { |
||
if idx < widths.len() { |
if idx < widths.len() { |
||
widths[idx] = std::cmp::max(widths[idx], len); |
widths[idx] = std::cmp::max(widths[idx], len); |
||
} |
} else { |
||
⚫ | |||
widths.push(len); |
widths.push(len); |
||
} |
} |
||
Line 6,821: | Line 6,827: | ||
let widths = get_column_widths(text); |
let widths = get_column_widths(text); |
||
let mut result = String::new(); |
let mut result = String::new(); |
||
for line in text |
for line in text |
||
.lines() |
|||
.map(|s| s.trim_matches(' ').trim_end_matches('$')) |
|||
{ |
|||
for (s, w) in line.split('$').zip(widths.iter()) { |
for (s, w) in line.split('$').zip(widths.iter()) { |
||
let blank_count = w - s.chars().count(); |
let blank_count = w - s.chars().count(); |