Skip to content

Commit 1d731ea

Browse files
author
Eric Douglass
committed
real scrolling and correct typing and arrow key behavior
1 parent cfc952d commit 1d731ea

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

src/display.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::convert::Infallible;
1+
use core::{convert::Infallible, cmp::Ordering};
22

33
use alloc::{
44
collections::VecDeque,
@@ -422,7 +422,24 @@ impl TextDisplay {
422422
// We add a correction if amount and COLUMNS are differ in even/odd parity
423423
let amount = amount % ROWS as isize;
424424
self.top = ((self.top as isize + amount) % ROWS as isize) as usize;
425-
425+
426+
let b = [' '; COLUMNS];
427+
let s = String::from_iter(b);
428+
429+
match amount.cmp(&0) {
430+
Ordering::Greater => {
431+
for i in ROWS-amount as usize..ROWS {
432+
self.write_text(i, 0, &s);
433+
}
434+
},
435+
Ordering::Less => {
436+
let amt = amount.unsigned_abs();
437+
for i in 0..amt {
438+
self.write_text(i, 0, &s);
439+
}
440+
},
441+
_ => {}
442+
}
426443
self.dirty_all();
427444
}
428445

src/terminal.rs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use crate::{
22
ansi::{self, EraseMode, Op, OpStr},
33
color::Rgb3,
4-
display::{self, TextDisplay, ROWS},
4+
display::{self, TextDisplay, ROWS, COLUMNS},
55
CHARACTER_DRAW_CYCLES,
66
};
77
use alloc::string::String;
88
use embedded_graphics::prelude::DrawTarget;
99
use esp32c3_hal::systimer::SystemTimer;
10-
use esp_println::println;
1110

1211
pub const IROWS: isize = display::ROWS as isize;
1312
pub const ICOLS: isize = display::COLUMNS as isize;
@@ -63,6 +62,18 @@ impl CursorPos {
6362
}
6463
}
6564

65+
enum VerticalLocation {
66+
Middle,
67+
Top,
68+
Bottom
69+
}
70+
71+
enum HorizontalLocation {
72+
Middle,
73+
Left,
74+
Right
75+
}
76+
6677
#[derive(Debug, Clone, Copy)]
6778
pub struct Cursor {
6879
pub pos: CursorPos,
@@ -91,8 +102,20 @@ impl Cursor {
91102
*self
92103
}
93104

94-
fn is_at_bottom(&self) -> bool {
95-
self.pos.row() == ROWS - 1
105+
fn location(&self) -> (VerticalLocation, HorizontalLocation) {
106+
const BOT: usize = ROWS - 1;
107+
const RIGHT: usize = COLUMNS - 1;
108+
let vert = match self.pos.row() {
109+
BOT => VerticalLocation::Bottom,
110+
0 => VerticalLocation::Top,
111+
_ => VerticalLocation::Middle,
112+
};
113+
let horz = match self.pos.col() {
114+
RIGHT => HorizontalLocation::Right,
115+
0 => HorizontalLocation::Left,
116+
_ => HorizontalLocation::Middle,
117+
};
118+
(vert, horz)
96119
}
97120

98121
fn set_highlight(&self, text: &mut TextDisplay) {
@@ -217,27 +240,43 @@ impl TextField {
217240
self.move_cursor(0, 1);
218241
}
219242
'\n' => {
220-
if self.cursor.is_at_bottom() {
221-
self.cursor.unset_highlight(&mut self.text);
222-
self.text.scroll_down(1);
223-
self.move_cursor(0, -(self.cursor.pos.col() as isize))
224-
} else {
225-
self.move_cursor(1, -(self.cursor.pos.col() as isize))
243+
match self.cursor.location() {
244+
(VerticalLocation::Bottom, _) => {
245+
self.cursor.unset_highlight(&mut self.text);
246+
self.text.scroll_down(1);
247+
self.move_cursor(0, -(self.cursor.pos.col() as isize))
248+
},
249+
_ => {
250+
self.move_cursor(1, -(self.cursor.pos.col() as isize))
251+
}
226252
}
227253
}
228254
'\r' => self.move_cursor(0, -(self.cursor.pos.col() as isize)),
229255
_ => {
230256
for c in t.escape_default() {
231257
self.text.write(self.cursor.pos.0, self.cursor.pos.1, c);
232-
self.move_cursor(0, 1);
258+
match self.cursor.location() {
259+
(_, HorizontalLocation::Left | HorizontalLocation::Middle) => {
260+
self.move_cursor(0, 1);
261+
},
262+
(VerticalLocation::Top | VerticalLocation::Middle, HorizontalLocation::Right) => {
263+
self.move_cursor(1, -(self.cursor.pos.col() as isize))
264+
},
265+
(VerticalLocation::Bottom, HorizontalLocation::Right) => {
266+
self.cursor.unset_highlight(&mut self.text);
267+
self.text.scroll_down(1);
268+
self.cursor.set_highlight(&mut self.text);
269+
self.move_cursor(0, -(self.cursor.pos.col() as isize));
270+
}
271+
}
233272
}
234273
}
235274
}
236275
}
237276

238277
fn handle_op(&mut self, op: Op) {
239278
use Op::*;
240-
println!("{:?}", op);
279+
// println!("{:?}", op);
241280
match op {
242281
MoveCursorAbs { x, y } => {
243282
self.move_cursor(
@@ -249,7 +288,11 @@ impl TextField {
249288
self.move_cursor(0, x as isize - self.cursor.pos.col() as isize);
250289
}
251290
MoveCursorDelta { dx, dy } => {
252-
self.move_cursor(dy, dx);
291+
// Constrain dx and dy so that the result added to the current position
292+
// stays within the window
293+
let x = (self.cursor.pos.col() as isize + dx).clamp(0, COLUMNS as isize - 1) - self.cursor.pos.col() as isize;
294+
let y = (self.cursor.pos.row() as isize + dy).clamp(0, ROWS as isize - 1) - self.cursor.pos.row() as isize;
295+
self.move_cursor(y, x);
253296
}
254297
MoveCursorBeginningAndLine { dy } => {
255298
self.move_cursor(dy, -(self.cursor.pos.col() as isize));

src/video.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn transmit_chunk() {
8181
spi::start_transmit(data);
8282
crate::perf::Measure::stop([start_xmit]);
8383

84-
timer::start_timer0_callback(1565, timer_callback);
84+
timer::start_timer0_callback(1500, timer_callback);
8585
crate::perf::Measure::stop([xmit_chunk]);
8686
crate::perf::Measure::flush([start_xmit, tx_wait, xmit_chunk]);
8787
}

0 commit comments

Comments
 (0)