Scroll on text input

This commit is contained in:
Jeremy Soller 2023-12-20 15:01:47 -07:00
parent f9f7904117
commit 7f181d0e64
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
3 changed files with 27 additions and 22 deletions

View file

@ -152,7 +152,7 @@ impl cosmic::Application for App {
let terminal = terminal.lock().unwrap(); let terminal = terminal.lock().unwrap();
let rgb = terminal.colors()[index].unwrap_or_default(); let rgb = terminal.colors()[index].unwrap_or_default();
let text = f(rgb); let text = f(rgb);
terminal.input(text.into_bytes()); terminal.input_no_scroll(text.into_bytes());
} }
} }
TermEvent::Exit => { TermEvent::Exit => {
@ -161,7 +161,7 @@ impl cosmic::Application for App {
TermEvent::PtyWrite(text) => { TermEvent::PtyWrite(text) => {
if let Some(terminal) = self.tab_model.data::<Mutex<Terminal>>(entity) { if let Some(terminal) = self.tab_model.data::<Mutex<Terminal>>(entity) {
let terminal = terminal.lock().unwrap(); let terminal = terminal.lock().unwrap();
terminal.input(text.into_bytes()); terminal.input_no_scroll(text.into_bytes());
} }
} }
TermEvent::ResetTitle => { TermEvent::ResetTitle => {
@ -172,7 +172,7 @@ impl cosmic::Application for App {
if let Some(terminal) = self.tab_model.data::<Mutex<Terminal>>(entity) { if let Some(terminal) = self.tab_model.data::<Mutex<Terminal>>(entity) {
let terminal = terminal.lock().unwrap(); let terminal = terminal.lock().unwrap();
let text = f(terminal.size().into()); let text = f(terminal.size().into());
terminal.input(text.into_bytes()); terminal.input_no_scroll(text.into_bytes());
} }
} }
TermEvent::Title(title) => { TermEvent::Title(title) => {

View file

@ -290,10 +290,15 @@ impl Terminal {
self.with_buffer_mut(|buffer| buffer.set_redraw(redraw)); self.with_buffer_mut(|buffer| buffer.set_redraw(redraw));
} }
pub fn input<I: Into<Cow<'static, [u8]>>>(&self, input: I) { pub fn input_no_scroll<I: Into<Cow<'static, [u8]>>>(&self, input: I) {
self.notifier.notify(input); self.notifier.notify(input);
} }
pub fn input_scroll<I: Into<Cow<'static, [u8]>>>(&self, input: I) {
self.input_no_scroll(input);
self.scroll(TerminalScroll::Bottom);
}
pub fn resize(&mut self, width: u32, height: u32, scale_factor: f32) { pub fn resize(&mut self, width: u32, height: u32, scale_factor: f32) {
//TODO: check scale factor //TODO: check scale factor
if width != self.size.width || height != self.size.height { if width != self.size.width || height != self.size.height {

View file

@ -319,46 +319,46 @@ where
modifiers, modifiers,
}) => match key_code { }) => match key_code {
KeyCode::Backspace => { KeyCode::Backspace => {
terminal.input(b"\x08".as_slice()); terminal.input_scroll(b"\x08".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Tab => { KeyCode::Tab => {
if modifiers.shift() { if modifiers.shift() {
terminal.input(b"\x1B[Z".as_slice()); terminal.input_scroll(b"\x1B[Z".as_slice());
} else { } else {
terminal.input(b"\t".as_slice()); terminal.input_scroll(b"\t".as_slice());
} }
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Enter => { KeyCode::Enter => {
terminal.input(b"\n".as_slice()); terminal.input_scroll(b"\n".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Escape => { KeyCode::Escape => {
terminal.input(b"\x1B".as_slice()); terminal.input_scroll(b"\x1B".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Up => { KeyCode::Up => {
terminal.input(b"\x1B[A".as_slice()); terminal.input_scroll(b"\x1B[A".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Down => { KeyCode::Down => {
terminal.input(b"\x1B[B".as_slice()); terminal.input_scroll(b"\x1B[B".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Right => { KeyCode::Right => {
terminal.input(b"\x1B[C".as_slice()); terminal.input_scroll(b"\x1B[C".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Left => { KeyCode::Left => {
terminal.input(b"\x1B[D".as_slice()); terminal.input_scroll(b"\x1B[D".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::End => { KeyCode::End => {
if modifiers.shift() { if modifiers.shift() {
terminal.scroll(TerminalScroll::Bottom); terminal.scroll(TerminalScroll::Bottom);
} else { } else {
terminal.input(b"\x1B[F".as_slice()); terminal.input_scroll(b"\x1B[F".as_slice());
} }
status = Status::Captured; status = Status::Captured;
} }
@ -366,23 +366,23 @@ where
if modifiers.shift() { if modifiers.shift() {
terminal.scroll(TerminalScroll::Top); terminal.scroll(TerminalScroll::Top);
} else { } else {
terminal.input(b"\x1B[H".as_slice()); terminal.input_scroll(b"\x1B[H".as_slice());
} }
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Insert => { KeyCode::Insert => {
terminal.input(b"\x1B[2~".as_slice()); terminal.input_scroll(b"\x1B[2~".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::Delete => { KeyCode::Delete => {
terminal.input(b"\x1B[3~".as_slice()); terminal.input_scroll(b"\x1B[3~".as_slice());
status = Status::Captured; status = Status::Captured;
} }
KeyCode::PageUp => { KeyCode::PageUp => {
if modifiers.shift() { if modifiers.shift() {
terminal.scroll(TerminalScroll::PageUp); terminal.scroll(TerminalScroll::PageUp);
} else { } else {
terminal.input(b"\x1B[5~".as_slice()); terminal.input_scroll(b"\x1B[5~".as_slice());
} }
status = Status::Captured; status = Status::Captured;
} }
@ -390,7 +390,7 @@ where
if modifiers.shift() { if modifiers.shift() {
terminal.scroll(TerminalScroll::PageDown); terminal.scroll(TerminalScroll::PageDown);
} else { } else {
terminal.input(b"\x1B[6~".as_slice()); terminal.input_scroll(b"\x1B[6~".as_slice());
} }
status = Status::Captured; status = Status::Captured;
} }
@ -414,7 +414,7 @@ where
if character.is_control() { if character.is_control() {
let mut buf = [0, 0, 0, 0]; let mut buf = [0, 0, 0, 0];
let str = character.encode_utf8(&mut buf); let str = character.encode_utf8(&mut buf);
terminal.input(str.as_bytes().to_vec()); terminal.input_scroll(str.as_bytes().to_vec());
} }
} }
(false, false, true) => { (false, false, true) => {
@ -422,7 +422,7 @@ where
// Handle alt for non-control characters // Handle alt for non-control characters
let mut buf = [0x1B, 0, 0, 0, 0]; let mut buf = [0x1B, 0, 0, 0, 0];
let str = character.encode_utf8(&mut buf[1..]); let str = character.encode_utf8(&mut buf[1..]);
terminal.input(str.as_bytes().to_vec()); terminal.input_scroll(str.as_bytes().to_vec());
} }
} }
(false, false, false) => { (false, false, false) => {
@ -430,7 +430,7 @@ where
if !character.is_control() { if !character.is_control() {
let mut buf = [0, 0, 0, 0]; let mut buf = [0, 0, 0, 0];
let str = character.encode_utf8(&mut buf); let str = character.encode_utf8(&mut buf);
terminal.input(str.as_bytes().to_vec()); terminal.input_scroll(str.as_bytes().to_vec());
} }
} }
} }