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

Manage the View Display. More...

Functions

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

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.

Note
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 1624 of file view_engine.c.

1624 {
1625 char ln_s[16];
1626
1627 if (view->cury < 0)
1628 view->cury = 0;
1629 if (view->cury > view->scroll_lines - 1)
1630 view->cury = view->scroll_lines - 1;
1631 if (view->f_ln) {
1632 ssnprintf(ln_s, 7, "%6jd", view->ln);
1633 wmove(view->ln_win, view->cury, 0);
1634 wclrtoeol(view->ln_win);
1635 mvwaddstr(view->ln_win, view->cury, 0, ln_s);
1636 }
1637 wmove(view->pad, view->cury, 0);
1638 wclrtoeol(view->pad);
1639 wadd_wchstr(view->pad, view->cmplx_buf);
1640 view->cury++;
1641}
View * view
Definition mem.c:48
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:147

References View::cmplx_buf, View::cury, View::f_ln, View::ln, View::ln_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 1899 of file view_engine.c.

1899 {
1900 char message_str[PAD_COLS + 1];
1901 int l;
1902 l = strnz__cpy(message_str, s, PAD_COLS);
1903 wmove(view->win, view->cmd_line, 0);
1904 if (l != 0) {
1905 wclrtoeol(view->win);
1906 wattron(view->win, WA_REVERSE);
1907 waddstr(view->win, " ");
1908 waddstr(view->win, message_str);
1909 waddstr(view->win, " ");
1910 wattroff(view->win, WA_REVERSE);
1911 waddstr(view->win, " ");
1912 view->curx = l + 2;
1913 wmove(view->win, view->cmd_line, view->curx);
1914 }
1915 return (view->curx);
1916}
#define PAD_COLS
Definition view.h:30
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition futil.c:269

References View::cmd_line, View::curx, strnz__cpy(), and View::win.

Referenced by lp(), 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.

Definition at line 1655 of file view_engine.c.

1655 {
1656 char ansi_tok[MAXLEN];
1657 int i = 0, j = 0;
1658 int len = 0;
1659 const char *s;
1660 attr_t attr = WA_NORMAL;
1661 int cpx = cp_win;
1662 cchar_t cc = {0};
1663 wchar_t wstr[2] = {L'\0', L'\0'};
1664
1665 char *in_str = view->line_in_s;
1666 cchar_t *cmplx_buf = view->cmplx_buf;
1667
1668 rtrim(view->line_out_s);
1669 mbstate_t mbstate;
1670 memset(&mbstate, 0, sizeof(mbstate));
1671 while (in_str[i] != '\0') {
1672 if (in_str[i] == '\033' && in_str[i + 1] == '[') {
1673 len = strcspn(&in_str[i], "mK ") + 1;
1674 memcpy(ansi_tok, &in_str[i], len + 1);
1675 ansi_tok[len] = '\0';
1676 if (ansi_tok[0] == '\0') {
1677 if (i + 2 < MAXLEN)
1678 i += 2;
1679 continue;
1680 }
1681 if (len == 0 || in_str[i + len - 1] == ' ') {
1682 i += 2;
1683 continue;
1684 } else if (in_str[i + len - 1] == 'K') {
1685 i += len;
1686 continue;
1687 }
1688 parse_ansi_str(ansi_tok, &attr, &cpx);
1689 i += len;
1690 } else {
1691 if (in_str[i] == '\033') {
1692 i++;
1693 continue;
1694 }
1695 s = &in_str[i];
1696 if (*s == '\t') {
1697 do {
1698 wstr[1] = L'\0';
1699 setcchar(&cc, wstr, attr, cpx, nullptr);
1700 view->stripped_line_out[j] = ' ';
1701 cmplx_buf[j++] = cc;
1702 } while ((j < PAD_COLS - 2) && (j % view->tab_stop != 0));
1703 i++;
1704 } else {
1705 wstr[1] = L'\0';
1706 len = mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
1707 if (len <= 0) {
1708 wstr[0] = L'?';
1709 wstr[1] = L'\0';
1710 len = 1;
1711 }
1712 if (setcchar(&cc, wstr, attr, cpx, nullptr) != ERR) {
1713 if (len > 0 && (j + len) < PAD_COLS - 1) {
1714 view->stripped_line_out[j] = *s;
1715 cmplx_buf[j++] = cc;
1716 }
1717 }
1718 i += len;
1719 }
1720 }
1721 }
1722 if (j > view->maxcol)
1723 view->maxcol = j;
1724 wstr[0] = '\0';
1725 wstr[1] = '\0';
1726 setcchar(&cc, wstr, WA_NORMAL, cpx, nullptr);
1727 cmplx_buf[j] = cc;
1728 view->stripped_line_out[j] = '\0';
1729 return j;
1730}
size_t rtrim(char *)
Trims trailing spaces from string s in place.
Definition futil.c:102
char in_str[BUFSIZ+1]
Definition iloan.c:20
#define MAXLEN
Definition curskeys.c:15
int cp_win
Definition dwin.c:137
void parse_ansi_str(char *, attr_t *, int *)
Parse ANSI SGR Escape Sequence.

