项目作者: mehdishojaei

项目描述 :
A small and dependency-free crate for formatting the terminal outputs in a column-based grid style
高级语言: Rust
项目地址: git://github.com/mehdishojaei/cli-grid.git
创建时间: 2020-11-14T14:33:19Z
项目社区:https://github.com/mehdishojaei/cli-grid

开源协议:

下载


A small and dependency free crate for formatting the terminal outputs
in a column based grid style.

  1. [---------------------] [---------------------] [---------------------]
  2. [---------------------] [---------------------] [---------------------]
  3. [---------------------] [---------------------] [---------------------]
  4. <----------1----------> <-2->
  5. 1: Grid column
  6. 2: Grid padding

Each Cell of the grid can span into 1 or more columns.

  1. [ Cell with colspan 1 ] [---------------------] [---------------------]
  2. [-------------- Cell with colspan 2 --------------] [---------------------]
  3. [---------------------------- Cell with colspan 3 ----------------------------]

Horizontal alignments of cells are HAlign::Left, HAlign::Center, HAlign::Right and HAlign::Fill,

Vertical alignments of cells are VAlign::Top, VAlign::Middle and VAlign::Bottom.

Examples:

  1. use cli_grid::*;
  2. let grid = Grid::builder(vec![
  3. Row::new(vec![
  4. Cell::new("1".into(), 1),
  5. Cell::new("1".into(), 1),
  6. Cell::new("1".into(), 1),
  7. ]),
  8. Row::new(vec![
  9. Cell::new("2".into(), 2),
  10. Cell::new("1".into(), 1),
  11. ]),
  12. Row::new(vec![
  13. Cell::new("3".into(), 3),
  14. ]),
  15. ])
  16. .default_blank_char('.')
  17. .column_width(15)
  18. .build();
  19. let expected = format!(
  20. "{}\n{}\n{}\n",
  21. "1.............. 1.............. 1..............",
  22. "2.............................. 1..............",
  23. "3..............................................",
  24. );
  25. assert_eq!(grid.to_string(), expected);

Multiline Cells also is supported:

  1. use cli_grid::*;
  2. let grid = Grid::builder(vec![
  3. Row::new(vec![
  4. Cell::new("1".into(), 1),
  5. Cell::new("1\n1\n1".into(), 1),
  6. Cell::new("1".into(), 1),
  7. ]),
  8. Row::new(vec![
  9. Cell::new("2".into(), 2),
  10. Cell::new("1".into(), 1),
  11. ]),
  12. Row::new(vec![
  13. Cell::new("3".into(), 3),
  14. ]),
  15. ])
  16. .default_blank_char('.')
  17. .column_width(15)
  18. .build();
  19. let expected = format!(
  20. "{}\n{}\n{}\n{}\n{}\n",
  21. "1.............. 1.............. 1..............",
  22. "............... 1.............. ...............",
  23. "............... 1.............. ...............",
  24. "2.............................. 1..............",
  25. "3..............................................",
  26. );
  27. assert_eq!(grid.to_string(), expected);

So nested grids also is supported:

  1. use cli_grid::*;
  2. let nested_grid = Grid::builder(vec![
  3. Row::new(vec![
  4. Cell::new("1".into(), 1),
  5. Cell::new("1".into(), 1),
  6. ]),
  7. Row::new(vec![
  8. Cell::new("1".into(), 1),
  9. Cell::new("1".into(), 1),
  10. ]),
  11. Row::new(vec![
  12. Cell::new("1".into(), 1),
  13. Cell::new("1".into(), 1),
  14. ]),
  15. ])
  16. .default_h_align(HAlign::Center)
  17. .default_blank_char('-')
  18. .column_width(5)
  19. .build();
  20. let grid = Grid::builder(vec![
  21. Row::new(vec![
  22. Cell::new("2".into(), 2),
  23. Cell::new("1".into(), 1),
  24. ]),
  25. Row::new(vec![
  26. Cell::new("1".into(), 1),
  27. Cell::new(nested_grid.to_string(), 1),
  28. Cell::new("1".into(), 1),
  29. ]),
  30. Row::new(vec![
  31. Cell::new("3".into(), 3),
  32. ]),
  33. ])
  34. .default_h_align(HAlign::Center)
  35. .default_v_align(VAlign::Middle)
  36. .default_blank_char('.')
  37. .column_width(15)
  38. .build();
  39. let expected = format!(
  40. "{}\n{}\n{}\n{}\n{}\n",
  41. "...............2............... .......1.......",
  42. "............... ..--1-- --1--.. ...............",
  43. ".......1....... ..--1-- --1--.. .......1.......",
  44. "............... ..--1-- --1--.. ...............",
  45. ".......................3.......................",
  46. );
  47. assert_eq!(grid.to_string(), expected);

Empty cells and rows can be created by Cell::new_empty and Row::new_empty methods.

Filled cells and rows can be created by Cell::new_fill and Row::new_fill methods.