~kiwec/ke

ref: 842396976984e3cbd83251296dd90a46b9d53b3c ke/src/Prompt.h -rw-r--r-- 852 bytes
84239697 — Wolf Clément Downgrade to C++17, avoid stdlib 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#pragma once
#include <functional>
#include <termbox.h>
#include <wchar.h>
#include "Document.h"
#include "Input.h"

class Prompt : public InputHandler {
private:
	const wchar_t* m_prompt = L"Example prompt : ";
	Text m_answer;
	uint m_pos = 0;

	std::function<void(wchar_t*)> m_callback;
	bool m_prompting;

public:
	void draw(int rwidth);
	virtual bool handle_key(tb_event* ev) override;
	virtual bool handle_mouse(tb_event* ev) override;

	Prompt() : m_prompting(false) {}

	Prompt(const wchar_t* prompt, std::function<void(wchar_t*)> callback)
		: m_prompt(prompt), m_callback(callback), m_prompting(true) {}

	uint get_visible_x();

	void set_answer(wchar_t* str) {
		m_answer = Text(); // erase current text
		m_answer.insert(str, wcslen(str), 0);
	}

	/// Returns true if user hasn't answered yet
	bool is_prompting() { return m_prompting; }
};