C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
Manage View Display

Manage the View Display. More...

Functions

void display_line (View *view)
 Display Line on Padparam View *view data structure.
int display_prompt (View *view, char *s)
 Display Command Line Prompt.
int fmt_line (View *view)
 Format Line for Display.
int pad_refresh (View *view)
 Refresh Pad and Line Number Window.
void parse_ansi_str (char *ansi_str, attr_t *attr, int *cpx)
 Parse ANSI SGR Escape Sequence.
void view_display_help (Init *init)
 Display View Help File.
void view_display_page (View *view)
 Display Current Page.

Detailed Description

Manage the View Display.

Function Documentation

◆ display_line()

void display_line ( View * view)

Display Line on Padparam View *view data structure.

This function displays a single line of text on the ncurses pad. If line numbering is enabled (view->f_ln), it is formatted and displayed at the beginning of the line with the specified attributes and color pair.

Because get_next_char calls increment_ln upon encountering a line feed and increment_ln advances view->ln after updating the line table, the line number displayed is one greater than the index to the line table. That means the line counter begins with 1, while the table origin is 0.

Definition at line 1746 of file view_engine.c.

1746 {
1747 char ln_s[16];
1748
1749 if (view->cury < 0)
1750 view->cury = 0;
1751 if (view->cury > view->scroll_lines - 1)
1752 view->cury = view->scroll_lines - 1;
1753 if (view->f_ln) {
1754 ssnprintf(ln_s, 8, "%7jd", view->ln);
1755 wmove(view->lnno_win, view->cury, 0);
1756 wclrtoeol(view->lnno_win);
1757 mvwaddstr(view->lnno_win, view->cury, 0, ln_s);
1758 }
1759 wmove(view->pad, view->cury, 0);
1760 wclrtoeol(view->pad);
1761 wadd_wchstr(view->pad, view->cmplx_buf);
1762 view->cury++;
1763}
View * view
Definition mem.c:49
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:420

References View::cmplx_buf, View::cury, View::f_ln, View::ln, View::lnno_win, View::pad, View::scroll_lines, and ssnprintf().

Referenced by scroll_down_n_lines(), scroll_up_n_lines(), search(), and view_display_page().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ display_prompt()

int display_prompt ( View * view,
char * s )

Display Command Line Prompt.

Parameters
viewis the current view data structure
sis the prompt string

Definition at line 2036 of file view_engine.c.

2036 {
2037 char message_str[PAD_COLS + 1];
2038 int l;
2039 l = strnz__cpy(message_str, s, PAD_COLS);
2040 wmove(view->cmdln_win, view->cmd_line, 0);
2041 if (l != 0) {
2042 wclrtoeol(view->cmdln_win);
2043 // wbkgrndset(view->cmdln_win, &CC_NT_HL_REV);
2044 // mvwadd_wchnstr(view->cmdln_win, view->cmd_line, 0, &ran, 1);
2045 // mvwadd_wchnstr(view->cmdln_win, view->cmd_line, 0, &sp, 1);
2046 wbkgrndset(view->cmdln_win, &CC_NT_REV);
2047 mvwaddstr(view->cmdln_win, view->cmd_line, 0, " ");
2048 mvwaddstr(view->cmdln_win, view->cmd_line, 1, message_str);
2049 waddstr(view->cmdln_win, " ");
2050 wbkgrndset(view->cmdln_win, &CC_NT);
2051 getyx(view->cmdln_win, view->cmd_line, view->curx);
2052 wmove(view->cmdln_win, view->cmd_line, view->curx);
2053 }
2054 return (view->curx);
2055}
cchar_t CC_NT
Definition dwin.c:204
cchar_t CC_NT_REV
Definition dwin.c:205
#define PAD_COLS
Definition view.h:32
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition futil.c:544

References CC_NT, CC_NT_REV, View::cmd_line, View::cmdln_win, View::curx, and strnz__cpy().

Referenced by get_cmd_char(), lp(), new_view_file(), and view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fmt_line()

int fmt_line ( View * view)

Format Line for Display.

Parameters
viewpointer to View structure containing line input and output buffers
Returns
length of formatted line in characters

This function processes the input line from view->line_in_s, handling ANSI escape sequences for text attributes and colors, as well as multi-byte characters. It converts the input line into a formatted line suitable for display in the terminal, storing the result in view->cmplx_buf and view->stripped_line_out. The function returns the length of the formatted line in characters, which may be used for tracking the maximum column width of the displayed content.