References View::cmplx_buf, cp_win, 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 1179 of file view_engine.c.

1179 {
1180 int rc;
1181 if (view->f_ln)
1182 rc = prefresh(view->pad, view->pminrow, view->pmincol, view->sminrow,
1183 view->smincol + 7, view->smaxrow, view->smaxcol);
1184 else
1185 rc = prefresh(view->pad, view->pminrow, view->pmincol, view->sminrow,
1186 view->smincol, view->smaxrow, view->smaxcol);
1187 if (rc == ERR)
1188 Perror("Error refreshing screen");
1189 wrefresh(view->win);
1190 if (view->f_ln)
1191 wrefresh(view->ln_win);
1192 return rc;
1193}
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1110

References View::f_ln, View::ln_win, View::pad, Perror(), View::pmincol, View::pminrow, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, and View::win.

Referenced by get_cmd_char(), 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 SGR (Select Graphic Rendition) escape sequence and updates the provided attr_t and color pair index based on the attributes specified in the ANSI string.

 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 1786 of file view_engine.c.

1786 {
1787 char *tok;
1788 char t0, t1;
1789 int len, x_idx;
1790 int fg, bg;
1791 int fg_clr, bg_clr;
1792 char *ansi_p = ansi_str + 2;
1793 extended_pair_content(*cpx, &fg_clr, &bg_clr);
1794 fg = fg_clr;
1795 bg = bg_clr;
1796 RGB rgb;
1797 tok = strtok((char *)ansi_p, ";m");
1798 bool a_toi_error = false;
1799 while (1) {
1800 if (tok == nullptr || *tok == '\0')
1801 break;
1802 len = strlen(tok);
1803 if (len == 2) {
1804 t0 = tok[0];
1805 t1 = tok[1];
1806 if (t0 == '3' || t0 == '4') {
1807 if (t1 == '8') {
1808 tok = strtok(nullptr, ";m");
1809 if (tok != nullptr) {
1810 if (*tok == '5') {
1811 tok = strtok(nullptr, ";m");
1812 if (tok != nullptr) {
1813 x_idx = a_toi(tok, &a_toi_error);
1814 rgb = xterm256_idx_to_rgb(x_idx);
1815 }
1816 } else if (*tok == '2') {
1817 tok = strtok(nullptr, ";m");
1818 rgb.r = a_toi(tok, &a_toi_error);
1819 tok = strtok(nullptr, ";m");
1820 rgb.g = a_toi(tok, &a_toi_error);
1821 tok = strtok(nullptr, ";m");
1822 rgb.b = a_toi(tok, &a_toi_error);
1823 }
1824 }
1825 if (t0 == '3')
1826 fg_clr = rgb_to_curses_clr(&rgb);
1827 else if (t0 == '4')
1828 bg_clr = rgb_to_curses_clr(&rgb);
1829 } else if (t1 == '9') {
1830 if (t0 == '3')
1831 fg_clr = CLR_FG;
1832 else if (t0 == '4')
1833 bg_clr = CLR_BG;
1834 } else if (t1 >= '0' && t1 <= '7') {
1835 if (t0 == '3') {
1836 x_idx = a_toi(&t1, &a_toi_error);
1837 rgb = xterm256_idx_to_rgb(x_idx);
1838 fg_clr = rgb_to_curses_clr(&rgb);
1839 } else if (t0 == '4') {
1840 x_idx = a_toi(&t1, &a_toi_error);
1841 rgb = xterm256_idx_to_rgb(x_idx);
1842 bg_clr = rgb_to_curses_clr(&rgb);
1843 }
1844 }
1845 } else if (t0 == '0') {
1846 *tok = t1;
1847 len = 1;
1848 }
1849 }
1850 if (len == 1) {
1851 if (*tok == '0') {
1852 *attr = WA_NORMAL;
1853 fg_clr = CLR_FG;
1854 bg_clr = CLR_BG;
1855 } else {
1856 switch (a_toi(tok, &a_toi_error)) {
1857 case 1:
1858 *attr |= WA_BOLD;
1859 break;
1860 case 2:
1861 *attr |= WA_DIM;
1862 break;
1863 case 3:
1864 *attr |= WA_ITALIC;
1865 break;
1866 case 4:
1867 *attr |= WA_UNDERLINE;
1868 break;
1869 case 5:
1870 *attr |= WA_BLINK;
1871 break;
1872 case 7:
1873 *attr |= WA_REVERSE;
1874 break;
1875 case 8:
1876 *attr |= WA_INVIS;
1877 break;
1878 default:
1879 break;
1880 }
1881 }
1882 } else if (len == 0) {
1883 *attr = WA_NORMAL;
1884 fg_clr = CLR_FG;
1885 bg_clr = CLR_BG;
1886 }
1887 tok = strtok(nullptr, ";m");
1888 }
1889 if (!a_toi_error && (fg_clr != fg || bg_clr != bg)) {
1890 clr_pair_idx = get_clr_pair(fg_clr, bg_clr);
1891 *cpx = clr_pair_idx;
1892 }
1893 return;
1894}
@ CLR_FG
Definition cm.h:140
@ CLR_BG
Definition cm.h:141
int clr_pair_idx
Definition dwin.c:143
RGB xterm256_idx_to_rgb(int)
Convert XTerm 256 color index to RGB color.
Definition dwin.c:590
int rgb_to_curses_clr(RGB *)
Get color index for RGB color.
Definition dwin.c:541
int get_clr_pair(int fg, int bg)
Get color pair index for foreground and background colors.
Definition dwin.c:510
int a_toi(char *, bool *)
a safer alternative to atoi() for converting ASCII strings to integers.
Definition futil.c:505
Definition cm.h:265
int r
Definition cm.h:266
int b
Definition cm.h:268
int g
Definition cm.h:267

