mirror of
https://gitlab.com/arnekeller/ascii-table.git
synced 2024-12-04 21:49:08 +00:00
Fixed bugs.
This commit is contained in:
parent
cf67c877e1
commit
34befb3609
@ -15,12 +15,12 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ascii-table. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TableConfig {
|
||||
pub width: u32,
|
||||
pub columns: HashMap<usize, ColumnConfig>
|
||||
pub columns: BTreeMap<usize, ColumnConfig>
|
||||
}
|
||||
|
||||
impl Default for TableConfig {
|
||||
@ -28,7 +28,7 @@ impl Default for TableConfig {
|
||||
fn default() -> Self {
|
||||
TableConfig {
|
||||
width: 80,
|
||||
columns: HashMap::new()
|
||||
columns: BTreeMap::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
18
src/lib.rs
18
src/lib.rs
@ -120,10 +120,11 @@ fn square_data(mut data: Vec<Vec<String>>, num_cols: usize) -> Vec<Vec<String>>
|
||||
fn correct_config(conf: &TableConfig, num_cols: usize) -> TableConfig {
|
||||
let mut conf = conf.clone();
|
||||
for col in 0..num_cols {
|
||||
if conf.columns.get(&col).is_none() {
|
||||
conf.columns.insert(col, ColumnConfig::default());
|
||||
}
|
||||
if conf.columns.get(&col).is_none() {
|
||||
conf.columns.insert(col, ColumnConfig::default());
|
||||
}
|
||||
}
|
||||
conf.columns.split_off(&num_cols);
|
||||
conf
|
||||
}
|
||||
|
||||
@ -140,12 +141,11 @@ fn column_widths(data: &Vec<Vec<String>>, conf: &TableConfig) -> Vec<u32> {
|
||||
|
||||
fn trunc(mut widths: Vec<u32>, conf: &TableConfig) -> Vec<u32> {
|
||||
let max_width = conf.width;
|
||||
while widths.iter().sum::<u32>() + ((widths.len() as u32 - 1) * 3) + 4 > max_width {
|
||||
let idx;
|
||||
{
|
||||
let max = widths.iter().max().unwrap();
|
||||
idx = widths.iter().rposition(|x| x == max).unwrap();
|
||||
}
|
||||
let table_padding = ((widths.len() as u32 - 1) * 3) + 4;
|
||||
while widths.iter().sum::<u32>() + table_padding > max_width &&
|
||||
*widths.iter().max().unwrap() > 0 {
|
||||
let max = widths.iter().max().unwrap();
|
||||
let idx = widths.iter().rposition(|x| x == max).unwrap();
|
||||
widths[idx] = widths[idx] - 1;
|
||||
}
|
||||
widths
|
||||
|
15
src/test.rs
15
src/test.rs
@ -243,6 +243,21 @@ fn partial_head2() {
|
||||
assert_eq!(expected, format_table(input, &config));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore_unused_head() {
|
||||
let config = cube_config();
|
||||
let input = vec![&[1], &[2], &[3]];
|
||||
let expected = "┌───┐\n\
|
||||
│ a │\n\
|
||||
├───┤\n\
|
||||
│ 1 │\n\
|
||||
│ 2 │\n\
|
||||
│ 3 │\n\
|
||||
└───┘\n";
|
||||
|
||||
assert_eq!(expected, format_table(input, &config));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn align_right() {
|
||||
let mut config = TableConfig::default();
|
||||
|
Loading…
Reference in New Issue
Block a user