This section calls the decoder for ANSI escape sequences

This section converts the input string to wide characters, handling tabs and multi-byte characters, and stores the result in the complex buffer for display. ANSI escape sequences are ignored in this section since they are handled separately above.

Definition at line 1777 of file view_engine.c.

1777 {
1778 char ansi_tok[MAXLEN];
1779 int i = 0, j = 0;
1780 int len = 0;
1781 const char *s;
1782 attr_t attr = WA_NORMAL;
1783 int cpx = cp_nt;
1784 cchar_t cc = {0};
1785 wchar_t wstr[2] = {L'\0', L'\0'};
1786
1787 char *in_str = view->line_in_s;
1788 cchar_t *cmplx_buf = view->cmplx_buf;
1789
1790 rtrim(view->line_out_s);
1791 mbstate_t mbstate;
1792 memset(&mbstate, 0, sizeof(mbstate));
1793 while (in_str[i] != '\0') {
1794 if (in_str[i] == '\033' && in_str[i + 1] == '[') {
1796 len = strcspn(&in_str[i], "mK ") + 1;
1797 memcpy(ansi_tok, &in_str[i], len + 1);
1798 ansi_tok[len] = '\0';
1799 if (ansi_tok[0] == '\0') {
1800 if (i + 2 < MAXLEN)
1801 i += 2;
1802 continue;
1803 }
1804 if (len == 0 || in_str[i + len - 1] == ' ') {
1805 i += 2;
1806 continue;
1807 } else if (in_str[i + len - 1] == 'K') {
1808 i += len;
1809 continue;
1810 }
1811 parse_ansi_str(ansi_tok, &attr, &cpx);
1812 i += len;
1813 } else {
1819 if (in_str[i] == '\033') {
1820 i++;
1821 continue;
1822 }
1823 s = &in_str[i];
1824 if (*s == '\t') {
1825 do {
1826 wstr[0] = L' ';
1827 wstr[1] = L'\0';
1828 setcchar(&cc, wstr, attr, cpx, nullptr);
1829 view->stripped_line_out[j] = ' ';
1830 cmplx_buf[j++] = cc;
1831 } while ((j < PAD_COLS - 2) && (j % view->tab_stop != 0));
1832 i++;
1833 } else {
1834 wstr[1] = L'\0';
1835 len = mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
1836 if (len <= 0) {
1837 wstr[0] = L'?';
1838 wstr[1] = L'\0';
1839 len = 1;
1840 }
1841 if (setcchar(&cc, wstr, attr, cpx, nullptr) != ERR) {
1842 if (len > 0 && (j + len) < PAD_COLS - 1) {
1843 view->stripped_line_out[j] = *s;
1844 cmplx_buf[j++] = cc;
1845 }
1846 }
1847 i += len;
1848 }
1849 }
1850 }
1851 if (j > view->maxcol)
1852 view->maxcol = j;
1853 wstr[0] = '\0';
1854 wstr[1] = '\0';
1855 setcchar(&cc, wstr, WA_NORMAL, cpx, nullptr);
1856 cmplx_buf[j] = cc;
1857 view->stripped_line_out[j] = '\0';
1858 return j;
1859}
size_t rtrim(char *)
Trims trailing spaces from string s in place.
Definition futil.c:338
int cp_nt
Definition dwin.c:183
char in_str[BUFSIZ+1]
Definition iloan.c:20
#define MAXLEN
Definition curskeys.c:15
void parse_ansi_str(char *, attr_t *, int *)
Parse ANSI SGR Escape Sequence.

References View::cmplx_buf, cp_nt, View::line_in_s, View::line_out_s, View::maxcol, parse_ansi_str(), rtrim(), View::stripped_line_out, and View::tab_stop.

Referenced by scroll_down_n_lines(), scroll_up_n_lines(), search(), and view_display_page().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pad_refresh()

int pad_refresh ( View * view)

Refresh Pad and Line Number Window.

Parameters
viewdata structure
Returns
OK on success, ERR on failure

Definition at line 1280 of file view_engine.c.

