chiark / gitweb /
Put x first in all coordinate pairs.
authorSimon Tatham <anakin@pobox.com>
Thu, 28 Dec 2023 08:32:21 +0000 (08:32 +0000)
committerSimon Tatham <anakin@pobox.com>
Thu, 28 Dec 2023 08:32:21 +0000 (08:32 +0000)
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.

src/menu.rs
src/tui.rs

index 14a7f1bd56b3310776b0897f88ddaa9dd381d822..ba6ce5ca86206072e53a4ea052d86a04d2063ca6 100644 (file)
@@ -17,7 +17,7 @@ pub fn main_menu() -> Box<dyn ActivityState> {
 }
 
 impl ActivityState for Menu {
-    fn draw(&self, h: usize, w: usize)
+    fn draw(&self, w: usize, h: usize)
             -> (Vec<ColouredString>, CursorPosition) {
         let mut lines = Vec::new();
         lines.extend_from_slice(&self.title.render(w));
index 5aed7bfc0c04cf6c9400e9568e0ed4d108799d27..181613ce19ffd3cd67e3a424f1f85ec285e13e09 100644 (file)
@@ -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<ColouredString>, CursorPosition);
+    fn draw(&self, w: usize, h: usize) -> (Vec<ColouredString>, 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)),
         }
     }