Skip to content

Commit

Permalink
add optional support for send with enter key
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Jun 22, 2024
1 parent 354b0b7 commit 6fd3675
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 43 deletions.
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/common/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

#pragma once

#define NCHAT_VERSION "4.89"
#define NCHAT_VERSION "4.90"
2 changes: 1 addition & 1 deletion lib/ncutil/src/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& p_Lines, const std::string& p_Delim)
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" "June 2024" "nchat v4.89" "User Commands"
.TH NCHAT "1" "June 2024" "nchat v4.90" "User Commands"
.SH NAME
nchat \- ncurses chat
.SH SYNOPSIS
Expand Down
1 change: 1 addition & 0 deletions src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/uiconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
6 changes: 3 additions & 3 deletions src/uihelpview.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,7 +34,7 @@ void UiHelpView::Draw()
static std::vector<std::wstring> listDialogHelpItems = []()
{
std::vector<std::wstring> 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);
Expand All @@ -44,7 +44,7 @@ void UiHelpView::Draw()
static std::vector<std::wstring> messageDialogHelpItems = []()
{
std::vector<std::wstring> helpItems;
AppendHelpItem(UiKeyConfig::GetKey("return"), "OK", helpItems);
AppendHelpItem(UiKeyConfig::GetKey("ok"), "OK", helpItems);
AppendHelpItem(UiKeyConfig::GetKey("cancel"), "Cancel", helpItems);
return helpItems;
}();
Expand Down
14 changes: 10 additions & 4 deletions src/uikeyconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
#include "fileutil.h"
#include "log.h"
#include "strutil.h"
#include "uiconfig.h"

Config UiKeyConfig::m_Config;
std::map<std::string, int> 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<std::string, int>({
// 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
Expand Down Expand Up @@ -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" },
Expand Down
7 changes: 1 addition & 6 deletions src/uikeyconfig.h
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions src/uikeydump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <ncurses.h>

#include "uiconfig.h"
#include "uicontroller.h"
#include "uikeyconfig.h"
#include "uikeyinput.h"
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/uilistdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/uimessagedialog.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/uimodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/uitextinputdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6fd3675

Please sign in to comment.