Skip to content

Commit

Permalink
Alt+Enter newline shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
goolord committed Aug 3, 2020
1 parent 1226a04 commit a7ac6ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
18 changes: 10 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,18 @@ impl App {
})
}

pub fn put_char(&mut self, c: char) {
let mut idx = self.data.input_cursor;
while !self.data.input.is_char_boundary(idx) {
idx += 1;
}
self.data.input.insert(idx, c);
self.data.input_cursor += 1;
}

pub fn on_key(&mut self, key: KeyCode) {
match key {
KeyCode::Char(c) => {
let mut idx = self.data.input_cursor;
while !self.data.input.is_char_boundary(idx) {
idx += 1;
}
self.data.input.insert(idx, c);
self.data.input_cursor += 1;
}
KeyCode::Char(c) => self.put_char(c),
KeyCode::Enter if !self.data.input.is_empty() => {
if let Some(idx) = self.data.channels.state.selected() {
self.send_input(idx)
Expand Down
35 changes: 21 additions & 14 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,25 @@ pub fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {

fn draw_chat<B: Backend>(f: &mut Frame<B>, app: &mut App, area: Rect) {
let text_width = area.width.saturating_sub(2) as usize;
let lines = app
.data
.input
.chars()
.enumerate()
.fold(Vec::new(), |mut lines, (idx, c)| {
if idx % text_width == 0 {
lines.push(String::new())
}
lines.last_mut().unwrap().push(c);
lines
});
let lines: Vec<String> =
app.data
.input
.chars()
.enumerate()
.fold(Vec::new(), |mut lines, (idx, c)| {
if idx % text_width == 0 {
lines.push(String::new());
}
match c {
'\r' => lines.push(String::new()),
_ => lines.last_mut().unwrap().push(c),
}
lines
});
let chars_since_newline = match lines.last() {
Some(last) => last.len(),
None => 0,
};
let num_input_lines = lines.len().max(1);
let input: Vec<Spans> = lines.into_iter().map(Spans::from).collect();
let extra_cursor_line = if app.data.input_cursor > 0 && app.data.input_cursor % text_width == 0
Expand All @@ -92,9 +99,9 @@ fn draw_chat<B: Backend>(f: &mut Frame<B>, app: &mut App, area: Rect) {
f.render_widget(input, chunks[1]);
f.set_cursor(
// Put cursor past the end of the input text
chunks[1].x + ((app.data.input_cursor as u16) % text_width as u16) + 1,
chunks[1].x + ((chars_since_newline as u16) % text_width as u16) + 1,
// Move one line down, from the border to the input line
chunks[1].y + (app.data.input_cursor as u16 / (text_width as u16)) + 1,
chunks[1].y + (chars_since_newline as u16 / (text_width as u16)) + num_input_lines as u16,
);
}

Expand Down

0 comments on commit a7ac6ad

Please sign in to comment.