References a_toi(), RGB::b, CLR_BG, CLR_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:

◆ resize_page()

void resize_page ( Init * init)

Resize Viewing Page.

Parameters
initdata structure

Definition at line 1145 of file view_engine.c.

1145 {
1146 int scr_lines, scr_cols;
1147 bool f_resize = false;
1148 view = init->view;
1149 if (!view->f_full_screen) {
1150 getmaxyx(stdscr, scr_lines, scr_cols);
1151 if (view->begy + view->lines + 2 > scr_lines) {
1152 view->lines = (scr_lines - view->begy) - 2;
1153 f_resize = true;
1154 }
1155 if (view->begx + view->cols + 2 > scr_cols) {
1156 view->cols = (scr_cols - view->begx) - 2;
1157 f_resize = true;
1158 }
1159 if (f_resize) {
1160 view->scroll_lines = view->lines - 1;
1161 view->cmd_line = 0;
1162 view->smaxrow = view->lines - 1;
1163 view->smaxcol = view->cols - 1;
1164 win_resize(view->lines + 2, view->cols + 2, view->title);
1165 restore_wins();
1166 wsetscrreg(view->pad, 0, view->scroll_lines);
1167 }
1168 }
1169 if (f_resize)
1170 view->f_redisplay_page = true;
1171 else
1172 view->f_redisplay_page = false;
1173}
Init * init
Definition common.h:186
void restore_wins()
Restore all windows after a screen resize.
Definition dwin.c:938
void win_resize(int, int, char *)
Resize the current window and its box, and update the title.
Definition dwin.c:851

