Skip to content

Commit

Permalink
implement G and gg for chat and history
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Jan 25, 2024
1 parent a3f911d commit 5a5ec5d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::notification::Notification;
use crate::spinner::Spinner;
use crate::{config::Config, formatter::Formatter};
use arboard::Clipboard;
use crossterm::event::KeyCode;
use ratatui::text::Line;

use std::sync::Arc;
Expand Down Expand Up @@ -36,6 +37,7 @@ pub struct App<'a> {
pub terminate_response_signal: Arc<AtomicBool>,
pub clipboard: Option<Clipboard>,
pub help: Help,
pub previous_key: KeyCode,
pub config: Arc<Config>,
pub formatter: &'a Formatter<'a>,
}
Expand All @@ -54,6 +56,7 @@ impl<'a> App<'a> {
terminate_response_signal: Arc::new(AtomicBool::new(false)),
clipboard: Clipboard::new().ok(),
help: Help::new(),
previous_key: KeyCode::Null,
config,
formatter,
}
Expand Down
8 changes: 6 additions & 2 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ impl Chat<'_> {
}

pub fn move_to_bottom(&mut self) {
// self.scroll = (self.formatted_chat.height() + self.answer.formatted_answer.height())
// .saturating_sub((self.area_height - 2).into());
self.scroll = (self.formatted_chat.height() + self.answer.formatted_answer.height())
.saturating_sub((self.area_height - 2).into()) as u16;
}

pub fn move_to_top(&mut self) {
self.scroll = 0;
}

pub fn render(&mut self, frame: &mut Frame, area: Rect, focused_block: &FocusedBlock) {
Expand Down
19 changes: 17 additions & 2 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,19 @@ pub fn handle_key_events(
},

// `gg`: Move to the top
//TODO:
KeyCode::Char('g') => {
if app.previous_key == KeyCode::Char('g') {
match app.focused_block {
FocusedBlock::Chat => {
app.chat.move_to_top();
}
FocusedBlock::History => {
app.history.move_to_top();
}
_ => (),
}
}
}

// New chat
KeyCode::Char(c)
Expand Down Expand Up @@ -265,8 +277,11 @@ pub fn handle_key_events(
}
}

app.prompt.handler(key_event, app.clipboard.as_mut());
app.prompt
.handler(key_event, app.previous_key, app.clipboard.as_mut());
}

app.previous_key = key_event.code;

Ok(())
}
25 changes: 13 additions & 12 deletions src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub enum Mode {

pub struct Prompt<'a> {
pub mode: Mode,
pub previous_key: KeyCode,
pub formatted_prompt: Text<'a>,
pub editor: TextArea<'a>,
pub block: Block<'a>,
Expand All @@ -41,7 +40,6 @@ impl Default for Prompt<'_> {

Self {
mode: Mode::Normal,
previous_key: KeyCode::Null,
formatted_prompt: Text::raw(""),
editor,
block,
Expand Down Expand Up @@ -91,7 +89,12 @@ impl Prompt<'_> {
});
}

pub fn handler(&mut self, key_event: KeyEvent, clipboard: Option<&mut Clipboard>) {
pub fn handler(
&mut self,
key_event: KeyEvent,
previous_key: KeyCode,
clipboard: Option<&mut Clipboard>,
) {
match self.mode {
Mode::Insert => match key_event.code {
KeyCode::Enter => {
Expand Down Expand Up @@ -149,7 +152,7 @@ impl Prompt<'_> {
self.editor.move_cursor(CursorMove::Forward);
}

KeyCode::Char('w') => match self.previous_key {
KeyCode::Char('w') => match previous_key {
KeyCode::Char('d') => {
self.editor.delete_next_word();
}
Expand All @@ -162,7 +165,7 @@ impl Prompt<'_> {
_ => self.editor.move_cursor(CursorMove::WordForward),
},

KeyCode::Char('b') => match self.previous_key {
KeyCode::Char('b') => match previous_key {
KeyCode::Char('d') => {
self.editor.delete_word();
}
Expand All @@ -175,7 +178,7 @@ impl Prompt<'_> {
_ => self.editor.move_cursor(CursorMove::WordBack),
},

KeyCode::Char('$') => match self.previous_key {
KeyCode::Char('$') => match previous_key {
KeyCode::Char('d') => {
self.editor.delete_line_by_end();
}
Expand All @@ -187,7 +190,7 @@ impl Prompt<'_> {
_ => self.editor.move_cursor(CursorMove::End),
},

KeyCode::Char('0') => match self.previous_key {
KeyCode::Char('0') => match previous_key {
KeyCode::Char('d') => {
self.editor.delete_line_by_head();
}
Expand All @@ -202,7 +205,7 @@ impl Prompt<'_> {
KeyCode::Char('G') => self.editor.move_cursor(CursorMove::Bottom),

KeyCode::Char('g') => {
if self.previous_key == KeyCode::Char('g') {
if previous_key == KeyCode::Char('g') {
self.editor.move_cursor(CursorMove::Jump(0, 0))
}
}
Expand All @@ -214,14 +217,14 @@ impl Prompt<'_> {
}

KeyCode::Char('d') => {
if self.previous_key == KeyCode::Char('d') {
if previous_key == KeyCode::Char('d') {
self.editor.move_cursor(CursorMove::Head);
self.editor.delete_line_by_end();
}
}

KeyCode::Char('c') => {
if self.previous_key == KeyCode::Char('c') {
if previous_key == KeyCode::Char('c') {
self.editor.move_cursor(CursorMove::Head);
self.editor.delete_line_by_end();
self.mode = Mode::Insert;
Expand Down Expand Up @@ -297,8 +300,6 @@ impl Prompt<'_> {
_ => {}
},
}

self.previous_key = key_event.code;
}

pub fn render(&mut self, frame: &mut Frame, block: Rect) {
Expand Down

0 comments on commit 5a5ec5d

Please sign in to comment.