-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathctrl_c_exit.hpp
More file actions
58 lines (49 loc) · 1.47 KB
/
Copy pathctrl_c_exit.hpp
File metadata and controls
58 lines (49 loc) · 1.47 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include <chrono>
namespace acecode::tui {
inline constexpr std::chrono::milliseconds kCtrlCExitWindow{1000};
enum class CtrlCExitAction {
Arm,
Exit,
};
inline void clear_ctrl_c_exit_state(
bool& armed,
std::chrono::steady_clock::time_point& last_press) {
armed = false;
last_press = {};
}
inline bool ctrl_c_exit_expired(
bool armed,
std::chrono::steady_clock::time_point last_press,
std::chrono::steady_clock::time_point now,
std::chrono::milliseconds window = kCtrlCExitWindow) {
if (!armed) {
return false;
}
return now - last_press > window;
}
inline bool expire_ctrl_c_exit_state(
bool& armed,
std::chrono::steady_clock::time_point& last_press,
std::chrono::steady_clock::time_point now,
std::chrono::milliseconds window = kCtrlCExitWindow) {
if (!ctrl_c_exit_expired(armed, last_press, now, window)) {
return false;
}
clear_ctrl_c_exit_state(armed, last_press);
return true;
}
inline CtrlCExitAction record_ctrl_c_exit_press(
bool& armed,
std::chrono::steady_clock::time_point& last_press,
std::chrono::steady_clock::time_point now,
std::chrono::milliseconds window = kCtrlCExitWindow) {
if (armed && !ctrl_c_exit_expired(armed, last_press, now, window)) {
clear_ctrl_c_exit_state(armed, last_press);
return CtrlCExitAction::Exit;
}
armed = true;
last_press = now;
return CtrlCExitAction::Arm;
}
} // namespace acecode::tui