Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement initial unicode movement and repaint
  • Loading branch information
sophiajt committed Mar 9, 2021
commit 6d9e04a0cc7bf9fbeec7a499da0329d47dbbd7f5
11 changes: 8 additions & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ X Fixed the scroll-off-the-bottom issue
X Initial commands for the engine
* Output commands from engine to the line editor
* UTF-8 support
* History
* Up/down history
* Home/end
X Line buffer
X Backspace/delete
* Navigation
X History
X Up/down history
X Home/end
* Refactor keypresses to flush at the end
* Ctrl-A, Ctrl-K, etc
* Under status
* Async/parallel prompt
* Validation
Expand Down
36 changes: 28 additions & 8 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ impl LineBuffer {
}

pub fn inc_insertion_point(&mut self) {
// let char_indices: Vec<_> = self.buffer.char_indices().collect();

// for i in 0..char_indices.len() {

// }
// for (idx, c) in char_indices {
// if idx == self.insertion_point
// }
let grapheme_indices = self.get_grapheme_indices();
for i in 0..grapheme_indices.len() {
if grapheme_indices[i].0 == self.insertion_point && i < (grapheme_indices.len() - 1) {
Expand Down Expand Up @@ -109,6 +101,34 @@ impl LineBuffer {
self.buffer.clear()
}

// pub fn get_grapheme_index_left(&self) -> usize {
// let grapheme_indices = self.get_grapheme_indices();

// let mut prev = 0;
// for (idx, _) in grapheme_indices {
// if idx >= self.insertion_point {
// return prev;
// }
// prev = idx;
// }

// prev
// }

// pub fn get_grapheme_index_right(&self) -> usize {
// let grapheme_indices = self.get_grapheme_indices();

// let mut next = self.buffer.len();
// for (idx, _) in grapheme_indices.iter().rev() {
// if *idx <= self.insertion_point {
// return next;
// }
// next = *idx;
// }

// next
// }

pub fn move_word_left(&mut self) -> usize {
let mut words = self.buffer[..self.insertion_point]
.split_word_bound_indices()
Expand Down
44 changes: 41 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::io::{stdout, Write};

use crossterm::{
cursor::{position, MoveLeft, MoveRight, MoveToColumn, MoveToNextLine},
cursor::{
position, MoveLeft, MoveRight, MoveToColumn, MoveToNextLine, RestorePosition, SavePosition,
},
event::read,
event::{Event, KeyCode, KeyEvent, KeyModifiers},
style::{Color, Print, ResetColor, SetForegroundColor},
Expand All @@ -28,6 +30,23 @@ fn print_message(stdout: &mut Stdout, msg: &str) -> Result<()> {
Ok(())
}

fn buffer_repaint(
stdout: &mut Stdout,
buffer: &LineBuffer,
prompt_offset: u16,
new_index: usize,
) -> Result<()> {
let raw_buffer = buffer.get_buffer();

stdout.queue(MoveToColumn(prompt_offset))?;
stdout.queue(Print(&raw_buffer[0..new_index]))?;
stdout.queue(SavePosition)?;
stdout.queue(Print(&raw_buffer[new_index..]))?;
stdout.queue(RestorePosition)?;

Ok(())
}

fn main() -> Result<()> {
let mut stdout = stdout();

Expand Down Expand Up @@ -109,6 +128,17 @@ fn main() -> Result<()> {
stdout.flush()?;
}
}
KeyCode::Home => {
buffer_repaint(&mut stdout, &buffer, prompt_offset, 0)?;
stdout.flush()?;
buffer.set_insertion_point(0);
}
KeyCode::End => {
let buffer_len = buffer.get_buffer_len();
buffer.set_insertion_point(buffer_len);
buffer_repaint(&mut stdout, &buffer, prompt_offset, buffer_len)?;
stdout.flush()?;
}
KeyCode::Enter => {
if buffer.get_buffer() == "exit" {
break 'repl;
Expand Down Expand Up @@ -215,8 +245,15 @@ fn main() -> Result<()> {
new_insertion_point as u16 + prompt_offset,
))?;
} else {
stdout.queue(MoveLeft(1))?;
// Start after the prompt
// Draw the string slice from 0 to the grapheme start left of insertion point
// Then, get the position on the screen
// Then draw the remainer of the buffer from above
// Finally, reset the cursor to the saved position
buffer.dec_insertion_point();
// let idx = buffer.get_grapheme_index_left();
let idx = buffer.get_insertion_point();
buffer_repaint(&mut stdout, &buffer, prompt_offset, idx)?;
}
stdout.flush()?;
}
Expand All @@ -229,8 +266,9 @@ fn main() -> Result<()> {
new_insertion_point as u16 + prompt_offset,
))?;
} else {
stdout.queue(MoveRight(1))?;
buffer.inc_insertion_point();
let idx = buffer.get_insertion_point();
buffer_repaint(&mut stdout, &buffer, prompt_offset, idx)?;
}
stdout.flush()?;
}
Expand Down