Skip to content

Commit

Permalink
add support for deleting line before cursor with ctrl-u
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Apr 17, 2022
1 parent df1e602 commit 013ed70
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/ncutil/src/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/ncutil/src/strutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> ExtractUrlsFromStr(const std::string& p_Str);
Expand Down
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/uikeyconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
4 changes: 2 additions & 2 deletions src/uilistdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -138,7 +138,7 @@ void UiListDialog::KeyHandler(wint_t p_Key)
{
m_Index = std::numeric_limits<int>::max();
}
else if ((p_Key == keyBackspace) || (p_Key == keyAltBackspace))
else if ((p_Key == keyBackspace) || (p_Key == keyBackspaceAlt))
{
if (m_FilterStr.size() > 0)
{
Expand Down
13 changes: 9 additions & 4 deletions src/uimodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down

0 comments on commit 013ed70

Please sign in to comment.