1 | /*
|
---|
2 | * Copyright (c) 2024 Jiří Zárevúcky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <termui.h>
|
---|
30 | #include <stdbool.h>
|
---|
31 |
|
---|
32 | #define INTERNAL __attribute__((visibility("internal")))
|
---|
33 |
|
---|
34 | static bool _cell_is_empty(const termui_cell_t cell)
|
---|
35 | {
|
---|
36 | return cell.glyph_idx == 0 && cell.bgcolor == 0 && cell.fgcolor == 0 &&
|
---|
37 | cell.padding == 0;
|
---|
38 | }
|
---|
39 |
|
---|
40 | struct cell_buffer {
|
---|
41 | termui_cell_t *buf;
|
---|
42 |
|
---|
43 | size_t head_offset;
|
---|
44 | size_t head_top;
|
---|
45 |
|
---|
46 | /* Tail offset is implicitly zero. */
|
---|
47 | size_t tail_top;
|
---|
48 |
|
---|
49 | size_t buf_len;
|
---|
50 | size_t max_len;
|
---|
51 | };
|
---|
52 |
|
---|
53 | struct history_line {
|
---|
54 | size_t idx;
|
---|
55 | size_t len;
|
---|
56 | };
|
---|
57 |
|
---|
58 | struct line_buffer {
|
---|
59 | struct history_line *buf;
|
---|
60 |
|
---|
61 | size_t head;
|
---|
62 | size_t tail;
|
---|
63 |
|
---|
64 | size_t buf_len;
|
---|
65 | size_t max_len;
|
---|
66 | };
|
---|
67 |
|
---|
68 | struct history {
|
---|
69 | size_t viewport_top;
|
---|
70 | size_t row_delta;
|
---|
71 |
|
---|
72 | size_t cols;
|
---|
73 |
|
---|
74 | struct cell_buffer cells;
|
---|
75 | struct line_buffer lines;
|
---|
76 |
|
---|
77 | bool append;
|
---|
78 | };
|
---|
79 |
|
---|
80 | INTERNAL bool _scrollback_active(const struct history *history);
|
---|
81 | INTERNAL void _history_append_row(struct history *history, const termui_cell_t *b, bool last);
|
---|
82 | INTERNAL int _history_viewport_rows(const struct history *history, size_t max);
|
---|
83 | INTERNAL int _history_iter_rows(const struct history *history, int row, int count, termui_update_cb_t cb, void *udata);
|
---|
84 | INTERNAL int _history_scroll(struct history *history, int delta);
|
---|
85 | INTERNAL const termui_cell_t *_history_reflow(struct history *history, size_t new_cols, size_t *recouped);
|
---|