forked from draios/sysdig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctext.cpp
1373 lines (1143 loc) · 30.3 KB
/
ctext.cpp
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
/*
Copyright (C) 2013-2015 Draios inc.
This file is part of sysdig.
sysdig is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
sysdig is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sysdig. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _WIN32
#include "ctext.h"
#include <unistd.h>
#include <string.h>
#include <algorithm>
#include <climits>
using namespace std;
#define CTEXT_UNDER_X 0x01
#define CTEXT_OVER_X 0x02
#define CTEXT_UNDER_Y 0x04
#define CTEXT_OVER_Y 0x08
#define CTEXT_OVER (CTEXT_OVER_Y | CTEXT_OVER_X)
#define CTEXT_UNDER (CTEXT_UNDER_Y | CTEXT_UNDER_X)
ctext_config config_default;
void search_copy(ctext_search *dst, ctext_search *src)
{
// Because c++ makes life impossibly difficult.
memcpy(dst, src, sizeof(ctext_search) - sizeof(string));
dst->_query = src->_query;
}
ctext::ctext(WINDOW *win, ctext_config *config)
{
this->m_win = win;
config_default.m_buffer_size = CTEXT_DEFAULT_BUFFER_SIZE;
config_default.m_bounding_box = CTEXT_DEFAULT_BOUNDING_BOX;
config_default.m_do_wrap = CTEXT_DEFAULT_DO_WRAP;
config_default.m_append_top = CTEXT_DEFAULT_APPEND_TOP;
config_default.m_scroll_on_append = CTEXT_DEFAULT_SCROLL_ON_APPEND;
config_default.m_auto_newline = CTEXT_DEFAULT_AUTO_NEWLINE;
/*
this->m_debug = new ofstream();
this->m_debug->open("debug1.txt");
*/
this->m_do_draw = true;
if(config)
{
memcpy(&this->m_config, config, sizeof(ctext_config));
}
else
{
memcpy(&this->m_config, &config_default, sizeof(ctext_config));
}
this->m_pos_start.x = this->m_pos_start.y = 0;
this->m_attr_mask = 0;
this->m_last_search = 0;
this->m_event_counter = 0;
this->m_max_y = 0;
// initialized the buffer with the empty row
this->add_row();
}
int8_t ctext::search_off()
{
int8_t ret = (this->m_last_search != 0);
this->m_last_search = 0;
return ret;
}
int8_t ctext::set_config(ctext_config *config)
{
memcpy(&this->m_config, config, sizeof(ctext_config));
return this->redraw();
}
int8_t ctext::get_config(ctext_config *config)
{
return !memcpy(config, &this->m_config, sizeof(ctext_config));
}
int8_t ctext::attach_curses_window(WINDOW *win)
{
this->m_win = win;
return this->redraw();
}
int8_t ctext::highlight(ctext_search *context, int32_t mask)
{
this->m_attr_mask |= mask;
this->redraw_partial(&context->pos, context->_query.size());
this->m_attr_mask &= ~mask;
return 0;
}
int8_t ctext::set_query(ctext_search *p_search, string new_query)
{
this->get_offset(&p_search->pos);
this->get_offset(&p_search->_start_pos);
p_search->_query = new_query;
p_search->_last_match.y = -1;
p_search->_last_event = this->m_event_counter;
p_search->_match_count = 0;
return 0;
}
ctext_search *ctext::new_search(ctext_search *you_manage_this_memory, string to_search, bool is_case_insensitive, bool is_forward, bool do_wrap)
{
ctext_search *p_search = you_manage_this_memory;
if(!p_search)
{
return NULL;
}
p_search->is_case_insensitive = is_case_insensitive;
p_search->do_wrap = do_wrap;
p_search->is_forward = is_forward;
this->set_query(p_search, to_search);
return p_search;
}
int8_t ctext::highlight_matches(ctext_search *to_search)
{
int8_t search_ret;
int32_t mask = A_BOLD | A_UNDERLINE;
if(!to_search)
{
to_search = this->m_last_search;
if(!to_search)
{
return 0;
}
}
// We move the viewport pointer through the current viewport,
// highlighting all matching instances.
ctext_search in_viewport;
ctext_pos limit;
search_copy(&in_viewport, to_search);
// We will say the limit is the viewport height ... this makes sure we go over
// the maximum extent possible. We also make sure we do this after our first match
// otherwise this would reflect our current viewport, shameful!
limit.y = min(this->m_pos_start.y + this->m_win_height, (int32_t)this->m_buffer.size());
// Now we iterate through the viewport highlighting all of the instances, using the
// limit and the in_viewport pointer
if(this->m_event_counter != in_viewport._last_event)
{
search_ret = this->str_search_single(&in_viewport, &in_viewport, &limit);
}
do
{
this->highlight(&in_viewport, mask);
search_ret = this->str_search_single(&in_viewport, &in_viewport, &limit);
mask = A_REVERSE;
} while(search_ret >= 0);
return 0;
}
int8_t ctext::str_search(ctext_search *to_search)
{
int8_t search_ret, scroll_ret;
this->m_last_search = to_search;
// This makes sure that we scroll to a new y row
// if multiple matches are on the same viewport row.
for(;;)
{
search_ret = this->str_search_single(to_search, to_search);
if(search_ret == -1)
{
break;
}
if(this->m_config.m_do_wrap)
{
scroll_ret = this->direct_scroll(&to_search->pos);
}
else
{
scroll_ret = this->direct_scroll(0, to_search->pos.y);
}
// This makes sure we move forward ... but we only do this
// if we didn't push the event forward
if(!scroll_ret || to_search->_match_count == 1)
{
break;
}
}
// This means that it was found somewhere and our
// pointer has been moved forward
if(search_ret >= 0)
{
// We can do a general scroll_to and redraw.
this->redraw();
}
return search_ret;
}
int8_t ctext::str_search_single(ctext_search *to_search_in, ctext_search *new_pos_out, ctext_pos *limit)
{
int32_t size = (int32_t)this->m_buffer.size();
size_t found;
string haystack;
ctext_search res, *out;
if(!to_search_in)
{
return -1;
}
string query = to_search_in->_query;
if(!new_pos_out)
{
search_copy(&res, to_search_in);
out = &res;
}
else
{
out = new_pos_out;
}
// If a (scroll) event has happened since we last ran this,
// then we need to update exactly where we want to start
// the search from.
if(to_search_in->_last_event < this->m_event_counter)
{
out->pos.y = this->m_pos_start.y;
out->pos.x = this->m_pos_start.x;
out->_last_event = this->m_event_counter;
out->_match_count = 0;
}
if(to_search_in->is_case_insensitive)
{
transform(query.begin(), query.end(), query.begin(), ::tolower);
}
for(;;)
{
haystack = this->m_buffer[out->pos.y].data;
if(to_search_in->is_case_insensitive)
{
transform(haystack.begin(), haystack.end(), haystack.begin(), ::tolower);
}
if(out->is_forward)
{
found = haystack.find(query, (size_t)( (out->pos.x == -2) ? out->pos.x + 2 : out->pos.x + 1));
}
else
{
found = haystack.rfind(query, (size_t)( (out->pos.x == (int32_t)haystack.size()) ? out->pos.x : out->pos.x - 1));
}
if(found == string::npos)
{
if(out->is_forward)
{
out->pos.y = (out->pos.y + 1) % size;
out->pos.x = -2;
}
else
{
out->pos.y--;
// Wrap if we are going backwards.
if(out->pos.y == -1)
{
out->pos.y = size - 1;
}
out->pos.x = (int32_t)this->m_buffer[out->pos.y].data.size();
}
//
// The edge case here is if there are no matches and we ARE wrapping,
// we don't want the idiot case of going through the haystack endlessly
// like a chump and locking up the application.
//
if(
(out->pos.y == out->_start_pos.y && (out->do_wrap == false || out->_last_match.y == -1)) ||
(limit && out->pos.y > limit->y)
)
{
return -1;
}
}
else
{
// This is all we really care about, we don't need
// to look at the x value
out->_last_match.y = out->pos.y;
out->pos.x = (int32_t)found;
out->_match_count++;
break;
}
}
return 0;
}
int32_t ctext::clear(int32_t row_count)
{
int32_t ret = 0;
if(row_count == -1)
{
ret = this->m_buffer.size();
this->m_buffer.clear();
this->add_row();
}
else if(this->m_buffer.size())
{
ret = this->m_buffer.size();
this->m_buffer.erase(this->m_buffer.begin(), this->m_buffer.begin() + row_count);
ret -= this->m_buffer.size();
}
// We do the same logic when removing content
// .. perhaps forcing things down or upward
if(this->m_config.m_scroll_on_append)
{
this->get_win_size();
// Now we force it.
this->direct_scroll(0, this->m_buffer.size() - this->m_win_height);
}
this->redraw();
return ret;
}
int8_t ctext::ob_start()
{
int8_t ret = this->m_do_draw;
this->m_do_draw = false;
return ret;
}
int8_t ctext::ob_end()
{
int8_t ret = !this->m_do_draw;
this->m_do_draw = true;
this->redraw();
return ret;
}
int8_t ctext::direct_scroll(ctext_pos*p)
{
return this->direct_scroll(p->x, p->y);
}
int8_t ctext::direct_scroll(int32_t x, int32_t y)
{
ctext_pos start;
memcpy(&start, &this->m_pos_start, sizeof(ctext_pos));
this->get_win_size();
if(this->m_config.m_bounding_box)
{
y = min(y, this->m_max_y - this->m_win_height);
x = max(0, x);
y = max(0, y);
}
// Under this context we should only be x-scrolling
// to a modulus of a windows width
if(this->m_config.m_do_wrap)
{
// We always go *under* to make sure that the
// content appears in the viewport.
x -= x % this->m_win_width;
}
this->m_pos_start.x = x;
this->m_pos_start.y = y;
// If the values have changed and we have actively scrolled,
// return 0, otherwise return -1.
return (start.x != x || start.y != y) ? 0 : -1;
}
int8_t ctext::scroll_to(ctext_pos *pos)
{
return this->scroll_to(pos->x, pos->y);
}
int8_t ctext::scroll_to(int32_t x, int32_t y)
{
this->direct_scroll(x, y);
this->m_event_counter++;
return this->redraw();
}
int8_t ctext::get_offset(ctext_pos *pos)
{
return this->get_offset(&pos->x, &pos->y);
}
int8_t ctext::get_offset(int32_t*x, int32_t*y)
{
*x = this->m_pos_start.x;
*y = this->m_pos_start.y;
return 0;
}
int8_t ctext::get_offset_percent(float*percent)
{
this->get_win_size();
*percent = (float)(this->m_pos_start.y) / (this->m_max_y - this->m_win_height);
return 0;
}
int8_t ctext::get_buf_size(int32_t*buf_size)
{
*buf_size = this->m_max_y;
return 0;
}
int32_t ctext::available_rows()
{
// Since our buffer clearing scheme permits us to overflow,
// we have to bind this to make sure that we return >= 0 values
if(this->m_config.m_buffer_size == -1)
{
return (int32_t)LONG_MAX;
}
return max(this->m_config.m_buffer_size - this->m_max_y - 1, 0);
}
int32_t ctext::up(int32_t amount)
{
return this->down(-amount);
}
int32_t ctext::page_down(int32_t page_count)
{
this->get_win_size();
return this->down(page_count * this->m_win_height);
}
int32_t ctext::page_up(int32_t page_count)
{
this->get_win_size();
return this->down(-page_count * this->m_win_height);
}
// Let's do this real fast.
int8_t ctext::map_to_win(int32_t buffer_x, int32_t buffer_y, ctext_pos*win)
{
int8_t ret = 0;
// This is the trivial case.
if(!this->m_config.m_do_wrap)
{
// These are trivial.
win->y = buffer_y - this->m_pos_start.y;
win->x = buffer_x - this->m_pos_start.x;
return ((buffer_x < this->m_pos_start.x)) | ((buffer_x > this->m_pos_start.x + this->m_win_width) << 1)
| ((buffer_y < this->m_pos_start.y) << 2) | ((buffer_y > this->m_pos_start.y + this->m_win_height) << 3);
}
// Otherwise it's much more challenging.
else
{
// If we are below the fold or we are at the first line and before
// the start of where we ought to be drawing
if(buffer_y < this->m_pos_start.y || (buffer_y == this->m_pos_start.y && buffer_x < this->m_pos_start.x))
{
// We omit win calculations here since they
// would be more expensive then we'd like
win->x = win->y = -1;
ret |= CTEXT_UNDER_Y;
}
else
{
// To see if it's an overflow y is a bit harder
int32_t new_y = this->m_pos_start.y;
int32_t new_offset = this->m_pos_start.x;
string *data = &this->m_buffer[new_y].data;
for(win->y = 0; win->y < this->m_win_height; win->y++)
{
new_offset += this->m_win_width;
//
// There's an edge case that requires this
// twice due to a short circuit exit that
// would be triggered at the end of a buffer,
// see below.
//
if(buffer_y == new_y && buffer_x < new_offset)
{
win->x = buffer_x - (new_offset - this->m_win_width);
return ret;
}
if(new_offset > (int32_t)data->size())
{
new_offset = 0;
new_y ++;
if(new_y > this->m_max_y)
{
break;
}
data = &this->m_buffer[new_y].data;
}
if(
// We've passed it and we know it's not
// an underflow, so we're done.
(buffer_y < new_y) ||
// We are at the end line and our test point
// is below the offset that we just flew past
(buffer_y == new_y && buffer_x < new_offset)
)
{
win->x = buffer_x - (new_offset - this->m_win_width);
return ret;
}
}
// Keep the y at the end
// win->y = -1;
// If we get here that means that we went all the way through
// a "generation" and didn't reach our hit point ... that means
// it's an overflow.
ret |= CTEXT_OVER_Y;
}
}
return ret;
}
int8_t ctext::y_scroll_calculate(int32_t amount, ctext_pos *pos)
{
if(this->m_config.m_do_wrap)
{
int32_t new_y = this->m_pos_start.y;
int32_t new_offset = this->m_pos_start.x;
ctext_row *p_row = &this->m_buffer[this->m_pos_start.y];
this->get_win_size();
while(amount > 0)
{
new_offset += this->m_win_width;
amount --;
if(new_offset > (int32_t)p_row->data.size())
{
if((new_y + this->m_win_height + 1) >= (int32_t)this->m_buffer.size())
{
//
// This means that forwarding our buffer was a mistake
// so we undo our work and get out of here.
//
new_offset -= this->m_win_width;
break;
}
new_offset = 0;
new_y++;
p_row = &this->m_buffer[new_y];
}
}
while(amount < 0)
{
new_offset -= this->m_win_width;
amount ++;
if(new_offset < 0)
{
if(new_y - 1 < 0)
{
break;
}
new_y--;
p_row = &this->m_buffer[new_y];
new_offset = p_row->data.size() - p_row->data.size() % this->m_win_width;
}
}
pos->x = new_offset;
pos->y = new_y;
}
else
{
pos->x = this->m_pos_start.x;
pos->y = this->m_pos_start.y + amount;
}
return 0;
}
int32_t ctext::down(int32_t amount)
{
ctext_pos new_pos;
this->y_scroll_calculate(amount, &new_pos);
return this->scroll_to(&new_pos);
}
int32_t ctext::jump_to_first_line()
{
int32_t current_line = this->m_pos_start.y;
//
// Now we try to scroll above the first
// line. the bounding box rule will
// take care of the differences for us.
//
this->scroll_to(this->m_pos_start.x, 0 - this->m_win_height + 1);
return current_line - this->m_pos_start.y;
}
int32_t ctext::jump_to_last_line()
{
int32_t current_line = this->m_pos_start.y;
this->get_win_size();
this->scroll_to(this->m_pos_start.x, this->m_max_y - 1);
return current_line - this->m_pos_start.y;
}
int32_t ctext::left(int32_t amount)
{
return this->right(-amount);
}
int32_t ctext::right(int32_t amount)
{
return this->scroll_to(this->m_pos_start.x + amount, this->m_pos_start.y);
}
void ctext::get_win_size()
{
int32_t width = 0, height = 0;
if(this->m_win)
{
getmaxyx(this->m_win, height, width);
}
this->m_win_width = width;
this->m_win_height = height;
}
int8_t ctext::rebuf()
{
// Memory management is expensive, so we only do this occasionally
if(this->m_config.m_buffer_size != -1 && (int32_t)this->m_buffer.size() > (this->m_config.m_buffer_size * 11 / 10))
{
this->m_buffer.erase(this->m_buffer.begin(), this->m_buffer.end() - this->m_config.m_buffer_size);
}
this->m_max_y = this->m_buffer.size() - 1;
//
// Since we've changed the bounding box of the content we have to
// issue a rescroll on exactly our previous parameters. This may
// force us inward or may retain our position.
//
return this->direct_scroll(this->m_pos_start.x, this->m_pos_start.y);
}
void ctext::add_format_if_needed()
{
attr_t attrs;
int16_t color_pair;
if(!this->m_win)
{
return;
}
if(this->m_buffer.empty())
{
return;
}
// Get the most current row.
ctext_row *p_row = &this->m_buffer.back();
ctext_format p_format = {0,0,0};
if(!p_row->format.empty())
{
// And the most current format
p_format = p_row->format.back();
}
wattr_get(this->m_win, &attrs, &color_pair, 0);
if(attrs != p_format.attrs || color_pair != p_format.color_pair)
{
// Our properties have changed so we need to record this.
ctext_format new_format;
// This is our offset
new_format.offset = (int32_t)p_row->data.size();
new_format.attrs = attrs;
new_format.color_pair = color_pair;
//
// If the new thing we are adding has the same
// offset as the previous, then we dump the
// previous.
//
if(p_format.offset == new_format.offset && !p_row->format.empty())
{
p_row->format.pop_back();
}
p_row->format.push_back(new_format);
}
}
ctext_row* ctext::add_row()
{
ctext_row row;
// If there is an exsting line, then
// we carry over the format from the
// last line..
if(!this->m_buffer.empty())
{
ctext_row p_row = this->m_buffer.back();
if(!p_row.format.empty())
{
ctext_format p_format( p_row.format.back() );
// Set the offset to the initial.
p_format.offset = 0;
row.format.push_back(p_format);
}
}
this->m_buffer.push_back(row);
return &this->m_buffer.back();
}
char* next_type(char* search, const char delim)
{
while(*search && *search != delim)
{
search ++;
}
return search;
}
int8_t ctext::vprintf(const char*format, va_list ap)
{
char *p_line, *n_line;
char large_buffer[CTEXT_BUFFER_SIZE];
this->add_format_if_needed();
ctext_row *p_row = &this->m_buffer.back();
vsnprintf(large_buffer, CTEXT_BUFFER_SIZE, format, ap);
p_line = large_buffer;
do
{
n_line = next_type(p_line, '\n');
string wstr(p_line, n_line - p_line);
p_row->data += wstr;
if(*n_line)
{
p_row = this->add_row();
}
p_line = n_line + 1;
}
while (*n_line);
if(this->m_config.m_auto_newline)
{
this->add_row();
}
// Since we are adding content we need to see if we are
// to force on scroll.
if(this->m_config.m_scroll_on_append)
{
this->get_win_size();
// Now we force it.
this->direct_scroll(0, this->m_buffer.size() - this->m_win_height);
}
return this->redraw();
}
int cprintf(ctext*win, const char *format, ...)
{
int ret;
va_list args;
va_start(args, format);
ret = win->vprintf(format, args);
va_end(args);
return ret;
}
int8_t ctext::printf(const char*format, ...)
{
int8_t ret;
va_list args;
va_start(args, format);
ret = this->vprintf(format, args);
va_end(args);
return ret;
}
int8_t ctext::nprintf(const char*format, ...)
{
int8_t ret;
va_list args;
// First turn off the rerdaw flag
this->ob_start();
// Then call the variadic version
va_start(args, format);
ret = this->vprintf(format, args);
va_end(args);
//
// Then manually untoggle the flag
// (this is necessary because ob_end
// does TWO things, breaking loads of
// anti-patterns I'm sure.)
//
this->m_do_draw = true;
return ret;
}
#if 0
int8_t ctext::redraw_partial_test()
{
attr_t res_attrs;
int16_t res = 0;
int16_t res_color_pair;
int32_t x, y, end_x;
string *data;
this->get_win_size();
wattr_get(this->m_win, &res_attrs, &res_color_pair, 0);
wattr_off(this->m_win, COLOR_PAIR(res_color_pair), 0);
this->rebuf();
werase(this->m_win);
x = this->m_pos_start.x;
y = this->m_pos_start.y;
data = &this->m_buffer[y].data;
while(!(res & CTEXT_OVER_Y))
{
this->m_attr_mask ^= A_REVERSE;
end_x = min(x + rand() % 9 + 1, (int32_t)data->size());
res = this->redraw_partial(x, y, end_x, y);
wrefresh(this->m_win);
usleep(1000 * 500);
x = end_x;
if(end_x == (int32_t)data->size() || (res & CTEXT_OVER_X))
{
y++;
x = 0;
if(y > this->m_max_y)
{
break;
}
data = &this->m_buffer[y].data;
}
}
wrefresh(this->m_win);
wattr_set(this->m_win, res_attrs, res_color_pair, 0);
return 0;
}
#endif
int16_t ctext::redraw_partial(ctext_pos *pos, size_t len)
{
return this->redraw_partial(pos->x, pos->y, pos->x + len, pos->y);
}
//
// redraw_partial takes a buffer offset and sees if it is
// to be drawn within the current view port, which is specified
// by m_pos_start.x and m_pos_start.y.
//
int16_t ctext::redraw_partial(
int32_t buf_start_x, int32_t buf_start_y,
int32_t buf_end_x, int32_t buf_end_y)
{
bool b_format = false;
string to_add;
ctext_row *p_source;
vector<ctext_format>::iterator p_format;
bool is_first_line = true;
int16_t ret = 0;
int32_t num_added_x = 0;
int32_t num_to_add = 0;
int32_t win_current_x;
int32_t win_current_end_x;
int32_t buf_offset_x;
// We need to get relative start and end positions.
ctext_pos win_start, win_end;
buf_start_x = max(0, buf_start_x);
ret = this->map_to_win(buf_start_x, buf_start_y, &win_start);
// This means that none of this will map to screen,
// return the overflow and bail.
if(ret & CTEXT_OVER_Y)
{
return ret;
}
ret = this->map_to_win(buf_end_x, buf_end_y, &win_end);
// This also means that none of this will map to screen,
// return the underflow and bail.
if(ret & CTEXT_UNDER_Y)
{
return ret;
}
//
// We start as m_pos_start.y in our list and move up to
// m_pos_start.y + m_win_height except in the case of
// wrap around. Because of this special case,
// we compute when to exit slightly differently.
//
// This is the current line of output, which stays
// below m_win_height
//
int32_t win_current_y = win_start.y;
int32_t buf_current_y = buf_start_y;
// This is for horizontal scroll.
int32_t start_char = max(0, this->m_pos_start.x);
while(win_current_y <= win_end.y)
{