| 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 | /*
|
|---|
| 69 | * History consists of two circular buffers.
|
|---|
| 70 | * The cell buffer is a linear array of character cells.
|
|---|
| 71 | * The line buffer contains offsets into the cell buffer, and segments it into logical lines.
|
|---|
| 72 | * Both of these buffers have a maximum size, so history is limited both by maximum number
|
|---|
| 73 | * of characters, and maximum number of lines.
|
|---|
| 74 | */
|
|---|
| 75 | struct history {
|
|---|
| 76 | size_t viewport_top;
|
|---|
| 77 | size_t row_delta;
|
|---|
| 78 |
|
|---|
| 79 | size_t cols;
|
|---|
| 80 |
|
|---|
| 81 | struct cell_buffer cells;
|
|---|
| 82 | struct line_buffer lines;
|
|---|
| 83 |
|
|---|
| 84 | bool append;
|
|---|
| 85 | };
|
|---|
| 86 |
|
|---|
| 87 | INTERNAL bool _scrollback_active(const struct history *history);
|
|---|
| 88 | INTERNAL void _history_append_row(struct history *history, const termui_cell_t *b, bool last);
|
|---|
| 89 | INTERNAL int _history_viewport_rows(const struct history *history, size_t max);
|
|---|
| 90 | INTERNAL int _history_iter_rows(const struct history *history, int row, int count, termui_update_cb_t cb, void *udata);
|
|---|
| 91 | INTERNAL int _history_scroll(struct history *history, int delta);
|
|---|
| 92 | INTERNAL const termui_cell_t *_history_reflow(struct history *history, size_t new_cols, size_t *recouped);
|
|---|