From 6fd36756eef58d188dd1109803ddf401f5bc38fb Mon Sep 17 00:00:00 2001 From: Kristofer Berggren Date: Sat, 22 Jun 2024 19:02:57 +0800 Subject: [PATCH] add optional support for send with enter key --- README.md | 47 ++++++++++++++++++++++---------------- lib/common/src/version.h | 2 +- lib/ncutil/src/strutil.cpp | 2 +- src/nchat.1 | 2 +- src/ui.cpp | 1 + src/uiconfig.cpp | 1 + src/uihelpview.cpp | 6 ++--- src/uikeyconfig.cpp | 14 ++++++++---- src/uikeyconfig.h | 7 +----- src/uikeydump.cpp | 3 +++ src/uilistdialog.cpp | 4 ++-- src/uimessagedialog.cpp | 6 ++--- src/uimodel.cpp | 7 ++++++ src/uitextinputdialog.cpp | 4 ++-- 14 files changed, 63 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 831dbeb8..9f13ea5d 100644 --- a/README.md +++ b/README.md @@ -336,6 +336,7 @@ This configuration file holds general user interface settings. Default content: file_picker_command= help_enabled=1 home_fetch_all=0 + linefeed_on_enter=1 link_open_command= list_enabled=1 list_width=14 @@ -458,6 +459,13 @@ Specifies whether to display help bar. Controlled by Ctrl-g in run-time. Specifies whether `home` button shall repeatedly fetch all chat history. +### linefeed_on_enter + +Specifies if enter key press should be read as linefeed (LF `\12`). +Otherwise read as carriage return (CR `\15`). This setting is only +relevant if `key.conf` uses numerical key value for enter (LF `\12`, +CR `\15`). The key name `KEY_RETURN` always maps to the one in use. + ### link_open_command Specifies a custom command to use for opening/viewing links. The @@ -618,8 +626,10 @@ This configuration file holds user interface key bindings. Default content: jump_quoted=\33\161 kill_word= left=KEY_LEFT + linebreak=KEY_RETURN next_chat=KEY_TAB next_page=KEY_NPAGE + ok=KEY_RETURN open=KEY_CTRLV open_link=KEY_CTRLW open_msg=\33\167 @@ -629,7 +639,6 @@ This configuration file holds user interface key bindings. Default content: prev_page=KEY_PPAGE quit=KEY_CTRLQ react=\33\163 - return=KEY_RETURN right=KEY_RIGHT save=KEY_CTRLR select_contact=KEY_CTRLN @@ -854,24 +863,7 @@ setting is not specified. FAQ === -### Custom colors are not shown when running nchat? - -Please try to run nchat with a TERM supporting custom colors, e.g: - - TERM=xterm-256color nchat - -### No QR code is shown in the terminal when setting up WhatsApp in WSL? - -If the environment variable `$DISPLAY` is set, nchat uses the default image -viewer via `xdg-open` to display the QR code. If running Linux without X, -but still have `$DISPLAY` set (some Windows Subsystem for Linux / WSL / WSL2 -setups seems to do this), one needs to unset it before performing WhatsApp -setup, e.g.: - - unset DISPLAY - nchat -s - -### Alt/Opt-keyboard shortcuts are not working +### 1. Alt/Opt-keyboard shortcuts are not working? For Linux please ensure the terminal is configured with [meta to send escape](https://askubuntu.com/questions/442644/how-to-make-xterm-to-send-the-alt-key-to-emacs-as-meta). @@ -881,7 +873,22 @@ For macOS Terminal ensure that the Terminal profile keyboard setting If issues are still encountered, please use `nchat -k` (keydump mode) to determine the key codes and modify `~/.config/nchat/key.conf` accordingly. -### Sent messages are not visible +### 2. Send messages with Enter key? + +To simply send on enter key press and skip message compose with linebreaks, +one can just set `send_msg=KEY_RETURN` in `~/.config/nchat/key.conf`. + +To also be able to compose messages with linebreaks using Alt/Opt-Enter, edit +`~/.config/nchat/ui.conf` and set `linefeed_on_enter=0`. And in +`~/.config/nchat/key.conf` set `linebreak=\33\15`. + +### 3. Custom colors are not shown when running nchat in tmux? + +Please try to run nchat with a TERM supporting custom colors, e.g: + + TERM=xterm-256color nchat + +### 4. Sent messages are not visible? For terminals with eight colors (or more) the default color theme displays sent messages in gray (shaded). Some terminals may wrongly report supporting diff --git a/lib/common/src/version.h b/lib/common/src/version.h index e622b0e1..b6f24f6c 100644 --- a/lib/common/src/version.h +++ b/lib/common/src/version.h @@ -7,4 +7,4 @@ #pragma once -#define NCHAT_VERSION "4.89" +#define NCHAT_VERSION "4.90" diff --git a/lib/ncutil/src/strutil.cpp b/lib/ncutil/src/strutil.cpp index 910db8fe..112875a2 100644 --- a/lib/ncutil/src/strutil.cpp +++ b/lib/ncutil/src/strutil.cpp @@ -184,7 +184,7 @@ bool StrUtil::IsInteger(const std::string& p_Str) bool StrUtil::IsValidTextKey(int p_Key) { - return ((p_Key >= 0x20) || (p_Key == 0xA)); + return (p_Key >= 0x20); } std::string StrUtil::Join(const std::vector& p_Lines, const std::string& p_Delim) diff --git a/src/nchat.1 b/src/nchat.1 index 5cf3bcdb..31f96ee6 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" "June 2024" "nchat v4.89" "User Commands" +.TH NCHAT "1" "June 2024" "nchat v4.90" "User Commands" .SH NAME nchat \- ncurses chat .SH SYNOPSIS diff --git a/src/ui.cpp b/src/ui.cpp index b05236f4..fc47ba38 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -53,6 +53,7 @@ void Ui::Init() initscr(); noecho(); cbreak(); + UiConfig::GetBool("linefeed_on_enter") ? nl() : nonl(); keypad(stdscr, TRUE); curs_set(0); timeout(0); diff --git a/src/uiconfig.cpp b/src/uiconfig.cpp index e06f95c5..49552f62 100644 --- a/src/uiconfig.cpp +++ b/src/uiconfig.cpp @@ -33,6 +33,7 @@ void UiConfig::Init() { "file_picker_command", "" }, { "help_enabled", "1" }, { "home_fetch_all", "0" }, + { "linefeed_on_enter", "1" }, { "link_open_command", "" }, { "list_enabled", "1" }, { "list_width", "14" }, diff --git a/src/uihelpview.cpp b/src/uihelpview.cpp index cd37cab4..ca23b3db 100644 --- a/src/uihelpview.cpp +++ b/src/uihelpview.cpp @@ -1,6 +1,6 @@ // uihelpview.cpp // -// Copyright (c) 2019-2023 Kristofer Berggren +// Copyright (c) 2019-2024 Kristofer Berggren // All rights reserved. // // nchat is distributed under the MIT license, see LICENSE for details. @@ -34,7 +34,7 @@ void UiHelpView::Draw() static std::vector listDialogHelpItems = []() { std::vector helpItems; - AppendHelpItem(UiKeyConfig::GetKey("return"), "Select", helpItems); + AppendHelpItem(UiKeyConfig::GetKey("ok"), "Select", helpItems); AppendHelpItem(UiKeyConfig::GetKey("cancel"), "Cancel", helpItems); AppendHelpItem('a', "AddFiltr", helpItems); AppendHelpItem(UiKeyConfig::GetKey("backspace"), "DelFiltr", helpItems); @@ -44,7 +44,7 @@ void UiHelpView::Draw() static std::vector messageDialogHelpItems = []() { std::vector helpItems; - AppendHelpItem(UiKeyConfig::GetKey("return"), "OK", helpItems); + AppendHelpItem(UiKeyConfig::GetKey("ok"), "OK", helpItems); AppendHelpItem(UiKeyConfig::GetKey("cancel"), "Cancel", helpItems); return helpItems; }(); diff --git a/src/uikeyconfig.cpp b/src/uikeyconfig.cpp index 7aade607..dd9319b8 100644 --- a/src/uikeyconfig.cpp +++ b/src/uikeyconfig.cpp @@ -14,17 +14,22 @@ #include "fileutil.h" #include "log.h" #include "strutil.h" +#include "uiconfig.h" Config UiKeyConfig::m_Config; std::map UiKeyConfig::m_KeyCodes; void UiKeyConfig::InitKeyCodes(bool p_MapKeys) { + int keyCodeTab = 9; + int keyCodeSpace = 32; + int keyCodeReturn = UiConfig::GetBool("linefeed_on_enter") ? 10 : 13; + m_KeyCodes = std::map({ // additional keys - { "KEY_TAB", KEY_TAB }, - { "KEY_RETURN", KEY_RETURN }, - { "KEY_SPACE", KEY_SPACE }, + { "KEY_TAB", keyCodeTab }, + { "KEY_SPACE", keyCodeSpace }, + { "KEY_RETURN", keyCodeReturn }, { "KEY_NONE", -1 }, // ctrl keys @@ -187,7 +192,8 @@ void UiKeyConfig::Init(bool p_MapKeys) { "quit", "KEY_CTRLQ" }, { "left", "KEY_LEFT" }, { "right", "KEY_RIGHT" }, - { "return", "KEY_RETURN" }, + { "ok", "KEY_RETURN" }, + { "linebreak", "KEY_RETURN" }, { "prev_page", "KEY_PPAGE" }, { "next_page", "KEY_NPAGE" }, { "down", "KEY_DOWN" }, diff --git a/src/uikeyconfig.h b/src/uikeyconfig.h index fd78cf9a..487614ab 100644 --- a/src/uikeyconfig.h +++ b/src/uikeyconfig.h @@ -1,6 +1,6 @@ // uikeyconfig.h // -// Copyright (c) 2019-2023 Kristofer Berggren +// Copyright (c) 2019-2024 Kristofer Berggren // All rights reserved. // // nchat is distributed under the MIT license, see LICENSE for details. @@ -11,11 +11,6 @@ #include "config.h" -#define KEY_TAB 9 -#define KEY_RETURN 10 -#define KEY_SPACE 32 -#define KEY_DELETE 127 - class UiKeyConfig { public: diff --git a/src/uikeydump.cpp b/src/uikeydump.cpp index 6eb6fea2..3d3011c5 100644 --- a/src/uikeydump.cpp +++ b/src/uikeydump.cpp @@ -14,6 +14,7 @@ #include +#include "uiconfig.h" #include "uicontroller.h" #include "uikeyconfig.h" #include "uikeyinput.h" @@ -22,11 +23,13 @@ void UiKeyDump::Run() { + UiConfig::Init(); printf("\033[?1004h"); setlocale(LC_ALL, ""); initscr(); noecho(); cbreak(); + UiConfig::GetBool("linefeed_on_enter") ? nl() : nonl(); raw(); keypad(stdscr, TRUE); curs_set(0); diff --git a/src/uilistdialog.cpp b/src/uilistdialog.cpp index d1846e86..02ecf29d 100644 --- a/src/uilistdialog.cpp +++ b/src/uilistdialog.cpp @@ -63,7 +63,7 @@ void UiListDialog::KeyHandler(wint_t p_Key) static wint_t keyLeft = UiKeyConfig::GetKey("left"); static wint_t keyRight = UiKeyConfig::GetKey("right"); - static wint_t keyReturn = UiKeyConfig::GetKey("return"); + static wint_t keyOk = UiKeyConfig::GetKey("ok"); static wint_t keyPrevPage = UiKeyConfig::GetKey("prev_page"); static wint_t keyNextPage = UiKeyConfig::GetKey("next_page"); static wint_t keyDown = UiKeyConfig::GetKey("down"); @@ -105,7 +105,7 @@ void UiListDialog::KeyHandler(wint_t p_Key) m_Result = false; m_Running = false; } - else if ((p_Key == keyRight) || (p_Key == keyReturn)) + else if ((p_Key == keyRight) || (p_Key == keyOk)) { OnSelect(); } diff --git a/src/uimessagedialog.cpp b/src/uimessagedialog.cpp index e822e9bf..c1291efc 100644 --- a/src/uimessagedialog.cpp +++ b/src/uimessagedialog.cpp @@ -1,6 +1,6 @@ // uimessagedialog.cpp // -// Copyright (c) 2019-2023 Kristofer Berggren +// Copyright (c) 2019-2024 Kristofer Berggren // All rights reserved. // // nchat is distributed under the MIT license, see LICENSE for details. @@ -49,7 +49,7 @@ void UiMessageDialog::KeyHandler(wint_t p_Key) static wint_t keyCancel = UiKeyConfig::GetKey("cancel"); static wint_t keyQuit = UiKeyConfig::GetKey("quit"); static wint_t keyOtherCommandsHelp = UiKeyConfig::GetKey("other_commands_help"); - static wint_t keyReturn = UiKeyConfig::GetKey("return"); + static wint_t keyOk = UiKeyConfig::GetKey("ok"); static wint_t keyTerminalFocusIn = UiKeyConfig::GetKey("terminal_focus_in"); static wint_t keyTerminalFocusOut = UiKeyConfig::GetKey("terminal_focus_out"); static wint_t keyTerminalResize = UiKeyConfig::GetKey("terminal_resize"); @@ -82,7 +82,7 @@ void UiMessageDialog::KeyHandler(wint_t p_Key) m_Result = false; m_Running = false; } - else if (p_Key == keyReturn) + else if (p_Key == keyOk) { m_Result = true; m_Running = false; diff --git a/src/uimodel.cpp b/src/uimodel.cpp index 16193a0f..3abb2893 100644 --- a/src/uimodel.cpp +++ b/src/uimodel.cpp @@ -375,6 +375,7 @@ void UiModel::EntryKeyHandler(wint_t p_Key) static wint_t keyBackwardKillWord = UiKeyConfig::GetKey("backward_kill_word"); static wint_t keyKillWord = UiKeyConfig::GetKey("kill_word"); static wint_t keyClear = UiKeyConfig::GetKey("clear"); + static wint_t keyLinebreak = UiKeyConfig::GetKey("linebreak"); std::string profileId = m_CurrentChat.first; std::string chatId = m_CurrentChat.second; @@ -565,6 +566,12 @@ void UiModel::EntryKeyHandler(wint_t p_Key) entryPos = 0; SetTyping(profileId, chatId, true); } + else if (p_Key == keyLinebreak) + { + wint_t keyLF = 0xA; + entryStr.insert(entryPos++, 1, keyLF); + SetTyping(profileId, chatId, true); + } else if (StrUtil::IsValidTextKey(p_Key)) { entryStr.insert(entryPos++, 1, p_Key); diff --git a/src/uitextinputdialog.cpp b/src/uitextinputdialog.cpp index a9a3dba9..a6424e01 100644 --- a/src/uitextinputdialog.cpp +++ b/src/uitextinputdialog.cpp @@ -51,7 +51,7 @@ void UiTextInputDialog::KeyHandler(wint_t p_Key) static wint_t keyCancel = UiKeyConfig::GetKey("cancel"); static wint_t keyQuit = UiKeyConfig::GetKey("quit"); static wint_t keyOtherCommandsHelp = UiKeyConfig::GetKey("other_commands_help"); - static wint_t keyReturn = UiKeyConfig::GetKey("return"); + static wint_t keyOk = UiKeyConfig::GetKey("ok"); static wint_t keyTerminalFocusIn = UiKeyConfig::GetKey("terminal_focus_in"); static wint_t keyTerminalFocusOut = UiKeyConfig::GetKey("terminal_focus_out"); static wint_t keyTerminalResize = UiKeyConfig::GetKey("terminal_resize"); @@ -83,7 +83,7 @@ void UiTextInputDialog::KeyHandler(wint_t p_Key) m_Result = false; m_Running = false; } - else if (p_Key == keyReturn) + else if (p_Key == keyOk) { m_Result = true; m_Running = false;