From 013ed70ef3781fdd486b7a51b15cfc7d8ee242bc Mon Sep 17 00:00:00 2001 From: Kristofer Berggren Date: Sun, 17 Apr 2022 19:13:03 +0800 Subject: [PATCH] add support for deleting line before cursor with ctrl-u --- CMakeLists.txt | 2 +- README.md | 5 ++++- lib/ncutil/src/strutil.cpp | 14 ++++++++++++++ lib/ncutil/src/strutil.h | 1 + src/nchat.1 | 2 +- src/uikeyconfig.cpp | 7 ++++--- src/uilistdialog.cpp | 4 ++-- src/uimodel.cpp | 13 +++++++++---- 8 files changed, 36 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 491fcc0d..01a7ecde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ # Project cmake_minimum_required(VERSION 3.16 FATAL_ERROR) # 3.1 is ok, but is 3.16 needed for proper version string -project(nchat VERSION 2.50 LANGUAGES CXX) +project(nchat VERSION 2.51 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) include(CheckCXXSourceCompiles) set(NCHAT_PROJECT_VERSION ${PROJECT_VERSION}) diff --git a/README.md b/README.md index e083a784..971ed65e 100644 --- a/README.md +++ b/README.md @@ -405,8 +405,11 @@ Specifies whether to display top bar. Controlled by Ctrl-p in run-time. This configuration file holds user interface key bindings. Default content: backspace=KEY_BACKSPACE + backspace_alt=KEY_ALT_BACKSPACE cancel=KEY_CTRLC delete=KEY_DC + delete_line_after_cursor=KEY_CTRLK + delete_line_before_cursor=KEY_CTRLU delete_msg=KEY_CTRLD down=KEY_DOWN end=KEY_END @@ -431,7 +434,7 @@ This configuration file holds user interface key bindings. Default content: toggle_list=KEY_CTRLL toggle_top=KEY_CTRLP transfer=KEY_CTRLT - unread_chat=KEY_CTRLU + unread_chat=KEY_CTRLF up=KEY_UP Refer to function UiKeyConfig::GetKeyCode() in diff --git a/lib/ncutil/src/strutil.cpp b/lib/ncutil/src/strutil.cpp index 2bb8aea0..1b68ac44 100644 --- a/lib/ncutil/src/strutil.cpp +++ b/lib/ncutil/src/strutil.cpp @@ -21,6 +21,20 @@ #include "emoji.h" #include "log.h" +void StrUtil::DeleteFromMatch(std::wstring& p_Str, int& p_EndPos, const wchar_t p_StartChar) +{ + if (p_Str.empty()) return; + + size_t startPos = p_Str.rfind(p_StartChar, (p_EndPos > 0) ? (p_EndPos - 1) : 0); + if (startPos == std::wstring::npos) + { + startPos = 0; + } + + p_Str.erase(startPos, p_EndPos - startPos); + p_EndPos -= (p_EndPos - startPos); +} + void StrUtil::DeleteToMatch(std::wstring& p_Str, const int p_StartPos, const wchar_t p_EndChar) { size_t endPos = p_Str.find(p_EndChar, p_StartPos); diff --git a/lib/ncutil/src/strutil.h b/lib/ncutil/src/strutil.h index af3d18ca..b36b1a05 100644 --- a/lib/ncutil/src/strutil.h +++ b/lib/ncutil/src/strutil.h @@ -14,6 +14,7 @@ class StrUtil { public: + static void DeleteFromMatch(std::wstring& p_Str, int& p_EndPos, const wchar_t p_StartChar); static void DeleteToMatch(std::wstring& p_Str, const int p_StartPos, const wchar_t p_EndChar); static std::string Emojize(const std::string& p_Str); static std::vector ExtractUrlsFromStr(const std::string& p_Str); diff --git a/src/nchat.1 b/src/nchat.1 index a73a1b4f..54bc5e12 100644 --- a/src/nchat.1 +++ b/src/nchat.1 @@ -1,5 +1,5 @@ .\" DO NOT MODIFY THIS FILE! It was generated by help2man. -.TH NCHAT "1" "April 2022" "nchat v2.50" "User Commands" +.TH NCHAT "1" "April 2022" "nchat v2.51" "User Commands" .SH NAME nchat \- ncurses chat .SH SYNOPSIS diff --git a/src/uikeyconfig.cpp b/src/uikeyconfig.cpp index e4fdad39..63708ff5 100644 --- a/src/uikeyconfig.cpp +++ b/src/uikeyconfig.cpp @@ -28,16 +28,17 @@ void UiKeyConfig::Init() { "end", "KEY_END" }, { "home", "KEY_HOME" }, { "backspace", "KEY_BACKSPACE" }, - { "alt_backspace", "KEY_ALT_BACKSPACE" }, + { "backspace_alt", "KEY_ALT_BACKSPACE" }, { "delete", "KEY_DC" }, - { "delete_line", "KEY_CTRLK" }, + { "delete_line_after_cursor", "KEY_CTRLK" }, + { "delete_line_before_cursor", "KEY_CTRLU" }, { "toggle_emoji", "KEY_CTRLY" }, { "toggle_help", "KEY_CTRLG" }, { "toggle_list", "KEY_CTRLL" }, { "toggle_top", "KEY_CTRLP" }, { "next_chat", "KEY_TAB" }, { "prev_chat", "KEY_BTAB" }, - { "unread_chat", "KEY_CTRLU" }, + { "unread_chat", "KEY_CTRLF" }, { "send_msg", "KEY_CTRLX" }, { "delete_msg", "KEY_CTRLD" }, { "open", "KEY_CTRLV" }, diff --git a/src/uilistdialog.cpp b/src/uilistdialog.cpp index a52cc4cf..ba979bb9 100644 --- a/src/uilistdialog.cpp +++ b/src/uilistdialog.cpp @@ -69,7 +69,7 @@ void UiListDialog::KeyHandler(wint_t p_Key) static wint_t keyEnd = UiKeyConfig::GetKey("end"); static wint_t keyHome = UiKeyConfig::GetKey("home"); static wint_t keyBackspace = UiKeyConfig::GetKey("backspace"); - static wint_t keyAltBackspace = UiKeyConfig::GetKey("alt_backspace"); + static wint_t keyBackspaceAlt = UiKeyConfig::GetKey("backspace_alt"); bool isDirty = true; if (p_Key == KEY_RESIZE) @@ -138,7 +138,7 @@ void UiListDialog::KeyHandler(wint_t p_Key) { m_Index = std::numeric_limits::max(); } - else if ((p_Key == keyBackspace) || (p_Key == keyAltBackspace)) + else if ((p_Key == keyBackspace) || (p_Key == keyBackspaceAlt)) { if (m_FilterStr.size() > 0) { diff --git a/src/uimodel.cpp b/src/uimodel.cpp index dda98a52..20ba0c58 100644 --- a/src/uimodel.cpp +++ b/src/uimodel.cpp @@ -265,9 +265,10 @@ void UiModel::EntryKeyHandler(wint_t p_Key) static wint_t keyLeft = UiKeyConfig::GetKey("left"); static wint_t keyRight = UiKeyConfig::GetKey("right"); static wint_t keyBackspace = UiKeyConfig::GetKey("backspace"); - static wint_t keyAltBackspace = UiKeyConfig::GetKey("alt_backspace"); + static wint_t keyBackspaceAlt = UiKeyConfig::GetKey("backspace_alt"); static wint_t keyDelete = UiKeyConfig::GetKey("delete"); - static wint_t keyDeleteLine = UiKeyConfig::GetKey("delete_line"); + static wint_t keyDeleteLineAfterCursor = UiKeyConfig::GetKey("delete_line_after_cursor"); + static wint_t keyDeleteLineBeforeCursor = UiKeyConfig::GetKey("delete_line_before_cursor"); std::string profileId = m_CurrentChat.first; std::string chatId = m_CurrentChat.second; @@ -391,7 +392,7 @@ void UiModel::EntryKeyHandler(wint_t p_Key) entryPos = NumUtil::Bound(0, entryPos + 1, (int)entryStr.size()); } } - else if ((p_Key == keyBackspace) || (p_Key == keyAltBackspace)) + else if ((p_Key == keyBackspace) || (p_Key == keyBackspaceAlt)) { if (entryPos > 0) { @@ -416,10 +417,14 @@ void UiModel::EntryKeyHandler(wint_t p_Key) SetTyping(profileId, chatId, true); } } - else if (p_Key == keyDeleteLine) + else if (p_Key == keyDeleteLineAfterCursor) { StrUtil::DeleteToMatch(entryStr, entryPos, L'\n'); } + else if (p_Key == keyDeleteLineBeforeCursor) + { + StrUtil::DeleteFromMatch(entryStr, entryPos, L'\n'); + } else if (StrUtil::IsValidTextKey(p_Key)) { entryStr.insert(entryPos++, 1, p_Key);