| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use ratatui::layout::{Constraint, Layout, Rect}; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
pub const LOG_HEIGHT: u16 = 4; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
const MIN_HEIGHT_FOR_LOG: u16 = 12; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 27 |
pub struct ConsoleAreas { |
| 28 |
pub body: Rect, |
| 29 |
pub log: Rect, |
| 30 |
pub footer: Rect, |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
pub fn console(area: Rect) -> ConsoleAreas { |
| 35 |
console_with_log_height(area, LOG_HEIGHT) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
pub fn console_with_log_height(area: Rect, log_height: u16) -> ConsoleAreas { |
| 40 |
if area.height < MIN_HEIGHT_FOR_LOG || log_height == 0 { |
| 41 |
let [body, footer] = |
| 42 |
Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).areas(area); |
| 43 |
return ConsoleAreas { |
| 44 |
body, |
| 45 |
log: Rect { height: 0, ..body }, |
| 46 |
footer, |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
let [body, log, footer] = Layout::vertical([ |
| 51 |
Constraint::Min(0), |
| 52 |
Constraint::Length(log_height), |
| 53 |
Constraint::Length(1), |
| 54 |
]) |
| 55 |
.areas(area); |
| 56 |
|
| 57 |
ConsoleAreas { body, log, footer } |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub const GUTTER_WIDTH: u16 = 3; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
pub fn centered(area: Rect, width: u16, height: u16) -> Rect { |
| 80 |
let width = width.min(area.width); |
| 81 |
let height = height.min(area.height); |
| 82 |
Rect { |
| 83 |
x: area.x + (area.width - width) / 2, |
| 84 |
y: area.y + (area.height - height) / 2, |
| 85 |
width, |
| 86 |
height, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 92 |
pub struct PaneAreas { |
| 93 |
pub left: Rect, |
| 94 |
pub gutter: Rect, |
| 95 |
pub right: Rect, |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn panes(area: Rect) -> PaneAreas { |
| 106 |
|
| 107 |
|
| 108 |
const MIN_PANE_WIDTH: u16 = 16; |
| 109 |
|
| 110 |
if area.width < MIN_PANE_WIDTH * 2 + GUTTER_WIDTH { |
| 111 |
return PaneAreas { |
| 112 |
left: area, |
| 113 |
gutter: Rect { width: 0, ..area }, |
| 114 |
right: Rect { width: 0, ..area }, |
| 115 |
}; |
| 116 |
} |
| 117 |
|
| 118 |
let usable = area.width - GUTTER_WIDTH; |
| 119 |
let left_width = usable - usable / 2; |
| 120 |
|
| 121 |
PaneAreas { |
| 122 |
left: Rect { |
| 123 |
width: left_width, |
| 124 |
..area |
| 125 |
}, |
| 126 |
gutter: Rect { |
| 127 |
x: area.x + left_width, |
| 128 |
width: GUTTER_WIDTH, |
| 129 |
..area |
| 130 |
}, |
| 131 |
right: Rect { |
| 132 |
x: area.x + left_width + GUTTER_WIDTH, |
| 133 |
width: usable / 2, |
| 134 |
..area |
| 135 |
}, |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[cfg(test)] |
| 140 |
mod tests { |
| 141 |
use super::*; |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn panes_tile_the_area_exactly() { |
| 145 |
let areas = panes(Rect::new(0, 0, 80, 20)); |
| 146 |
assert_eq!(areas.left.x, 0); |
| 147 |
assert_eq!(areas.gutter.x, areas.left.x + areas.left.width); |
| 148 |
assert_eq!(areas.right.x, areas.gutter.x + areas.gutter.width); |
| 149 |
assert_eq!( |
| 150 |
areas.left.width + areas.gutter.width + areas.right.width, |
| 151 |
80, |
| 152 |
"no column is unaccounted for" |
| 153 |
); |
| 154 |
assert_eq!(areas.gutter.width, GUTTER_WIDTH); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#[test] |
| 160 |
fn odd_widths_give_the_extra_column_to_the_left() { |
| 161 |
let areas = panes(Rect::new(0, 0, 81, 20)); |
| 162 |
assert_eq!(areas.left.width, 39); |
| 163 |
assert_eq!(areas.right.width, 39); |
| 164 |
assert_eq!( |
| 165 |
areas.left.width + areas.gutter.width + areas.right.width, |
| 166 |
81 |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
#[test] |
| 172 |
fn narrow_area_collapses_to_a_single_pane() { |
| 173 |
let areas = panes(Rect::new(0, 0, 30, 20)); |
| 174 |
assert_eq!(areas.left.width, 30); |
| 175 |
assert_eq!(areas.right.width, 0); |
| 176 |
assert_eq!(areas.gutter.width, 0); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn full_height_gets_all_three_regions() { |
| 181 |
let areas = console(Rect::new(0, 0, 80, 24)); |
| 182 |
assert_eq!(areas.body.height, 24 - LOG_HEIGHT - 1); |
| 183 |
assert_eq!(areas.log.height, LOG_HEIGHT); |
| 184 |
assert_eq!(areas.footer.height, 1); |
| 185 |
assert_eq!( |
| 186 |
areas.body.height + areas.log.height + areas.footer.height, |
| 187 |
24, |
| 188 |
"the three regions must tile the screen exactly, with no dead row" |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
#[test] |
| 195 |
fn short_terminal_drops_the_log_pane() { |
| 196 |
let areas = console(Rect::new(0, 0, 80, 10)); |
| 197 |
assert_eq!( |
| 198 |
areas.log.height, 0, |
| 199 |
"log is dropped below the minimum height" |
| 200 |
); |
| 201 |
assert_eq!(areas.body.height, 9); |
| 202 |
assert_eq!(areas.footer.height, 1); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
#[test] |
| 208 |
fn single_row_terminal_yields_footer_only() { |
| 209 |
let areas = console(Rect::new(0, 0, 80, 1)); |
| 210 |
assert_eq!(areas.body.height, 0); |
| 211 |
assert_eq!(areas.log.height, 0); |
| 212 |
assert_eq!(areas.footer.height, 1); |
| 213 |
} |
| 214 |
} |
| 215 |
|