1280 {
1281 int rc;
1282 touchwin(view->pad);
1283 rc = pnoutrefresh(view->pad, view->pminrow, view->pmincol, view->sminrow,
1284 view->smincol, view->smaxrow, view->smaxcol);
1285 rc = prefresh(view->pad_view_win,
1286 view->pminrow,
1287 view->pmincol,
1288 view->sminrow,
1289 view->smincol,
1290 view->smaxrow,
1291 view->smaxcol);
1292 if (rc == ERR) {
1293 ssnprintf(em0, MAXLEN - 1, "%s:%d prefresh(view->pad_view_win, pminrow=%d, pmincol=%d, smaxrow=%d, smaxcol=%d) returned %d\n",
1294 __FILE__, __LINE__, view->pminrow, view->pmincol, view->smaxrow, view->smaxcol, rc);
1295 Perror(em0);
1296 }
1297 return rc;
1298}
char em0[MAXLEN]
Definition dwin.c:175
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1526

References em0, View::pad, View::pad_view_win, Perror(), View::pmincol, View::pminrow, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, and ssnprintf().

Referenced by get_cmd_arg(), get_cmd_char(), new_view_file(), and view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_ansi_str()

void parse_ansi_str ( char * ansi_str,
attr_t * attr,
int * cpx )

Parse ANSI SGR Escape Sequence.

Parameters
ansi_stris the ANSI escape sequence string to parse
attris a pointer to an attr_t variable where the parsed attributes will be stored
cpxis a pointer to an int variable where the parsed color pair index will be stored

This function parses an ANSI escape sequence and updates the color pair and color tables according to the attributes specified. Despite the depth of nested conditionals, with an understanding of the ANSI Select Graphics Rendition (SGR) scheme, which is defined in the ECMA-48 standard (ISO/IEC 6429), you will find that it is exceptionally simple and straightforward.

 This function converts the following SGR specification types to the
