Skip to content

Commit

Permalink
Merge pull request #61 from kohbis/keep_buffer_as_vec
Browse files Browse the repository at this point in the history
Keep Buffer as Vec
  • Loading branch information
kohbis authored Jul 15, 2022
2 parents d95a7d8 + e385315 commit 957ef45
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/bin/rslack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ async fn main() {
console::print_as_table(&mut stdout, &chunked_data, max_col_size, &channel);

if message.trim().is_empty() {
let mut buffer: Vec<char> = Vec::new();
let mut buffer: Vec<Vec<char>> = vec![vec![]];
let mut cursor_line: usize = 0;

write!(
stdout,
Expand Down Expand Up @@ -143,28 +144,45 @@ async fn main() {
}
}
Key::Char('\n') => {
buffer.push('\r');
buffer.push('\n');
buffer[cursor_line].extend_from_slice(&['\r', '\n']);

// Add new line
buffer.push(vec![]);
cursor_line += 1;
}
Key::Char(c) => {
buffer.push(c);
buffer[cursor_line].push(c);
}
Key::Backspace => {
if buffer.len() > 0 {
buffer.remove(buffer.len() - 1);
if buffer[cursor_line].len() > 0 {
let remove_target = &buffer[cursor_line].len() - 1;
buffer[cursor_line].remove(remove_target);
write!(
stdout,
"{}{}",
termion::cursor::Left(1),
termion::clear::AfterCursor
)
.unwrap();
} else {
if buffer.len() > 0 {
buffer.remove(cursor_line);
cursor_line -= 1;

// Remove ['\r', '\n']
let line_len = &buffer[cursor_line].len() - 2;
buffer[cursor_line].truncate(line_len);
}
}
}
_ => {}
}

message = buffer.iter().collect();
message = buffer
.iter()
.map(|v| v.iter().collect::<String>())
.collect::<Vec<_>>()
.join("");
write!(
stdout,
"{}{}{}",
Expand Down

0 comments on commit 957ef45

Please sign in to comment.