Skip to content

Commit 7fcea0c

Browse files
committed
add tb_get_cell
1 parent 0b81603 commit 7fcea0c

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

termbox2.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extern "C" {
6565

6666
// __ffi_start
6767

68-
#define TB_VERSION_STR "2.6.0-dev"
68+
#define TB_VERSION_STR "2.7.0-dev"
6969

7070
/* The following compile-time options are supported:
7171
*
@@ -512,6 +512,25 @@ int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg,
512512
uintattr_t bg);
513513
int tb_extend_cell(int x, int y, uint32_t ch);
514514

515+
/* Return a pointer to the cell at the specified position.
516+
*
517+
* Cell memory may be invalid or freed after subsequent library calls, so
518+
* callers must copy any data that they need to persist across calls. Modifying
519+
* cell memory results in undefined behavior.
520+
*
521+
* Callers may use pointer math to access cells relative to the requested one.
522+
* The cell grid memory layout is a contiguous array indexable by the expression
523+
* `(y * width) + x`.
524+
*
525+
* If `back` is non-zero, return cell from the internal back buffer. Otherwise,
526+
* return cell from the front buffer. Note the front buffer is updated on each
527+
* call to `tb_present`, whereas the back buffer is updated immediately by
528+
* `tb_set_cell` and other functions that modify cell contents.
529+
*
530+
* If the position is invalid, `TB_ERR_OUT_OF_BOUNDS` is returned.
531+
*/
532+
int tb_get_cell(int x, int y, int back, struct tb_cell **cell);
533+
515534
/* Set the input mode. Termbox has two input modes:
516535
*
517536
* 1. `TB_INPUT_ESC`
@@ -2505,6 +2524,11 @@ int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg,
25052524
return TB_OK;
25062525
}
25072526

2527+
int tb_get_cell(int x, int y, int back, struct tb_cell **cell) {
2528+
if_not_init_return();
2529+
return cellbuf_get(back ? &global.back : &global.front, x, y, cell);
2530+
}
2531+
25082532
int tb_extend_cell(int x, int y, uint32_t ch) {
25092533
if_not_init_return();
25102534
#ifdef TB_OPT_EGC

tests/test_set_get/expected.ansi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#5a
2+
#5set=0
3+
#5invalid_get=-9
4+
#5back_get=0
5+
#5back_ch=a
6+
#5back_fg=3
7+
#5back_bg=0
8+
#5front1_get=0
9+
#5front1_ch=
10+
#5front1_fg=0
11+
#5front1_bg=0
12+
#5present=0
13+
#5front2_get=0
14+
#5front2_ch=a
15+
#5front2_fg=3
16+
#5front2_bg=0
17+
18+
19+
20+
21+
22+
23+
24+

tests/test_set_get/test.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
$test->ffi->tb_init();
5+
6+
$w = $test->ffi->tb_width();
7+
$h = $test->ffi->tb_height();
8+
$green = $test->defines['TB_GREEN'];
9+
$cellp = $test->ffi->new('struct tb_cell *');
10+
$back = 1;
11+
$front = 0;
12+
13+
$result = [];
14+
15+
$result['set'] = $test->ffi->tb_set_cell(0, 0, ord('a'), $green, 0); // 0 (TB_OK)
16+
17+
$result['invalid_get'] = $test->ffi->tb_get_cell(-1, -1, $back, FFI::addr($cellp)); // -9 (TB_ERR_OUT_OF_BOUNDS)
18+
19+
$result['back_get'] = $test->ffi->tb_get_cell(0, 0, $back, FFI::addr($cellp)); // 0 (TB_OK)
20+
$result['back_ch'] = chr($cellp->ch); // 'a'
21+
$result['back_fg'] = $cellp->fg; // 3 (green)
22+
$result['back_bg'] = $cellp->bg; // 0
23+
24+
$result['front1_get'] = $test->ffi->tb_get_cell(0, 0, $front, FFI::addr($cellp));
25+
$result['front1_ch'] = chr($cellp->ch); // <space> (front buffer empty)
26+
$result['front1_fg'] = $cellp->fg; // 0
27+
$result['front1_bg'] = $cellp->bg; // 0
28+
29+
$result['present'] = $test->ffi->tb_present(); // 0 (TB_OK) (front buffer now populated)
30+
31+
$result['front2_get'] = $test->ffi->tb_get_cell(0, 0, $front, FFI::addr($cellp)); // 0 (TB_OK)
32+
$result['front2_ch'] = chr($cellp->ch); // 'a'
33+
$result['front2_fg'] = $cellp->fg; // 3 (green)
34+
$result['front2_bg'] = $cellp->bg; // 0
35+
36+
$y = 1;
37+
foreach ($result as $k => $v) $test->ffi->tb_printf(0, $y++, 0, 0, '%s=%s', $k, "$v");
38+
39+
$test->ffi->tb_present();
40+
41+
$test->screencap();

0 commit comments

Comments
 (0)