From c1907bb8ee215d534daf4188824293203b60dd9c Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 23 Jul 2022 22:24:28 +0200 Subject: [PATCH] Fix frame cursor functions ignoring stack --- src/buffer.rs | 10 +++++++++- src/frame.rs | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 359fef7..da77ebc 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -184,7 +184,7 @@ impl Buffer { } /// Ignores the stack. - pub(crate) fn at(&self, x: u16, y: u16) -> &Cell { + pub fn at(&self, x: u16, y: u16) -> &Cell { assert!(x < self.size.width); assert!(y < self.size.height); let i = self.index(x, y); @@ -208,6 +208,14 @@ impl Buffer { self.stack.pop(); } + pub fn local_to_global(&self, pos: Pos) -> Pos { + pos + self.stack.last().map(|(p, _)| *p).unwrap_or(Pos::ZERO) + } + + pub fn global_to_local(&self, pos: Pos) -> Pos { + pos - self.stack.last().map(|(p, _)| *p).unwrap_or(Pos::ZERO) + } + fn drawable_area(&self) -> (Pos, Size) { self.stack.last().copied().unwrap_or((Pos::ZERO, self.size)) } diff --git a/src/frame.rs b/src/frame.rs index 75fc091..5859e4f 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -44,11 +44,11 @@ impl Frame { } pub fn cursor(&self) -> Option { - self.cursor + self.cursor.map(|p| self.buffer.global_to_local(p)) } pub fn set_cursor(&mut self, pos: Option) { - self.cursor = pos; + self.cursor = pos.map(|p| self.buffer.local_to_global(p)); } pub fn show_cursor(&mut self, pos: Pos) {