From: Simon Tatham Date: Thu, 28 Dec 2023 08:32:21 +0000 (+0000) Subject: Put x first in all coordinate pairs. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=3061583382d837e042f1185caea035adcf9d3a03;p=mastodonochrome.git Put x first in all coordinate pairs. This is the universal convention _almost_ everywhere. When writing actual curses programs I've sometimes reversed it, because curses's API puts line before column. But (a) ratatui is the sensible way round in the first place; (b) if my plan in this code is to localise the curses-type library as hard as possible into one module, then I wouldn't want to allow its conventions to leak into the rest of the code _anyway_. So, here we speak (x,y) like normal people, and since ratatui happens to be sensible, we do so _everywhere_; if for some reason we had to work with actual curses, we'd localise the swap to tui.rs. --- diff --git a/src/menu.rs b/src/menu.rs index 14a7f1b..ba6ce5c 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -17,7 +17,7 @@ pub fn main_menu() -> Box { } impl ActivityState for Menu { - fn draw(&self, h: usize, w: usize) + fn draw(&self, w: usize, h: usize) -> (Vec, CursorPosition) { let mut lines = Vec::new(); lines.extend_from_slice(&self.title.render(w)); diff --git a/src/tui.rs b/src/tui.rs index 5aed7bf..181613c 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -101,7 +101,7 @@ fn ratatui_style_from_colour(colour: char) -> Style { } } -fn ratatui_set_string(buf: &mut Buffer, y: usize, x: usize, +fn ratatui_set_string(buf: &mut Buffer, x: usize, y: usize, text: &ColouredStringSlice<'_>) { let mut x = x; if let Ok(y) = y.try_into() { @@ -231,8 +231,8 @@ impl Tui { self.terminal.draw(|frame| { let area = frame.size(); let buf = frame.buffer_mut(); - if let Some((y, x)) = self.state.draw_frame(area, buf) { - if let (Ok(y), Ok(x)) = (y.try_into(), x.try_into()) { + if let Some((x, y)) = self.state.draw_frame(area, buf) { + if let (Ok(x), Ok(y)) = (x.try_into(), y.try_into()) { frame.set_cursor(x, y); } } @@ -276,11 +276,11 @@ impl Tui { pub enum CursorPosition { None, // cursor is hidden End, // cursor at the end of the last drawn line (quite common in this UI) - At(usize, usize), // (y,x) + At(usize, usize), // (x,y) } pub trait ActivityState { - fn draw(&self, h: usize, w: usize) -> (Vec, CursorPosition); + fn draw(&self, w: usize, h: usize) -> (Vec, CursorPosition); fn handle_keypress(&mut self, key: OurKey) -> HandleEventResult; } @@ -310,19 +310,19 @@ impl TuiLogicalState { fn draw_frame(&self, area: Rect, buf: &mut Buffer) -> Option<(usize, usize)> { let (lines, cursorpos) = self.activity_state.draw( - area.height as usize, area.width as usize); + area.width as usize, area.height as usize); buf.reset(); let mut last_x = 0; let mut last_y = 0; for (y, line) in lines.iter().enumerate() { - ratatui_set_string(buf, y, 0, &line.slice()); + ratatui_set_string(buf, 0, y, &line.slice()); last_y = y; last_x = line.width(); } match cursorpos { CursorPosition::None => None, - CursorPosition::At(y, x) => Some((y, x)), - CursorPosition::End => Some((last_y, last_x)), + CursorPosition::At(x, y) => Some((x, y)), + CursorPosition::End => Some((last_x, last_y)), } }