References View::begx, View::begy, View::cmd_line, View::cols, View::f_full_screen, View::f_redisplay_page, View::lines, View::pad, restore_wins(), View::scroll_lines, View::smaxcol, View::smaxrow, View::title, Init::view, view, and win_resize().

Referenced by view_cmd_processor().

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.
Note
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 1952 of file view_engine.c.

1952 {
1953 char tmp_str[MAXLEN];
1954 int eargc = 0;
1955 char *eargv[MAXARGS];
1956 if (view->f_help_spec && view->help_spec[0] != '\0')
1957 strnz__cpy(tmp_str, view->help_spec, MAXLEN - 1);
1958 else {
1959 strnz__cpy(tmp_str, init->mapp_help, MAXLEN - 1);
1960 strnz__cat(tmp_str, "/", MAXLEN - 1);
1961 strnz__cat(tmp_str, VIEW_HELP_FILE, MAXLEN - 1);
1962 }
1963 eargv[eargc++] = strdup("view");
1964 eargv[eargc++] = strdup("-N");
1965 eargv[eargc++] = strdup("f");
1966 eargv[eargc++] = strdup(tmp_str);
1967 eargv[eargc] = nullptr;
1968 init->lines = 48;
1969 init->cols = 72;
1970 init->begy = 0;
1971 init->begx = 0;
1972 strnz__cpy(init->title, "View Help", MAXLEN - 1);
1973 popup_view(init, eargc, eargv, init->lines, init->cols, init->begy,
1974 init->begx);
1976 init->view->f_redisplay_page = true;
1977}
int popup_view(Init *, int, char **, int, int, int, int)
Definition popups.c:47
#define VIEW_HELP_FILE
Definition common.h:39
int eargc
Definition futil.c:41
#define MAXARGS
Definition cm.h:30
char * eargv[MAXARGS]
Definition futil.c:42
void destroy_argv(int argc, char **argv)
Deallocates memory allocated for argument strings in argv.
Definition futil.c:221
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition futil.c:298

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, Init::view, and 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 1239 of file view_engine.c.

1239 {
1240 int i;
1241 wmove(view->pad, 0, 0);
1242 if (view->ln_win)
1243 wmove(view->ln_win, 0, 0);
1244 view->page_top_ln = view->ln;
1245 view->page_bot_ln = view->ln;
1246 view->page_top_pos = view->ln_tbl[view->ln];
1247 view->file_pos = view->page_top_pos;
1248 view->page_bot_pos = view->file_pos;
1249 for (i = 0; i < view->scroll_lines; i++) {
1250 view->page_bot_pos = get_next_line(view, view->page_bot_pos);
1251 if (view->f_eod)
1252 break;
1253 fmt_line(view);
1255 }
1256 if (view->cury < view->scroll_lines) {
1257 wmove(view->ln_win, view->cury, 0);
1258 wclrtobot(view->ln_win);
1259 wmove(view->pad, view->cury, 0);
1260 wclrtobot(view->pad);
1261 }
1262 view->page_bot_ln = view->ln;
1263}
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::ln_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: