Skip to content

Commit

Permalink
ReplxxImpl: Allow **silently** aborting user input.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmokHuginnsson committed Nov 18, 2021
1 parent ba84204 commit 0fa68ff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/replxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ enum { REPLXX_KEY_PASTE_FINISH = REPLXX_KEY_PASTE_START + 1 };
enum { REPLXX_KEY_BACKSPACE = REPLXX_KEY_CONTROL( 'H' ) };
enum { REPLXX_KEY_TAB = REPLXX_KEY_CONTROL( 'I' ) };
enum { REPLXX_KEY_ENTER = REPLXX_KEY_CONTROL( 'M' ) };
enum { REPLXX_KEY_ABORT = REPLXX_KEY_META( REPLXX_KEY_CONTROL( 'M' ) ) };

/*! \brief List of built-in actions that act upon user input.
*/
Expand Down
6 changes: 6 additions & 0 deletions include/replxx.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ enum { ERROR_BB1CA97EC761FC37101737BA0AA2E7C5 = ERROR };
#undef ERROR
enum { ERROR = ERROR_BB1CA97EC761FC37101737BA0AA2E7C5 };
#endif
#ifdef ABORT
enum { ABORT_8D12A2CA7E5A64036D7251A3EDA51A38 = ABORT };
#undef ABORT
enum { ABORT = ABORT_8D12A2CA7E5A64036D7251A3EDA51A38 };
#endif
#ifdef DELETE
enum { DELETE_32F68A60CEF40FAEDBC6AF20298C1A1E = DELETE };
#undef DELETE
Expand Down Expand Up @@ -144,6 +149,7 @@ public:
static char32_t const BACKSPACE = 'H' | BASE_CONTROL;
static char32_t const TAB = 'I' | BASE_CONTROL;
static char32_t const ENTER = 'M' | BASE_CONTROL;
static char32_t const ABORT = 'C' | BASE_CONTROL | BASE_META;
};
/*! \brief List of built-in actions that act upon user input.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/replxx_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ Replxx::ReplxxImpl::ReplxxImpl( FILE*, FILE*, FILE* )
bind_key( Replxx::KEY::meta( 'U' ), _namedActions.at( action_names::UPPERCASE_SUBWORD ) );
bind_key( Replxx::KEY::control( 'T' ), _namedActions.at( action_names::TRANSPOSE_CHARACTERS ) );
bind_key( Replxx::KEY::control( 'C' ), _namedActions.at( action_names::ABORT_LINE ) );
bind_key( Replxx::KEY::ABORT, _namedActions.at( action_names::ABORT_LINE ) );
bind_key( Replxx::KEY::control( 'D' ), _namedActions.at( action_names::SEND_EOF ) );
bind_key( Replxx::KEY::INSERT + 0, _namedActions.at( action_names::TOGGLE_OVERWRITE_MODE ) );
bind_key( 127, _namedActions.at( action_names::DELETE_CHARACTER_UNDER_CURSOR ) );
Expand Down Expand Up @@ -1772,15 +1773,17 @@ Replxx::ACTION_RESULT Replxx::ReplxxImpl::transpose_characters( char32_t ) {
}

// ctrl-C, abort this line
Replxx::ACTION_RESULT Replxx::ReplxxImpl::abort_line( char32_t ) {
Replxx::ACTION_RESULT Replxx::ReplxxImpl::abort_line( char32_t keyCode_ ) {
errno = EAGAIN;
_history.drop_last();
// we need one last refresh with the cursor at the end of the line
// so we don't display the next prompt over the previous input line
_pos = _data.length(); // pass _data.length() as _pos for EOL
_lastRefreshTime = 0;
refresh_line( _refreshSkipped ? HINT_ACTION::REGENERATE : HINT_ACTION::TRIM );
_terminal.write8( "^C\r\n", 4 );
if ( keyCode_ == Replxx::KEY::control( 'C' ) ) {
_terminal.write8( "^C\r\n", 4 );
}
return ( Replxx::ACTION_RESULT::BAIL );
}

Expand Down

0 comments on commit 0fa68ff

Please sign in to comment.