|
1 | 1 | use ratatui::crossterm::terminal;
|
2 | 2 |
|
3 |
| -use crate::graph::{CellWidthType, Graph}; |
| 3 | +use crate::{ |
| 4 | + graph::{CellWidthType, Graph}, |
| 5 | + Result, |
| 6 | +}; |
4 | 7 |
|
5 | 8 | pub fn decide_cell_width_type(
|
6 | 9 | graph: &Graph,
|
7 | 10 | cell_width_type: Option<CellWidthType>,
|
8 |
| -) -> std::io::Result<CellWidthType> { |
| 11 | +) -> Result<CellWidthType> { |
9 | 12 | let (w, h) = terminal::size()?;
|
10 |
| - let cell_width_type = |
11 |
| - decide_cell_width_type_from(graph.max_pos_x, w as usize, h as usize, cell_width_type); |
12 |
| - Ok(cell_width_type) |
| 13 | + decide_cell_width_type_from(graph.max_pos_x, w as usize, h as usize, cell_width_type) |
13 | 14 | }
|
14 | 15 |
|
15 | 16 | fn decide_cell_width_type_from(
|
16 | 17 | max_pos_x: usize,
|
17 | 18 | term_width: usize,
|
18 | 19 | term_height: usize,
|
19 | 20 | cell_width_type: Option<CellWidthType>,
|
20 |
| -) -> CellWidthType { |
| 21 | +) -> Result<CellWidthType> { |
21 | 22 | let single_image_cell_width = max_pos_x + 1;
|
22 | 23 | let double_image_cell_width = single_image_cell_width * 2;
|
23 | 24 |
|
24 | 25 | match cell_width_type {
|
25 | 26 | Some(CellWidthType::Double) => {
|
26 | 27 | let required_width = double_image_cell_width + 2;
|
27 | 28 | if required_width > term_width {
|
28 |
| - panic!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = double)."); |
| 29 | + let msg = format!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = double)."); |
| 30 | + return Err(msg.into()); |
29 | 31 | }
|
30 |
| - CellWidthType::Double |
| 32 | + Ok(CellWidthType::Double) |
31 | 33 | }
|
32 | 34 | Some(CellWidthType::Single) => {
|
33 | 35 | let required_width = single_image_cell_width + 2;
|
34 | 36 | if required_width > term_width {
|
35 |
| - panic!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = single)."); |
| 37 | + let msg = format!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = single)."); |
| 38 | + return Err(msg.into()); |
36 | 39 | }
|
37 |
| - CellWidthType::Single |
| 40 | + Ok(CellWidthType::Single) |
38 | 41 | }
|
39 | 42 | None => {
|
40 | 43 | let double_required_width = double_image_cell_width + 2;
|
41 | 44 | if double_required_width <= term_width {
|
42 |
| - return CellWidthType::Double; |
| 45 | + return Ok(CellWidthType::Double); |
43 | 46 | }
|
44 | 47 | let single_required_width = single_image_cell_width + 2;
|
45 | 48 | if single_required_width <= term_width {
|
46 |
| - return CellWidthType::Single; |
| 49 | + return Ok(CellWidthType::Single); |
47 | 50 | }
|
48 |
| - panic!( |
49 |
| - "Terminal size {term_width}x{term_height} is too small. Required width is {single_required_width} (graph_width = single) or {double_required_width} (graph_width = double)." |
50 |
| - ); |
| 51 | + let msg = format!("Terminal size {term_width}x{term_height} is too small. Required width is {single_required_width} (graph_width = single) or {double_required_width} (graph_width = double)."); |
| 52 | + Err(msg.into()) |
51 | 53 | }
|
52 | 54 | }
|
53 | 55 | }
|
0 commit comments