appropriate curses color pair index for use in the terminal display.

 RGB:

     foreground \033[38;2;r;g;bm
     background \033[48;2;r;g;bm

     Where r, g, b are the red, green, and blue color components
(0-255)

 XTERM 256-color:

     foreground \033[38;5;xm
     background \033[48;5;xm

     Where x is the 256-color index (0-255)

     uses xterm256_idx_to_rgb() to convert the 256-color index to
RGB

 8-color:

     foreground \033[3cm
     background \033[4cm

     Where c is the color code (0 for black, 1 for red, 2 for green, 3
for yellow, 4 for blue, 5 for magenta, 6 for cyan, 7 for white).

 Attributes:

     \033[am

     Where a is the attribute code (1 for bold, 2 for dim, 3 for italic,
4 for underline, 5 for blink, 7 for reverse, 8 for invis). The function
also supports resetting attributes and colors to default using \033[0m.

 @sa xterm256_idx_to_rgb(), rgb_to_curses_clr(), extended_pair_content(),
get_clr_pair()

Definition at line 1918 of file view_engine.c.

1918 {
1919 char *tok;
1920 char t0, t1;
1921 char tstr[3];
1922 int len, x_idx;
1923 int fg, bg;
1924 int fg_clr, bg_clr;
1925 char *ansi_p = ansi_str + 2;
1926 extended_pair_content(*cpx, &fg_clr, &bg_clr);
1927 fg = fg_clr;
1928 bg = bg_clr;
1929 RGB rgb;
1930 tok = strtok((char *)ansi_p, ";m");
1931 bool a_toi_error = false;
1932 while (1) {
1933 if (tok == nullptr || *tok == '\0')
1934 break;
1935 len = strlen(tok);
1936 if (len == 2) {
1937 t0 = tok[0];
1938 t1 = tok[1];
1939 if (t0 == '3' || t0 == '4') {
1940 if (t1 == '8') {
1941 tok = strtok(nullptr, ";m");
1942 if (tok != nullptr) {
1943 if (*tok == '5') {
1944 tok = strtok(nullptr, ";m");
1945 if (tok != nullptr) {
1946 x_idx = a_toi(tok, &a_toi_error);
1947 rgb = xterm256_idx_to_rgb(x_idx);
1948 }
1949 } else if (*tok == '2') {
1950 tok = strtok(nullptr, ";m");
1951 rgb.r = a_toi(tok, &a_toi_error);
1952 tok = strtok(nullptr, ";m");
1953 rgb.g = a_toi(tok, &a_toi_error);
1954 tok = strtok(nullptr, ";m");
1955 rgb.b = a_toi(tok, &a_toi_error);
1956 }
1957 }
1958 if (t0 == '3')
1959 fg_clr = rgb_to_curses_clr(&rgb);
1960 else if (t0 == '4')
1961 bg_clr = rgb_to_curses_clr(&rgb);
1962 } else if (t1 == '9') {
1963 if (t0 == '3')
1964 fg_clr = CLR_NT_FG;
1965 else if (t0 == '4')
1966 bg_clr = CLR_NT_BG;
1967 } else if (t1 >= '0' && t1 <= '7') {
1968 if (t0 == '3') {
1969 tstr[0] = t1;
1970 tstr[1] = '\0';
1971 x_idx = a_toi(tstr, &a_toi_error);
1972 rgb = xterm256_idx_to_rgb(x_idx);
1973 fg_clr = rgb_to_curses_clr(&rgb);
1974 } else if (t0 == '4') {
1975 tstr[0] = t1;
1976 tstr[1] = '\0';
1977 x_idx = a_toi(tstr, &a_toi_error);
1978 rgb = xterm256_idx_to_rgb(x_idx);
1979 bg_clr = rgb_to_curses_clr(&rgb);
1980 }
1981 }
1982 } else if (t0 == '0') {
1983 *tok = t1;
1984 len = 1;
1985 }
1986 }
1987 if (len == 1) {
1988 if (*tok == '0') {
1989 *attr = WA_NORMAL;
1990 fg_clr = CLR_NT_FG;
1991 bg_clr = CLR_NT_BG;
1992 } else {
1993 switch (a_toi(tok, &a_toi_error)) {
1994 case 1:
1995 *attr |= WA_BOLD;
1996 break;
1997 case 2:
1998 *attr |= WA_DIM;
1999 break;
2000 case 3:
2001 *attr |= WA_ITALIC;
2002 break;
2003 case 4:
2004 *attr |= WA_UNDERLINE;
2005 break;
2006 case 5:
2007 *attr |= WA_BLINK;
2008 break;
2009 case 7:
2010 *attr |= WA_REVERSE;
2011 break;
2012 case 8:
2013 *attr |= WA_INVIS;
2014 break;
2015 default:
2016 break;
2017 }
2018 }
2019 } else if (len == 0) {
2020 *attr = WA_NORMAL;
2021 fg_clr = CLR_NT_FG;
2022 bg_clr = CLR_NT_BG;
2023 }
2024 tok = strtok(nullptr, ";m");
2025 }
2026 if (!a_toi_error && (fg_clr != fg || bg_clr != bg)) {
2027 clr_pair_idx = get_clr_pair(fg_clr, bg_clr);
2028 *cpx = clr_pair_idx;
2029 }
2030 return;
2031}
int clr_pair_idx
Definition dwin.c:196
@ CLR_NT_FG
Definition cm.h:188
@ CLR_NT_BG
Definition cm.h:189
RGB xterm256_idx_to_rgb(int)
Convert XTerm 256 color index to RGB.
Definition dwin.c:483
int rgb_to_curses_clr(RGB *)
Get color index for RGB color.
Definition dwin.c:432
int get_clr_pair(int, int)
Get color pair index for foreground and background colors.
Definition dwin.c:401
int a_toi(char *, bool *)
a safer alternative to atoi() for converting ASCII strings to integers.
Definition futil.c:769
Definition cm.h:381
int r
Definition cm.h:382
int b
Definition cm.h:382
int g
Definition cm.h:382

References a_toi(), RGB::b, CLR_NT_BG, CLR_NT_FG, clr_pair_idx, RGB::g, get_clr_pair(), RGB::r, rgb_to_curses_clr(), and xterm256_idx_to_rgb().

Referenced by fmt_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_display_help()

void view_display_help ( Init * init)

Display View Help File.

Parameters
initis the current initialization data structure.

The current View context is set aside by assigning the view structure to "view_save" while the help file is displayed using a new, separate view structure. The help file is specified by the VIEW_HELP_FILE macro can be set to a default help file path or overridden by the user through an environment variable. After the help file is closed, the original view is restored and the page is redisplayed. It may be necessary to reassign view after calling this function because the init->view pointer is temporarily set to nullptr during the help file display, and the original view is restored afterward. The default screen size for help can be set in the code below. If set to 0, popup_view will determine reasonable maximal size based on the terminal dimensions. The help file may contain Unicode characters and ANSI escape sequences for formatting, which will be properly handled and displayed by popup_view.

Definition at line 2093 of file view_engine.c.

2093 {
2094 char tmp_str[MAXLEN];
2095 int eargc = 0;
2096 char *eargv[MAXARGS];
2097 View *view = init->view;
2098 if (view->f_help_spec && view->help_spec[0] != '\0')
2099 strnz__cpy(tmp_str, view->help_spec, MAXLEN - 1);
2100 else {
2101 strnz__cpy(tmp_str, init->mapp_help, MAXLEN - 1);
2102 strnz__cat(tmp_str, "/", MAXLEN - 1);
2103 strnz__cat(tmp_str, VIEW_HELP_FILE, MAXLEN - 1);
2104 }
2105 eargv[eargc++] = strdup("view");
2106 eargv[eargc++] = strdup("-N");
2107 eargv[eargc++] = strdup("f");
2108 eargv[eargc++] = strdup(tmp_str);
2109 eargv[eargc] = nullptr;
2110 init->lines = 48;
2111 init->cols = 72;
2112 init->begy = 0;
2113 init->begx = 0;
2114 strnz__cpy(init->title, "View Help", MAXLEN - 1);
2115 popup_view(init, eargc, eargv, init->lines, init->cols, init->begy,
2116 init->begx);
2118 init->view->f_redisplay_page = true;
2119}
int popup_view(Init *, int, char **, int, int, int, int)
instantiate a view popup window
Definition popups.c:144
#define VIEW_HELP_FILE
Definition common.h:35
#define MAXARGS
Definition cm.h:48
int eargc
Definition futil.c:57
char * eargv[MAXARGS]
Definition futil.c:58
int destroy_argv(int argc, char **argv)
Deallocates memory allocated for argument strings in argv.
Definition futil.c:494
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition futil.c:573
char title[MAXLEN]
Definition common.h:129
char mapp_help[MAXLEN]
Definition common.h:148
int begx
Definition common.h:118
int cols
Definition common.h:116
int begy
Definition common.h:117
View * view
Definition common.h:188
int lines
Definition common.h:115
Definition view.h:42
bool f_redisplay_page
Definition view.h:100

References Init::begx, Init::begy, Init::cols, destroy_argv(), View::f_help_spec, View::f_redisplay_page, View::help_spec, Init::lines, Init::mapp_help, popup_view(), strnz__cat(), strnz__cpy(), Init::title, and Init::view.

Referenced by view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_display_page()

void view_display_page ( View * view)

Display Current Page.

Parameters
viewdata structure

Definition at line 1344 of file view_engine.c.

1344 {
1345 int i;
1346 view->cury = 0;
1347 wmove(view->pad, 0, 0);
1348 if (view->lnno_win)
1349 wmove(view->lnno_win, 0, 0);
1350 view->ln = view->page_top_ln;
1351 view->page_bot_ln = view->ln;
1352 view->page_top_pos = view->ln_tbl[view->ln];
1353 view->file_pos = view->page_top_pos;
1354 view->page_bot_pos = view->file_pos;
1355 for (i = 0; i < view->scroll_lines; i++) {
1356 view->page_bot_pos = get_next_line(view, view->page_bot_pos);
1357 if (view->f_eod)
1358 break;
1359 fmt_line(view);
1361 }
1362 if (view->cury < view->scroll_lines) {
1363 wmove(view->lnno_win, view->cury, 0);
1364 wclrtobot(view->lnno_win);
1365 wmove(view->pad, view->cury, 0);
1366 wclrtobot(view->pad);
1367 }
1368 view->page_bot_ln = view->ln;
1369}
off_t get_next_line(View *, off_t)
Get Next Line from View->buf.
void display_line(View *)
Display Line on Padparam View *view data structure.
int fmt_line(View *)
Format Line for Display.

References View::cury, display_line(), View::f_eod, View::file_pos, fmt_line(), get_next_line(), View::ln, View::ln_tbl, View::lnno_win, View::pad, View::page_bot_ln, View::page_bot_pos, View::page_top_ln, View::page_top_pos, and View::scroll_lines.

Referenced by next_page(), and view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function: