-
Notifications
You must be signed in to change notification settings - Fork 0
/
newtpp.hpp
1057 lines (888 loc) · 26.9 KB
/
newtpp.hpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <algorithm>
#include <array>
#include <concepts>
#include <newt.h>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>
#include <vector>
namespace newt {
/*
* CONCEPTS AND UTILITYS
*/
class component;
template <class T>
concept generic_component = requires {
requires std::derived_from<T, component> or std::same_as<T, component>;
};
template <typename type>
class conditional_ownership_ptr {
public:
using value_type = type;
using value_type_ptr = type*;
using deleter_ptr = void (*)(value_type_ptr);
private:
value_type_ptr data { nullptr };
deleter_ptr deleter { no_delete };
public:
static void no_delete(value_type_ptr /*unused*/) { }
conditional_ownership_ptr(value_type_ptr pointer, deleter_ptr deleter_fun)
: data { pointer }
, deleter(deleter_fun)
{
}
conditional_ownership_ptr(conditional_ownership_ptr& other) noexcept
: data(other.data)
, deleter(other.deleter)
{
other.deleter = no_delete;
}
conditional_ownership_ptr(conditional_ownership_ptr&& other) noexcept
: data(other.data)
, deleter(other.deleter)
{
other.deleter = no_delete;
}
/*
* Allowing a copy wold mean that copying from a pointer that
* is currently an owner would result in two owning pointers
*/
conditional_ownership_ptr& operator=(const conditional_ownership_ptr&) = delete;
conditional_ownership_ptr& operator=(conditional_ownership_ptr&& other) noexcept
{
deleter(data);
data = other.data;
deleter = other.deleter;
other.deleter = conditional_ownership_ptr::no_delete;
return *this;
}
bool operator<=>(const conditional_ownership_ptr&) const = default;
value_type_ptr operator*()
{
return data;
}
value_type_ptr operator*() const
{
return data;
}
[[nodiscard]] value_type_ptr get() const
{
return data;
}
[[nodiscard]] value_type_ptr own()
{
deleter = no_delete;
return data;
}
~conditional_ownership_ptr() noexcept
{
deleter(data);
}
};
/*
* SIZING AND POSITION
*/
template <std::integral number>
struct size_base {
number width { 0 };
number height { 0 };
size_base() = default;
// NOLINTNEXTLINE
size_base(const number WIDHT, const number HEIGHT)
: width(WIDHT)
, height(HEIGHT)
{
}
template <std::integral other_number>
explicit size_base(const size_base<other_number> OTHER)
: width(static_cast<number>(OTHER.width))
, height(static_cast<number>(OTHER.height))
{
}
size_base operator+(const number SCALAR) const
{
return size_base { width + SCALAR, height + SCALAR };
}
size_base operator-(const number SCALAR) const
{
return size_base { width - SCALAR, height - SCALAR };
}
size_base operator*(const number SCALAR) const
{
return size_base { width * SCALAR, height * SCALAR };
}
size_base operator/(const number SCALAR) const
{
return size_base { width / SCALAR, height / SCALAR };
}
size_base operator+(const size_base OTHER) const
{
return size_base { width + OTHER.width, height + OTHER.height };
}
size_base operator-(const size_base OTHER) const
{
return size_base { width - OTHER.width, height - OTHER.height };
}
size_base operator*(const size_base OTHER) const
{
return size_base { width * OTHER.width, height * OTHER.height };
}
size_base operator/(const size_base OTHER) const
{
return size_base { width / OTHER.width, height / OTHER.height };
}
};
using size = size_base<int>;
using usize = size_base<unsigned int>;
struct position : public size_base<int> {
int& left { width };
int& top { height };
position() = default;
position(const int LEFT, const int TOP)
: size_base(LEFT, TOP)
{
}
};
/*
* COLOR MANAGMENT
*/
namespace theme {
struct colors {
std::string_view root_fg;
std::string_view root_bg;
std::string_view border_fg;
std::string_view border_bg;
std::string_view window_fg;
std::string_view window_bg;
std::string_view shadow_fg;
std::string_view shadow_bg;
std::string_view title_fg;
std::string_view title_bg;
std::string_view button_fg;
std::string_view button_bg;
std::string_view act_button_fg;
std::string_view act_button_bg;
std::string_view checkbox_fg;
std::string_view checkbox_bg;
std::string_view act_checkbox_fg;
std::string_view act_checkbox_bg;
std::string_view entry_fg;
std::string_view entry_bg;
std::string_view label_fg;
std::string_view label_bg;
std::string_view listbox_fg;
std::string_view listbox_bg;
std::string_view act_listbox_fg;
std::string_view act_listbox_bg;
std::string_view textbox_fg;
std::string_view textbox_bg;
std::string_view act_textbox_fg;
std::string_view act_textbox_bg;
std::string_view help_line_fg;
std::string_view help_line_bg;
std::string_view root_text_fg;
std::string_view root_text_bg;
std::string_view empty_scale;
std::string_view full_scale;
std::string_view disabled_entry_fg;
std::string_view disabled_entry_bg;
std::string_view compact_button_fg;
std::string_view compact_button_bg;
std::string_view act_sel_listbox_fg;
std::string_view act_sel_listbox_bg;
std::string_view sel_listbox_fg;
std::string_view sel_listbox_bg;
constexpr explicit operator newtColors() const
{
return {
// newt takes char* to construct themes, so this is necessary
// NOLINTBEGIN(cppcoreguidelines-pro-type-const-cast)
.rootFg = const_cast<char*>(root_fg.data()),
.rootBg = const_cast<char*>(root_bg.data()),
.borderFg = const_cast<char*>(border_fg.data()),
.borderBg = const_cast<char*>(border_bg.data()),
.windowFg = const_cast<char*>(window_fg.data()),
.windowBg = const_cast<char*>(window_bg.data()),
.shadowFg = const_cast<char*>(shadow_fg.data()),
.shadowBg = const_cast<char*>(shadow_bg.data()),
.titleFg = const_cast<char*>(title_fg.data()),
.titleBg = const_cast<char*>(title_bg.data()),
.buttonFg = const_cast<char*>(button_fg.data()),
.buttonBg = const_cast<char*>(button_bg.data()),
.actButtonFg = const_cast<char*>(act_button_fg.data()),
.actButtonBg = const_cast<char*>(act_button_bg.data()),
.checkboxFg = const_cast<char*>(checkbox_fg.data()),
.checkboxBg = const_cast<char*>(checkbox_bg.data()),
.actCheckboxFg = const_cast<char*>(act_checkbox_fg.data()),
.actCheckboxBg = const_cast<char*>(act_checkbox_bg.data()),
.entryFg = const_cast<char*>(entry_fg.data()),
.entryBg = const_cast<char*>(entry_bg.data()),
.labelFg = const_cast<char*>(label_fg.data()),
.labelBg = const_cast<char*>(label_bg.data()),
.listboxFg = const_cast<char*>(listbox_fg.data()),
.listboxBg = const_cast<char*>(listbox_bg.data()),
.actListboxFg = const_cast<char*>(act_listbox_fg.data()),
.actListboxBg = const_cast<char*>(act_listbox_bg.data()),
.textboxFg = const_cast<char*>(textbox_fg.data()),
.textboxBg = const_cast<char*>(textbox_bg.data()),
.actTextboxFg = const_cast<char*>(act_textbox_fg.data()),
.actTextboxBg = const_cast<char*>(act_textbox_bg.data()),
.helpLineFg = const_cast<char*>(help_line_fg.data()),
.helpLineBg = const_cast<char*>(help_line_bg.data()),
.rootTextFg = const_cast<char*>(root_text_fg.data()),
.rootTextBg = const_cast<char*>(root_text_bg.data()),
.emptyScale = const_cast<char*>(empty_scale.data()),
.fullScale = const_cast<char*>(full_scale.data()),
.disabledEntryFg = const_cast<char*>(disabled_entry_fg.data()),
.disabledEntryBg = const_cast<char*>(disabled_entry_bg.data()),
.compactButtonFg = const_cast<char*>(compact_button_fg.data()),
.compactButtonBg = const_cast<char*>(compact_button_bg.data()),
.actSelListboxFg = const_cast<char*>(act_sel_listbox_fg.data()),
.actSelListboxBg = const_cast<char*>(act_sel_listbox_bg.data()),
.selListboxFg = const_cast<char*>(sel_listbox_fg.data()),
.selListboxBg = const_cast<char*>(sel_listbox_bg.data())
// NOLINTEND(cppcoreguidelines-pro-type-const-cast)
};
}
};
static void set(const colors& THEME)
{
newtSetColors(static_cast<newtColors>(THEME));
}
constexpr static colors ONE_DARK {
"#abb2bf", "#282c34", /* root fg, bg */
"#282c34", "#5c6370", /* border fg, bg */
"#101215", "#5c6370", /* window fg, bg */
"#abb2bf", "#101215", /* shadow fg, bg */
"#61afef", "#5c6370", /* title fg, bg */
"#282c34", "#e06c75", /* button fg, bg */
"#e06c75", "#282c34", /* active button fg, bg */
"#282c34", "#56b6c2", /* checkbox fg, bg */
"#282c34", "#c678dd", /* active checkbox fg, bg */
"#abb2bf", "#5c6370", /* entry box fg, bg */
"#abb2bf", "#5c6370", /* label fg, bg */
"#282c34", "#56b6c2", /* listbox fg, bg */
"#282c34", "#c678dd", /* active listbox fg, bg */
"#abb2bf", "#282c34", /* textbox fg, bg */
"#282c34", "#e06c75", /* active textbox fg, bg */
"#abb2bf", "#282c34", /* help line */
"#abb2bf", "#282c34", /* root text */
"#e06c75", /* scale full*/
"#98c379", /* scale empty*/
"#abb2bf", "#282c34", /* disabled entry fg, bg */
"#282c34", "#e06c75", /* compact button fg, bg */
"#282c34", "#3c909b", /* active & sel listbox */
"#282c34", "#56b6c2" /* selected listbox */
};
constexpr static colors ONE_LIGHT {
"#abb2bf", "#828997", /* root fg, bg */
"#282c34", "#abb2bf", /* border fg, bg */
"#101215", "#abb2bf", /* window fg, bg */
"#abb2bf", "#282c34", /* shadow fg, bg */
"#3b84c0", "#abb2bf", /* title fg, bg */
"#282c34", "#e06c75", /* button fg, bg */
"#e06c75", "#282c34", /* active button fg, bg */
"#282c34", "#56b6c2", /* checkbox fg, bg */
"#282c34", "#c678dd", /* active checkbox fg, bg */
"#282c34", "#abb2bf", /* entry box fg, bg */
"#282c34", "#abb2bf", /* label fg, bg */
"#282c34", "#56b6c2", /* listbox fg, bg */
"#282c34", "#c678dd", /* active listbox fg, bg */
"#282c34", "#828997", /* textbox fg, bg */
"#282c34", "#e06c75", /* active textbox fg, bg */
"#282c34", "#828997", /* help line */
"#abb2bf", "#282c34", /* root text */
"#be5046", /* scale empty*/
"#7a9f60", /* scale full*/
"#abb2bf", "#282c34", /* disabled entry fg, bg */
"#282c34", "#e06c75", /* compact button fg, bg */
"#282c34", "#3c909b", /* active & sel listbox */
"#282c34", "#56b6c2" /* selected listbox */
};
};
/*
* ROOT WINDOW AND OTHER FREE FUNCTIONS
*/
class root_window {
public:
static void init(const theme::colors& THEME = theme::ONE_DARK) noexcept
{
newtInit();
newtCls();
theme::set(THEME);
}
static void finish() noexcept
{
newtFinished();
}
explicit root_window(const theme::colors& THEME = theme::ONE_DARK)
{
init(THEME);
}
root_window(root_window&) = delete;
root_window(root_window&&) = default;
root_window& operator=(root_window&) = delete;
root_window& operator=(root_window&&) = delete;
~root_window()
{
finish();
}
static void draw_text(const position POS, const std::string_view TEXT)
{
newtDrawRootText(POS.left, POS.top, TEXT.data());
}
static void push_help_line(const std::string_view TEXT)
{
newtPushHelpLine(TEXT.data());
}
static void push_default_help_line()
{
// newt automatically pushes the default help line if the given pointer is nullptr
newtPushHelpLine(nullptr);
}
static void clear_help_line()
{
// newt automatically clears the default help line if the given pointer is pointing to an empty string
push_help_line("");
}
static void pop_help_line()
{
newtPopHelpLine();
}
/* TODO: Add suspand api <29-03-23, Giuseppe> */
};
inline void refresh()
{
newtRefresh();
}
inline void bell()
{
newtBell();
}
inline size get_screen_size()
{
size screen_size;
newtGetScreenSize(&screen_size.width, &screen_size.height);
return screen_size;
}
inline usize get_screen_usize()
{
return usize { get_screen_size() };
}
inline void clear_key_buffer()
{
newtClearKeyBuffer();
}
inline void wait_for_key()
{
newtWaitForKey();
}
inline void reflow_text(std::string& text, const int WIDTH)
{
const int MAX_FLEX { static_cast<int>(WIDTH / 5) };
newtReflowText(text.data(), WIDTH, MAX_FLEX, MAX_FLEX, nullptr, nullptr);
}
[[nodiscard]] inline std::string compute_filler(const std::string_view OLD, const std::string_view NEW)
{
size_t filling_size { OLD.size() - NEW.size() };
/*
* if filling_size is > OLD.size() it means that NEW.size() is bigger
* and doing the subtraction we caused the filling_size to overflow so
* instead we use 0
*/
filling_size = (filling_size < OLD.size()) ? filling_size : 0;
std::string filler;
std::fill_n(filler.begin(), filling_size, ' ');
return filler;
}
void inline resize_screen(const int REDRAW)
{
newtResizeScreen(REDRAW);
}
void inline delay(const unsigned int USECS)
{
newtDelay(USECS);
}
void inline cursor_on()
{
newtCursorOn();
}
void inline cursor_off()
{
newtCursorOff();
}
/*
* GENERIC COMPNENT
*/
class component {
public:
using ptr_type = conditional_ownership_ptr<std::remove_pointer<newtComponent>::type>;
protected:
public:
ptr_type data;
explicit component(newtComponent COMP, auto deleter) noexcept
: data(COMP, deleter)
{
}
explicit component(newtComponent COMP) noexcept
: data(COMP, newtComponentDestroy)
{
}
component(component& OTHER) = default;
component(component&& OTHER) = default;
component& operator=(const component&) noexcept = delete;
component& operator=(component&&) noexcept = default;
~component() = default;
newtComponent operator*() const
{
return *data;
}
newtComponent own()
{
return data.own();
}
template <generic_component component_t>
bool operator==(const component_t& other) const
{
return data == other.data;
}
std::span<component> as_range()
{
return std::span { this, 1 };
}
[[nodiscard]] std::span<const component> as_range() const
{
return std::span { this, 1 };
}
/* TODO: Add general component manipulation <29-03-23, Giuseppe> */
};
template <typename T>
concept component_range = requires(T obj) {
{
obj.as_range()
} -> std::same_as<std::span<component>>;
};
/*
* GRIDS
*/
struct padding {
int left { 0 };
int top { 0 };
int right { 0 };
int bottom { 0 };
};
enum anchor {
NOWHERE = 0,
LEFT = NEWT_ANCHOR_LEFT,
RIGHT = NEWT_ANCHOR_RIGHT,
TOP = NEWT_ANCHOR_TOP,
BOTTOM = NEWT_ANCHOR_BOTTOM,
};
enum grow {
NO = 0,
X = NEWT_GRID_FLAG_GROWX,
Y = NEWT_GRID_FLAG_GROWY,
};
class grid {
newtGrid data;
int cols;
int rows;
int auto_cols { 0 };
int auto_rows { 0 };
void increment_auto()
{
++auto_cols;
auto_rows += auto_cols / cols;
auto_cols = auto_cols % cols;
}
public:
explicit grid(const int COLS, const int ROWS) noexcept
: data(newtCreateGrid(COLS, ROWS))
, cols(COLS)
, rows(ROWS)
{
}
template <component_range... ranges>
explicit grid(const int COLS, const int ROWS, const ranges&... COMPONENTS) noexcept
: data(newtCreateGrid(COLS, ROWS))
, cols(COLS)
, rows(ROWS)
{
set_fields(COMPONENTS...);
}
grid(const grid&) = default;
grid(grid&&) = default;
grid& operator=(const grid&) = default;
grid& operator=(grid&&) = default;
~grid()
{
newtGridFree(data, 1);
}
template <generic_component component_t>
void set_field(const int COL, const int ROW, const component_t& COMPONENT, const padding PADDING = {}, const int ANCHOR = anchor::NOWHERE, const int GROW = grow::NO)
{
newtGridSetField(data, COL, ROW, NEWT_GRID_COMPONENT, *COMPONENT, PADDING.left, PADDING.top, PADDING.right, PADDING.bottom, ANCHOR, GROW);
}
template <component_range... ranges>
void set_fields(const ranges&... COMPONENTS)
{
auto set { [&]<component_range range>(const range& COMP) {
for (const auto& COMPONENT : COMP.as_range()) {
newtGridSetField(data, auto_cols, auto_rows, NEWT_GRID_COMPONENT, *COMPONENT, 0, 0, 0, (auto_rows != (rows - 1)) ? 1 : 0, 0, 0);
increment_auto();
}
} };
(set(COMPONENTS), ...);
}
std::pair<int, int> get_cols_rows()
{
return { cols, rows };
}
explicit operator newtGrid() const
{
return data;
}
};
/*
* WINDOW
*/
class window {
public:
explicit window(const usize SIZE, const std::string_view TITLE = std::string_view {}) noexcept
{
newtCenteredWindow(SIZE.width, SIZE.height, TITLE.data());
}
window(const position POS, const usize SIZE, const std::string_view TITLE = std::string_view {}) noexcept
{
newtOpenWindow(POS.left, POS.top, SIZE.width, SIZE.height, TITLE.data());
}
[[nodiscard]] explicit window(const grid& GRID, const std::string_view TITLE = {}) noexcept
{
/* I hate that i had to use const cast but newt is not consistent with
its API, why does newtGridWrappedWindow takes a char* and other methods
to construct windows instead are taking const char*
NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) */
newtGridWrappedWindow(static_cast<newtGrid>(GRID), const_cast<char*>(TITLE.data()));
}
~window()
{
newtPopWindow();
}
window(window&) noexcept = delete;
window(window&&) noexcept = delete;
window& operator=(window&) noexcept = delete;
window& operator=(window&&) noexcept = delete;
};
/*
* ALL COMPONENTS TYPES!
*/
class scroll_bar : public component {
public:
/* TODO: Change colors things <02-04-23, Giuseppe> */
scroll_bar(const position POS, const int HEIGHT, const int NORMAL_COLOR_SET, const int THUMB_COLOR_SET) noexcept
: component(newtVerticalScrollbar(POS.left, POS.top, HEIGHT, NORMAL_COLOR_SET, THUMB_COLOR_SET))
{
}
void set(const int WHERE, const int TOTAL)
{
newtScrollbarSet(*data, WHERE, TOTAL);
}
void set_color(const int NORMAL, const int THUMB)
{
newtScrollbarSetColors(*data, NORMAL, THUMB);
}
};
enum exit_reason : std::underlying_type_t<decltype(newtExitStruct::NEWT_EXIT_COMPONENT)> {
HOTKEY = newtExitStruct::NEWT_EXIT_HOTKEY,
COMPONENT = newtExitStruct::NEWT_EXIT_COMPONENT,
TIMER = newtExitStruct::NEWT_EXIT_TIMER,
FDREADY = newtExitStruct::NEWT_EXIT_FDREADY,
ERROR = newtExitStruct::NEWT_EXIT_ERROR
};
struct exit_info {
exit_reason reason;
std::variant<int, component> data;
explicit exit_info(const newtExitStruct& OTHER)
: reason(static_cast<exit_reason>(OTHER.reason))
{
switch (reason) {
case exit_reason::HOTKEY:
data = OTHER.u.key;
break;
case exit_reason::COMPONENT:
data = component { OTHER.u.co, component::ptr_type::no_delete };
break;
case exit_reason::TIMER:
data = OTHER.u.watch;
break;
case exit_reason::FDREADY:
case exit_reason::ERROR:
default:
data = -1;
break;
}
}
};
class form : public component {
public:
explicit form(void* help_tag = nullptr, const int FLAGS = 0) noexcept
: component(newtForm(nullptr, help_tag, FLAGS), newtFormDestroy)
{
}
/* TODO: what the fuck is helptag? <02-04-23, Giuseppe> */
explicit form(scroll_bar& bar, void* help_tag = nullptr, const int FLAGS = 0) noexcept
: component(newtForm(bar.own(), help_tag, FLAGS), newtFormDestroy)
{
/* TODO: Implement parameters support <29-03-23, Giuseppe> */
}
template <component_range... ranges>
explicit form(ranges&... components) noexcept
: component(newtForm(nullptr, nullptr, 0), newtFormDestroy)
{
add_components(components...);
}
template <component_range... ranges>
void add_components(ranges&... components)
{
auto add { [&]<component_range range>(range& comp) {
for (auto& comp : comp.as_range()) {
newtFormAddComponent(*data, comp.own());
}
} };
(add(components), ...);
}
exit_info run()
{
newtExitStruct result {};
newtFormRun(*data, &result);
return exit_info { result };
}
void add_hot_key(const int KEY)
{
newtFormAddHotKey(*data, KEY);
}
void watch_fd(const int FILE_DESCRIPTOR, const int FLAGS)
{
newtFormWatchFd(*data, FILE_DESCRIPTOR, FLAGS);
}
void draw_form()
{
newtDrawForm(*data);
}
template <generic_component component_t>
void set_current(const component_t& comp)
{
newtFormSetCurrent(*data, *comp);
}
void set_background(const int COLOR)
{
newtFormSetBackground(*data, COLOR);
}
void set_height(const int HEIGHT)
{
newtFormSetHeight(*data, HEIGHT);
}
void set_width(const int WIDTH)
{
newtFormSetWidth(*data, WIDTH);
}
component get_current()
{
return component { newtFormGetCurrent(*data), component::ptr_type::no_delete };
}
};
template <component_range... ranges>
[[nodiscard]] inline std::pair<exit_info, form> fast_run(const int COLS, const int ROWS, const std::string_view TITLE, ranges&... components)
{
const grid GRID { COLS, ROWS, components... };
const window WINDOW { GRID, TITLE };
form user_form { components... };
return { user_form.run(), std::move(user_form) };
}
class button : public component {
public:
explicit button(const std::string_view TEXT, const position POS = { 0, 0 }) noexcept
: component(newtButton(POS.left, POS.top, TEXT.data()))
{
}
};
class compact_button : public component {
public:
explicit compact_button(const std::string_view TEXT, const position POS = { 0, 0 }) noexcept
: component(newtCompactButton(POS.left, POS.top, TEXT.data()))
{
}
};
class label : public component {
public:
explicit label(const std::string_view TEXT, const position POS = { 0, 0 }) noexcept
: component(newtLabel(POS.left, POS.top, TEXT.data()))
{
}
void set_text(const std::string_view TEXT)
{
newtLabelSetText(*data, TEXT.data());
}
void set_colors(const int COLOR_SET)
{
newtLabelSetColors(*data, COLOR_SET);
}
};
class entrybox : public component {
const char* content;
public:
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init, hicpp-signed-bitwise) -- content gets initted by newtEntry
explicit entrybox(const int WIDTH, const position POS = { 0, 0 }, std::string_view INITIAL_VALUE = { "" }, const int FLAGS = NEWT_ENTRY_SCROLL) noexcept
: component(newtEntry(POS.left, POS.top, INITIAL_VALUE.data(), WIDTH, &content, FLAGS))
{
}
void set_value(const std::string_view TEXT, const bool CURSOR_AT_END = true)
{
newtEntrySet(*data, TEXT.data(), static_cast<int>(CURSOR_AT_END));
}
std::string_view get_value()
{
return std::string_view { content };
}
/* TODO: Add filter api <29-03-23, Giuseppe> */
};
class checkbox : public component {
public:
explicit checkbox(const std::string_view TEXT, const position POS = { 0, 0 }, const char DEFAULT_VAL = ' ', const std::string_view SEQ = std::string_view {}) noexcept
: component(newtCheckbox(POS.left, POS.top, TEXT.data(), DEFAULT_VAL, SEQ.data(), nullptr))
{
}
char get_value()
{
return newtCheckboxGetValue(*data);
}
void set_value(const char VALUE)
{
newtCheckboxSetValue(*data, VALUE);
}
/* TODO: add set_flags flags <30-03-23, Giuseppe> */
};
class radio_button : public component {
explicit radio_button(newtComponent other, ptr_type::deleter_ptr deleter_func = newtComponentDestroy)
: component(other, deleter_func)
{
}
public:
radio_button()
: component(nullptr, ptr_type::no_delete)
{
}
explicit radio_button(std::string_view TEXT, const position POS = { 0, 0 }, const radio_button& PREVIUS = {}, bool IS_DEFAULT = false) noexcept
: component(newtRadiobutton(POS.left, POS.top, TEXT.data(), static_cast<int>(IS_DEFAULT), *PREVIUS))
{
}
[[nodiscard]] radio_button get_current() const
{
return radio_button { newtRadioGetCurrent(*data), ptr_type::no_delete };
}
void set_current()
{
newtRadioSetCurrent(*data);
}
};
class radio_button_collection {
std::vector<radio_button> collection {};
public:
template <typename... T>
explicit radio_button_collection(T... strings)
{
(add_radio_button(strings), ...);
}
std::span<component> as_range()
{
return std::span { reinterpret_cast<component*>(collection.data()), collection.size() };
}
[[nodiscard]] std::span<const component> as_range() const
{
return std::span { reinterpret_cast<const component*>(collection.data()), collection.size() };
}
[[nodiscard]] size_t get_current_index() const
{
return static_cast<size_t>(std::abs(std::distance(collection.begin(), std::find(collection.begin(), collection.end(), collection.front().get_current()))));
}
void set_current(const size_t INDEX)
{
collection[INDEX].set_current();
}
template <typename... T>
void add_radio_button(std::string_view TEXT, const position POS = { 0, 0 }, const bool DEFAULT = false)
{
collection.empty()
? collection.emplace_back(TEXT, POS, radio_button {}, DEFAULT)
: collection.emplace_back(TEXT, POS, collection.back(), DEFAULT);
}
};
class scale : public component {
public:
scale(const int WIDTH, const long long FULL_VALUE, const position POS = { 0, 0 }) noexcept
: component(newtScale(POS.left, POS.top, WIDTH, FULL_VALUE))
{
}
void set_value(const unsigned long long VALUE)
{
newtScaleSet(*data, VALUE);
}
};
class textbox : public component {
public:
explicit textbox(const size SIZE, const std::string_view TEXT, const position POS = { 0, 0 }, const bool IS_SCROLLABLE = true) noexcept
// NOLINTNEXTLINE
: component(newtTextbox(POS.left, POS.top, SIZE.width, SIZE.height, (IS_SCROLLABLE) ? NEWT_FLAG_SCROLL : 0))
{
newtTextboxSetText(*data, TEXT.data());
}