[1X4 Browsing Tables in [5XGAP[1X using [10Xncurses[1X âThe User Interface[0X As stated in Section [14X1.1[0m, one aim of the [5XBrowse[0m package is to provide tools for the quite usual task to show a two-dimensional array or certain rows and columns of it on a character screen in a formatted way, to navigate in this array via key strokes (and mouse events), and to search for strings, to sort the array by row or column values etc. The idea is that one starts with an array of data, the [13Xmain table[0m. Optionally, labels for each row of the main table are given, which are also arranged in an array (with perhaps several columns), the [13Xrow labels table[0m; analogously, a [13Xcolumn labels table[0m of labels for the columns of the main table may be given. The row labels are shown to the left of the main table, the column labels are shown above the main table. The space above the row labels and to the left of the column labels can be used for a fourth table, the [13Xcorner table[0m, with information about the labels or about the main table. Optionally, a [13Xheader[0m and a [13Xfooter[0m may be shown above and below these four tables, respectively. Header and footer are not separated into columns. So the shown window has the following structure. ---------------------------------- | header | ---------------------------------- | | | | corner | column labels | | | | ---------------------------------- | | | | | | | row | main | | labels | table | | | | | | | | | | ---------------------------------- | footer | ---------------------------------- If not the whole tables fit into the window then only subranges of rows and columns of the main table are shown, together with the corresponding row and column labels. Also in this case, the row heights and column widths are computed w.r.t. the whole table not w.r.t. the shown rows and columns. This means that the shown row labels are unchanged if the range of shown columns is changed, the shown column labels are unchanged if the range of shown rows is changed, and the whole corner table is always shown. The current chapter describes the user interface for [13Xstandard applications[0m of this kind, i. e., those applications for which one just has to collect the data to be shown in a record âwhich we call a [13Xbrowse table[0mâ without need for additional [5XGAP[0m programming. Section [14X4.1[0m gives an overview of the features available in standard browse table applications, and Section [14X4.2[0m describes the data structures used in browse tables. Finally, Section [14X4.3[0m introduces the function [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m), which is the generic function for showing browse table in visual mode. For technical details needed to extend these applications and to build other applications, see Chapter [14X5[0m. Examples of browse table applications are shown in Chapter [14X6[0m. [1X4.1 Features Supported by the Function [10XNCurses.BrowseGeneric[1X[0X Standard applications of the function [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m) have the following functionality. Other applications may provide only a subset, or add further functionality, see Chapters [14X5[0m and [14X6[0m. [8XScrolling:[0m The subranges of shown rows and columns of the main table can be modified, such that the focus area is moved to the left, to the right, up, or down; depending on the context, the focus is moved by one character, by one table cell or a part of it, by the window height/width (minus one character or minus one table cell). If mouse events are enabled then cells can be selected also via mouse clicks. [8XSelecting:[0m A cell, row, or column of the main table can be selected; then it is shown highlighted on the screen (by default using the attribute [10XNCurses.attrs.STANDOUT[0m, see Section [14X2.1-7[0m). The selection can be moved inside the main table to a neighboring cell, row, or column; this causes also scrolling of the main table when the window borders are reached. [8XSearching:[0m A search string is entered by the user, and the first matching cell becomes selected; one can search further for the next matching cell. Global search parameters define what matching means (case sensitive or not, search for substrings or complete words) and what the first and the next matching cells are (search in the whole table or just in the selected row or column, search for whole words or prefixes or suffixes, search forwards or backwards). [8XSorting:[0m If a row or column is selected then the main table can be sorted w.r.t. the entries in this row or column. Global sort parameters describe for example whether one wants to sort ascending or descending, or case sensitive or not. If a categorized table is sorted by a column then the category rows are removed and the current sorting and filtering by rows is reset before the table is sorted by the given column. If a table is sorted by a column/row that is already sorted by a column/row then this ordering is reset first. Sorting can be undone globally, i. e., one can return to the unsorted table. [8XSorting and Categorizing: [0m If a column is selected then the main table can be sorted w.r.t. the entries in this column, and additionally these entries are turned into [13Xcategory rows[0m, i. e., additional rows are added to the main table, appearing immediately above the table rows with a fixed value in the selected column, and showing this column value. (There should be no danger to mix up this notion of categories with the one introduced in [14X'Reference: Categories'[0m.) The category rows can be [13Xcollapsed[0m (that is, the table rows that belong to this category row are not shown) or [13Xexpanded[0m (that is, the corresponding table rows are shown). Some of the global search parameters affect the category rows, for example, whether the category rows shall involve a counter showing the number of corresponding data rows, or whether a row of the browse table appears under different category rows. Sorting and categorizing can be undone globally, i. e., one can return to the unsorted table without category rows. [8XFiltering: [0m The browse table can be restricted to those rows or columns in which a given search string occurs. (Also entries in collapsed rows can match; they remain collapsed then.) As a consequence, the category rows are restricted to those under which a matching row occurs. (It is irrelevant whether the search string occurs in category rows.) If the search string does not occur at all then a message is printed, and the table remains as it was before. If a browse table is restricted then this fact is indicated by the message "restricted table" in the lower right corner of the window. When a column or row is selected then the search is restricted to the entries in this column or row, respectively. Besides using a search, one can also explicitly hide the selected row or column. Filtering in an already restricted table restricts the shown rows or columns further. Filtering can be undone globally, i. e., one can return to the unrestricted table. [8XHelp:[0m Depending on the application and on the situation, different sets of user inputs may be available and different meanings of these inputs are possible. An overview of the currently available inputs and their meanings can be opened in each situation, by hitting the [12X?[0m key. [8XRe-entering:[0m When one has called [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m) with a browse table, and returns from visual mode to the [5XGAP[0m prompt after some navigation steps, calling [10XNCurses.BrowseGeneric[0m again with this table will enter visual mode in the same situation where it was left. For example, the cell in the top-left position will be the same as before, and if a cell was selected before then this cell will be selected now. (One can avoid this behavior using the optional second argument of [10XNCurses.BrowseGeneric[0m.) [8XLogging:[0m The integers corresponding to the user inputs in visual mode are collected in a list that is stored in the component [10Xdynamic.log[0m of the browse table. It can be used for repeating the inputs with the replay feature. (For browse table applications that give the user no access to the browse table itself, one can force the log to be assigned to the component [10Xlog[0m of the global variable [10XBrowseData[0m, see Section [14X5.4-1[0m.) [8XReplay:[0m Instead of interactively hitting keys in visual mode, one can prescribe the user inputs to a browse table via a "replay record"; the inputs are then processed with given time intervals. The easiest way to create a meaningful replay record is via the function [2XBrowseData.SetReplay[0m ([14X5.4-2[0m), with argument the [10Xdynamic.log[0m component of the browse table in question that was stored in an interactive session. The following features are not available in standard applications. They require additional programming. [8XClicking:[0m One possible action is to "click" a selected cell, row, or column, by hitting the [12XEnter[0m key. It depends on the application what the effect is. A typical situation is that a corresponding [5XGAP[0m object is added to the list of return values of [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m). Again it depends on the application what this [5XGAP[0m object is. In order to use this feature, one has to provide a record whose components are [5XGAP[0m functions, see Section [14X5.4-1[0m for details. If mouse events are enabled then also mouse clicks can be used as an alternative to hitting the [12XEnter[0m key. [8XReturn Value: [0m The function [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m) may have an application dependent return value. A typical situation is that a list of objects corresponding to those cells is returned that were "clicked" in visual mode. In order to use this feature, one has to assign the desired value to the component [10Xdynamic.Return[0m of the browse table. [1X4.2 Data Structures used by [10XNCurses.BrowseGeneric[1X[0X [1X4.2-1 BrowseData.IsBrowseTableCellData[0m [2X> BrowseData.IsBrowseTableCellData( [0X[3Xobj[0X[2X ) __________________________[0Xfunction [6XReturns:[0X [9Xtrue[0m if the argument is a list or a record in a supported format. A [13Xtable cell data object[0m describes the input data for the contents of a cell in a browse table. It is represented by either an attribute line (see [2XNCurses.IsAttributeLine[0m ([14X2.2-3[0m)), for cells of height one, or a list of attribute lines or a record with the components [10Xrows[0m, a list of attribute lines, and optionally [10Xalign[0m, a substring of [10X"bclt"[0m, which describes the alignment of the attribute lines in the table cell -- bottom, horizontally centered, left, and top alignment; the default is right and vertically centered alignment. (Note that the height of a table row and the width of a table column can be larger than the height and width of an individual cell.) [4X--------------------------- Example ----------------------------[0X [4Xgap> BrowseData.IsBrowseTableCellData( "abc" );[0X [4Xtrue[0X [4Xgap> BrowseData.IsBrowseTableCellData( [ "abc", "def" ] );[0X [4Xtrue[0X [4Xgap> BrowseData.IsBrowseTableCellData( rec( rows:= [ "ab", "cd" ],[0X [4X> align:= "tl" ) );[0X [4Xtrue[0X [4Xgap> BrowseData.IsBrowseTableCellData( "" );[0X [4Xtrue[0X [4Xgap> BrowseData.IsBrowseTableCellData( [] );[0X [4Xtrue[0X [4X------------------------------------------------------------------[0X The [13Xempty string[0m is a table cell data object of height one and width zero whereas the [13Xempty list[0m (which is not in [2XIsStringRep[0m ([14XReference: IsStringRep[0m)) is a table cell data object of height zero and width zero. [1X4.2-2 BrowseData.BlockEntry[0m [2X> BrowseData.BlockEntry( [0X[3Xtablecelldata, height, width[0X[2X ) ____________[0Xfunction [6XReturns:[0X a list of attribute lines. For a table cell data object [3Xtablecelldata[0m (see [2XBrowseData.IsBrowseTableCellData[0m ([14X4.2-1[0m)) and two positive integers [3Xheight[0m and [3Xwidth[0m, [10XBrowseData.BlockEntry[0m returns a list of [3Xheight[0m attribute lines of displayed length [3Xwidth[0m each (see [2XNCurses.WidthAttributeLine[0m ([14X2.2-7[0m)), that represents the formatted version of [3Xtablecelldata[0m. If the rows of [3Xtablecelldata[0m have different numbers of displayed characters then they are filled up to the desired numbers of rows and columns, according to the alignment prescribed by [3Xtablecelldata[0m; the default alignment is right and vertically centered. [4X--------------------------- Example ----------------------------[0X [4Xgap> BrowseData.BlockEntry( "abc", 3, 5 );[0X [4X[ " ", " abc", " " ][0X [4Xgap> BrowseData.BlockEntry( rec( rows:= [ "ab", "cd" ],[0X [4X> align:= "tl" ), 3, 5 );[0X [4X[ "ab ", "cd ", " " ][0X [4X------------------------------------------------------------------[0X [1X4.2-3 BrowseData.IsBrowseTable[0m [2X> BrowseData.IsBrowseTable( [0X[3Xobj[0X[2X ) __________________________________[0Xfunction [6XReturns:[0X [9Xtrue[0m if the argument record has the required components and is consistent. A [13Xbrowse table[0m is a [5XGAP[0m record that can be used as the first argument of the function [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m). The supported components of a browse table are [10Xwork[0m and [10Xdynamic[0m, their values must be records: The components in [10Xwork[0m describe that part of the data that are not likely to depend on user interactions, such as the table entries and their heights and widths. The components in [10Xdynamic[0m describe that part of the data that is intended to change with user interactions, such as the currently shown top-left entry of the table, or the current status w.r.t. sorting. For example, suppose you call [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m) twice with the same browse table; the second call enters the table in the same status where it was left [13Xafter[0m the first call if the component [10Xdynamic[0m is kept, whereas one has to reset (which usually simply means to unbind) the component [10Xdynamic[0m if one wants to start in the same status as [13Xbefore[0m the first call. The following components are the most important ones for standard browse applications. All these components belong to the [10Xwork[0m record. For other supported components (of [10Xwork[0m as well as of [10Xdynamic[0m) and for the meaning of the term "mode", see Section [14X5.2[0m. [8X[10Xmain[0m[8X[0m is the list of lists of table cell data objects that form the matrix to be shown. There is no default for this component. (It is possible to compute the entries of the main table on demand, see the description of the component [10XMain[0m in Section [14X5.4-1[0m. In this situation, the value of the component [10Xmain[0m can be an empty list.) [8X[10Xheader[0m[8X[0m describes a header that shall be shown above the column labels. The value is either a list of attribute lines ("static header") or a function or a record whose component names are names of available modes of the browse table ("dynamic header"). In the function case, the function must take the browse table as its only argument, and return a list of attribute lines. In the record case, the values of the components must be such functions. It is assumed that the number of these lines depends at most on the mode. The default is an empty list, i. e., there is no header. [8X[10Xfooter[0m[8X[0m describes a footer that shall be shown below the table. The value is analogous to that of [10Xfooter[0m. The default is an empty list, i. e., there is no footer. [8X[10XlabelsRow[0m[8X[0m is a list of row label rows, each being a list of table cell data objects. These rows are shown to the left of the main table. The default is an empty list, i. e., there are no row labels. [8X[10XlabelsCol[0m[8X[0m is a list of column information rows, each being a list of table cell data objects. These rows are shown between the header and the main table. The default is an empty list, i. e., there are no column labels. [8X[10Xcorner[0m[8X[0m is a list of lists of table cell data objects that are printed in the upper left corner, i. e., to the left of the column label rows and above the row label columns. The default is an empty list. [8X[10XsepRow[0m[8X[0m describes the separators above and below rows of the main table and of the row labels table. The value is either an attribute line or a (not necessarily dense) list of attribute lines. In the former case, repetitions of the attribute line are used as separators below each row and above the first row of the table; in the latter case, repetitions of the entry at the first position (if bound) are used above the first row, and repetitions of the last bound entry before the (i+2)-th position (if there is such an entry at all) are used below the i-th table row. The default is an empty string, which means that there are no row separators. [8X[10XsepCol[0m[8X[0m describes the separators in front of and behind columns of the main table and of the column labels table. The format of the value is analogous to that of the component [10XsepRow[0m; the default is the string [10X" "[0m (whitespace of width one). [8X[10XsepLabelsCol[0m[8X[0m describes the separators above and below rows of the column labels table and of the corner table, analogously to [10XsepRow[0m. The default is an empty string, which means that there are no column label separators. [8X[10XsepLabelsRow[0m[8X[0m describes the separators in front of and behind columns of the row labels table and of the corner table, analogously to [10XsepCol[0m. The default is an empty string. We give a few examples of standard applications. The first example defines a small browse table by prescribing only the component [10Xwork.main[0m, so the defaults for row and column labels (no such labels), and for separators are used. The table cells are given by plain strings, so they have height one. Usually this table will fit on the screen. [4X--------------------------- Example ----------------------------[0X [4Xgap> m:= 10;; n:= 5;;[0X [4Xgap> xpl1:= rec( work:= rec([0X [4X> main:= List( [ 1 .. m ], i -> List( [ 1 .. n ],[0X [4X> j -> String( [ i, j ] ) ) ) ) );;[0X [4Xgap> BrowseData.IsBrowseTable( xpl1 );[0X [4Xtrue[0X [4X------------------------------------------------------------------[0X In the second example, also row and column labels appear, and different separators are used. The table cells have height three. Also this table will usually fit on the screen. [4X--------------------------- Example ----------------------------[0X [4Xgap> m:= 6;; n:= 5;;[0X [4Xgap> xpl2:= rec( work:= rec([0X [4X> main:= List( [ 1 .. m ], i -> List( [ 1 .. n ],[0X [4X> j -> rec( rows:= List( [ -i*j, i*j*1000+j, i-j ], String ),[0X [4X> align:= "c" ) ) ),[0X [4X> labelsRow:= List( [ 1 .. m ], i -> [ String( i ) ] ),[0X [4X> labelsCol:= [ List( [ 1 .. n ], String ) ],[0X [4X> sepRow:= "-",[0X [4X> sepCol:= "|",[0X [4X> ) );;[0X [4Xgap> BrowseData.IsBrowseTable( xpl2 );[0X [4Xtrue[0X [4X------------------------------------------------------------------[0X The third example additionally has a static header and a dynamic footer, and the table cells involve attributes. This table will usually not fit on the screen. [4X--------------------------- Example ----------------------------[0X [4Xgap> m:= 30;; n:= 25;;[0X [4Xgap> xpl3:= rec( work:= rec([0X [4X> header:= [ " Example 3" ],[0X [4X> main:= List( [ 1 .. m ], i -> List( [ 1 .. n ],[0X [4X> j -> rec( rows:= [ String( -i*j ),[0X [4X> [ NCurses.attrs.BOLD, true,[0X [4X> NCurses.attrs.ColorPairs[56+1], true,[0X [4X> String( i*j*1000+j ),[0X [4X> NCurses.attrs.NORMAL, true ],[0X [4X> String( i-j ) ],[0X [4X> align:= "c" ) ) ),[0X [4X> labelsRow:= List( [ 1 .. 30 ], i -> [ String( i ) ] ),[0X [4X> sepLabelsRow:= " % ",[0X [4X> labelsCol:= [ List( [ 1 .. 30 ], i -> [[0X [4X> NCurses.attrs.ColorPairs[ 56+4 ], true,[0X [4X> String( i ),[0X [4X> NCurses.attrs.NORMAL, true ] ) ],[0X [4X> sepLabelsCol:= "=",[0X [4X> sepRow:= "*",[0X [4X> sepCol:= " |",[0X [4X> footer:= t -> [ Concatenation( "top-left entry is: ",[0X [4X> String( t.dynamic.topleft{ [ 1, 2] } ) ) ],[0X [4X> ) );;[0X [4Xgap> BrowseData.IsBrowseTable( xpl3 );[0X [4Xtrue[0X [4X------------------------------------------------------------------[0X The fourth example illustrates that highlighting may not work properly for browse tables containing entries whose attributes are not set with explicit Boolean values, see [2XNCurses.IsAttributeLine[0m ([14X2.2-3[0m). Call [2XNCurses.BrowseGeneric[0m ([14X4.3-1[0m) with the browse table [10Xxpl4[0m, and select an entry (or a column or a row): Only the middle row of each selected cell will be highlighted, because only in this row, the color attribute is switched on with an explicit [9Xtrue[0m. [4X--------------------------- Example ----------------------------[0X [4Xgap> xpl4:= rec([0X [4X> defc:= NCurses.defaultColors,[0X [4X> wd:= Maximum( List( ~.defc, Length ) ),[0X [4X> ca:= NCurses.ColorAttr,[0X [4X> work:= rec([0X [4X> header:= [ "Examples of NCurses.ColorAttr" ],[0X [4X> main:= List( ~.defc, i -> List( ~.defc,[0X [4X> j -> [ [ ~.ca( i, j ), String( i, ~.wd ) ], # no true![0X [4X> [ ~.ca( i, j ), true, String( "on", ~.wd ) ],[0X [4X> [ ~.ca( i, j ), String( j, ~.wd ) ] ] ) ), # no true![0X [4X> labelsRow:= List( ~.defc, i -> [ String( i ) ] ),[0X [4X> labelsCol:= [ List( ~.defc, String ) ],[0X [4X> sepRow:= "-",[0X [4X> sepCol:= [ " |", "|" ],[0X [4X> ) );;[0X [4Xgap> BrowseData.IsBrowseTable( xpl4 );[0X [4Xtrue[0X [4X------------------------------------------------------------------[0X [1X4.3 The Function [10XNCurses.BrowseGeneric[1X[0X [1X4.3-1 NCurses.BrowseGeneric[0m [2X> NCurses.BrowseGeneric( [0X[3Xt[, arec][0X[2X ) _______________________________[0Xfunction [6XReturns:[0X an application dependent value, or nothing. [2XNCurses.BrowseGeneric[0m is used to show the browse table [3Xt[0m (see [2XBrowseData.IsBrowseTable[0m ([14X4.2-3[0m)) in a formatted way on a text screen, and allows the user to navigate in this table. The optional argument [3Xarec[0m, if given, must be a record whose components [10Xwork[0m and [10Xdynamic[0m, if bound, are used to provide defaults for missing values in the corresponding components of [3Xt[0m. The default for [3Xarec[0m and for the components not provided in [3Xarec[0m is [10XBrowseData.defaults[0m, see [2XBrowseData[0m ([14X5.4-1[0m), the function [10XBrowseData.SetDefaults[0m sets these default values. At least the component [10Xwork.main[0m must be bound in [3Xt[0m, with value a list of list of table cell data objects, see [2XBrowseData.IsBrowseTableCellData[0m ([14X4.2-1[0m). When the window or the screen is too small for the browse table, according to its component [10Xwork.minyx[0m, the table will not be shown in visual mode, and [9Xfail[0m is returned. (This holds also if there would be no return value of the call in a large enough screen.) Thus one should check for [9Xfail[0m results of programmatic calls of [2XNCurses.BrowseGeneric[0m, and one should better not admit [9Xfail[0m as a regular return value. Most likely, [2XNCurses.BrowseGeneric[0m will not be called on the command line, but the browse table [3Xt[0m will be composed by a suitable function which then calls [2XNCurses.BrowseGeneric[0m, see the examples in Chapter [14X6[0m.