xaizek / pipedial (License: GPLv3 only) (since 2019-01-08)
One more tool for selecting something in console.
Commit 3f214e687a2ddeab2b13b0a71cfc64c90aa5c3d5

Add input changed callback to PromptRequest
Author: xaizek
Author date (UTC): 2019-01-22 17:29
Committer name: xaizek
Committer date (UTC): 2019-01-24 13:53
Parent(s): 260e8044bfef2810ace576a532b07897e2b2bdf9
Signing key: 99DC5E4DB05F6BE2
Tree: 374adc890532084718abc6ebd640e63a04fcb350
File Lines added Lines deleted
src/PromptRequest.cpp 10 2
src/PromptRequest.hpp 13 6
File src/PromptRequest.cpp changed (mode: 100644) (index 4758e2d..823cb9a)
... ... PromptResult::PromptResult(std::wstring result)
77 77 { } { }
78 78
79 79 PromptRequest::PromptRequest(cursed::Input &input, cursed::Screen &screen, PromptRequest::PromptRequest(cursed::Input &input, cursed::Screen &screen,
80 cursed::Prompt &promptArea)
81 : input(input), screen(screen), promptArea(promptArea)
80 cursed::Prompt &promptArea,
81 inputChangedFunc onInputChanged)
82 : input(input), screen(screen), promptArea(promptArea),
83 onInputChanged(std::move(onInputChanged))
82 84 { {
83 85 screen.showCursor(); screen.showCursor();
84 86 } }
 
... ... PromptRequest::prompt(const std::wstring &invitation,
93 95 const std::wstring &initial) const std::wstring &initial)
94 96 { {
95 97 initialValue = toNarrow(initial); initialValue = toNarrow(initial);
98 lastValue = initialValue;
96 99
97 100 static PromptRequest *instance; static PromptRequest *instance;
98 101 instance = this; instance = this;
 
... ... PromptRequest::rlStartup()
136 139 void void
137 140 PromptRequest::rlRedisplay() PromptRequest::rlRedisplay()
138 141 { {
142 if (onInputChanged && rl_line_buffer != lastValue) {
143 onInputChanged(toWide(rl_line_buffer));
144 lastValue = rl_line_buffer;
145 }
146
139 147 std::wstring prompt = toWide(rl_display_prompt); std::wstring prompt = toWide(rl_display_prompt);
140 148 std::wstring beforeCursor = toWide({ rl_line_buffer, std::wstring beforeCursor = toWide({ rl_line_buffer,
141 149 rl_line_buffer + rl_point }); rl_line_buffer + rl_point });
File src/PromptRequest.hpp changed (mode: 100644) (index ad27139..f05c09f)
19 19 #ifndef PIPEDIAL__PROMPTREQUEST_HPP__ #ifndef PIPEDIAL__PROMPTREQUEST_HPP__
20 20 #define PIPEDIAL__PROMPTREQUEST_HPP__ #define PIPEDIAL__PROMPTREQUEST_HPP__
21 21
22 #include <functional>
22 23 #include <string> #include <string>
23 24 #include <vector> #include <vector>
24 25
 
... ... private:
58 59 // Provides text editing facility via prompt. // Provides text editing facility via prompt.
59 60 class PromptRequest class PromptRequest
60 61 { {
62 // Type of callback function invoked when input is changed.
63 using inputChangedFunc = std::function<void(const std::wstring &newInput)>;
64
61 65 public: public:
62 66 // Remembers arguments and makes cursor visible. // Remembers arguments and makes cursor visible.
63 67 PromptRequest(cursed::Input &input, cursed::Screen &screen, PromptRequest(cursed::Input &input, cursed::Screen &screen,
64 cursed::Prompt &promptArea);
68 cursed::Prompt &promptArea,
69 inputChangedFunc onInputChanged = {});
65 70 // Cleans up and hides cursor. // Cleans up and hides cursor.
66 71 ~PromptRequest(); ~PromptRequest();
67 72
 
... ... private:
94 99 void updateScreen(); void updateScreen();
95 100
96 101 private: private:
97 cursed::Input &input; // Source of input events.
98 cursed::Screen &screen; // Screen manager.
99 cursed::Prompt &promptArea; // Widget that displays the prompt.
100 std::string initialValue; // Initial value of the prompt.
101 std::vector<int> inputBuf; // Input queue.
102 cursed::Input &input; // Source of input events.
103 cursed::Screen &screen; // Screen manager.
104 cursed::Prompt &promptArea; // Widget that displays the prompt.
105 inputChangedFunc onInputChanged; // Callback invoked when input is changed.
106 std::string initialValue; // Initial value of the prompt.
107 std::string lastValue; // Last seen input value.
108 std::vector<int> inputBuf; // Input queue.
102 109 }; };
103 110
104 111 #endif // PIPEDIAL__PROMPTREQUEST_HPP__ #endif // PIPEDIAL__PROMPTREQUEST_HPP__
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pipedial

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pipedial

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master