Skip to content

Commit

Permalink
Implement C-x, announce 0.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Sep 1, 2018
1 parent b678c15 commit 385e6d1
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 50 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# WIP
# 2018/09/01: 0.4.3

- tiny now supports pasting multi-line strings. It runs `$EDITOR` to let you
edit the paste before sending. After closing the editor the final contents of
the file (excluding comment lines) are sent. Note: we currently don't support
commands in paste mode, so none of the lines can start with `/`.
- Ney key binding `C-x` implemented for editing current message in `$EDITOR`.
- Fixed a bug when pasing a string starting with a newline (#86).
- `auto_cmds` config field is gone and nick change and identification handling
is updated.
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiny"
version = "0.4.2"
version = "0.4.3"
authors = ["Ömer Sinan Ağacan <[email protected]>"]
repository = "https://github.com/osa1/tiny"
readme = "README.md"
Expand Down Expand Up @@ -33,7 +33,7 @@ serde_derive = "1.0.8"
serde_yaml = "0.7.1"
take_mut = "0.2.0"
tempfile = "3.0.3"
term_input = { path = "term_input", version = "0.1.4" }
term_input = { path = "term_input", version = "0.1.5" }
termbox_simple = { path = "termbox", version = "0.2.2" }
time = "0.1"

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ You can use `--config <path>` to specify your config file location.

- `alt-left/right` move tab to left/right

- `C-x` edit current message in `$EDITOR`

## Commands

Commands start with `/` character.
Expand Down
5 changes: 5 additions & 0 deletions src/tui/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ impl MessagingUI {
self.input_field.add_history(str)
}

/// Set input field contents.
pub fn set_input_field(&mut self, str: &str) {
self.input_field.set(str)
}

fn toggle_exit_dialogue(&mut self) {
let exit_dialogue = ::std::mem::replace(&mut self.exit_dialogue, None);
if exit_dialogue.is_none() {
Expand Down
105 changes: 60 additions & 45 deletions src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,64 +358,75 @@ impl TUI {
// translated to carriage returns when pasting so we check for
// both just to make sure
if str.contains('\n') || str.contains('\r') {
let tab = &mut self.tabs[self.active_idx].widget;
let tf = tab.flush_input_field();
match paste_lines(tf, &str) {
Ok(lines) => {
// Add the lines to text field history
for line in &lines {
tab.add_input_field_history(line);
}
TUIRet::Lines {
lines,
from: self.tabs[self.active_idx].src.clone(),
}
}
Err(err) => {
use std::env::VarError;
match err {
PasteError::Io(err) => {
self.add_client_err_msg(
&format!(
"Error while running $EDITOR: {:?}",
err),
&MsgTarget::CurrentTab);
}
PasteError::Var(VarError::NotPresent) => {
self.add_client_err_msg(
"Can't paste multi-line string: \
make sure your $EDITOR is set",
&MsgTarget::CurrentTab);
}
PasteError::Var(VarError::NotUnicode(_)) => {
self.add_client_err_msg(
"Can't paste multi-line string: \
can't parse $EDITOR (not unicode)",
&MsgTarget::CurrentTab);
}
PasteError::PastedCmd => {
self.add_client_err_msg(
"One of the pasted lines looks like a command.",
&MsgTarget::CurrentTab);
}
}
TUIRet::KeyHandled
}
}
self.edit_input(&str);
} else {
// TODO this may be too slow for pasting long single lines
for ch in str.chars() {
self.handle_input_event(Event::Key(Key::Char(ch)));
}
TUIRet::KeyHandled
}
TUIRet::KeyHandled
}

ev =>
TUIRet::EventIgnored(ev),
}
}

/// Edit current input + `str` before sending.
fn edit_input(&mut self, str: &str) -> TUIRet {
let tab = &mut self.tabs[self.active_idx].widget;
let tf = tab.flush_input_field();
match paste_lines(tf, &str) {
Ok(lines) => {
// If there's only one line just add it to the input field, do not send it
if lines.len() == 1 {
tab.set_input_field(&lines[0]);
TUIRet::KeyHandled
} else {
// Otherwise add the lines to text field history and send it
for line in &lines {
tab.add_input_field_history(line);
}
TUIRet::Lines {
lines,
from: self.tabs[self.active_idx].src.clone(),
}
}
}
Err(err) => {
use std::env::VarError;
match err {
PasteError::Io(err) => {
self.add_client_err_msg(
&format!(
"Error while running $EDITOR: {:?}",
err),
&MsgTarget::CurrentTab);
}
PasteError::Var(VarError::NotPresent) => {
self.add_client_err_msg(
"Can't paste multi-line string: \
make sure your $EDITOR is set",
&MsgTarget::CurrentTab);
}
PasteError::Var(VarError::NotUnicode(_)) => {
self.add_client_err_msg(
"Can't paste multi-line string: \
can't parse $EDITOR (not unicode)",
&MsgTarget::CurrentTab);
}
PasteError::PastedCmd => {
self.add_client_err_msg(
"One of the pasted lines looks like a command.",
&MsgTarget::CurrentTab);
}
}
TUIRet::KeyHandled
}
}
}

pub fn keypressed(&mut self, key: Key) -> TUIRet {
match self.tabs[self.active_idx].widget.keypressed(key) {
WidgetRet::KeyHandled =>
Expand Down Expand Up @@ -446,6 +457,10 @@ impl TUI {
TUIRet::KeyHandled
}

Key::Ctrl('x') => {
self.edit_input("")
}

Key::AltChar(c) =>
match c.to_digit(10) {
Some(i) => {
Expand Down
6 changes: 6 additions & 0 deletions src/tui/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ impl TextField {
self.history.push(str.chars().collect());
}

pub fn set(&mut self, str: &str) {
self.mode = Mode::Edit;
self.buffer = str.chars().collect();
self.move_cursor_to_end();
}

fn consume_word_before_curs(&mut self) {
// No modifications can happen if the scroll is at the beginning
if self.cursor == 0 {
Expand Down
2 changes: 1 addition & 1 deletion term_input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "term_input"
version = "0.1.4"
version = "0.1.5"
authors = ["Ömer Sinan Ağacan <[email protected]>"]
description = "Input handling for xterm-compatible terminals"
repository = "https://github.com/osa1/tiny"
Expand Down
3 changes: 2 additions & 1 deletion term_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static XTERM_KEY_SEQS : [(&'static [u8], Event); 27] =
];

// Make sure not to use 27 (ESC) because it's used as a prefix in many combinations.
static XTERM_SINGLE_BYTES : [(u8, Event); 12] =
static XTERM_SINGLE_BYTES : [(u8, Event); 13] =
[ (9, Event::Key(Key::Tab)),
(127, Event::Key(Key::Backspace)),
(1, Event::Key(Key::Ctrl('a'))),
Expand All @@ -157,6 +157,7 @@ static XTERM_SINGLE_BYTES : [(u8, Event); 12] =
(16, Event::Key(Key::Ctrl('p'))),
(14, Event::Key(Key::Ctrl('n'))),
(21, Event::Key(Key::Ctrl('u'))),
(24, Event::Key(Key::Ctrl('x'))),
];

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 385e6d1

Please sign in to comment.