zahl

Log | Files | Refs | README

crossline.h (5942B)


      1 /* crossline.h -- Version 1.0
      2  *
      3  * Crossline is a small, self-contained, zero-config, MIT licensed,
      4  *   cross-platform, readline and libedit replacement.
      5  *
      6  * Press <F1> to get full shortcuts list.
      7  *
      8  * See crossline.c for more information.
      9  *
     10  * ------------------------------------------------------------------------
     11  *
     12  * MIT License
     13  *
     14  * Copyright (c) 2019, JC Wang (wang_junchuan@163.com)
     15  *
     16  * Permission is hereby granted, free of charge, to any person obtaining a copy
     17  * of this software and associated documentation files (the "Software"), to deal
     18  * in the Software without restriction, including without limitation the rights
     19  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     20  * copies of the Software, and to permit persons to whom the Software is
     21  * furnished to do so, subject to the following conditions:
     22  *
     23  * The above copyright notice and this permission notice shall be included in all
     24  * copies or substantial portions of the Software.
     25  *
     26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     29  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     30  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     31  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     32  * SOFTWARE.
     33  * ------------------------------------------------------------------------
     34  */
     35 
     36 #ifndef __CROSSLINE_H
     37 #define __CROSSLINE_H
     38 
     39 #ifdef __cplusplus
     40 extern "C" {
     41 #endif
     42 
     43 typedef enum {
     44 	CROSSLINE_FGCOLOR_DEFAULT       = 0x00,
     45 	CROSSLINE_FGCOLOR_BLACK         = 0x01,
     46 	CROSSLINE_FGCOLOR_RED           = 0x02,
     47 	CROSSLINE_FGCOLOR_GREEN         = 0x03,
     48 	CROSSLINE_FGCOLOR_YELLOW        = 0x04,
     49 	CROSSLINE_FGCOLOR_BLUE          = 0x05,
     50 	CROSSLINE_FGCOLOR_MAGENTA       = 0x06,
     51 	CROSSLINE_FGCOLOR_CYAN          = 0x07,
     52 	CROSSLINE_FGCOLOR_WHITE     	= 0x08,
     53 	CROSSLINE_FGCOLOR_BRIGHT     	= 0x80,
     54 	CROSSLINE_FGCOLOR_MASK     	    = 0x7F,
     55 
     56 	CROSSLINE_BGCOLOR_DEFAULT       = 0x0000,
     57 	CROSSLINE_BGCOLOR_BLACK         = 0x0100,
     58 	CROSSLINE_BGCOLOR_RED           = 0x0200,
     59 	CROSSLINE_BGCOLOR_GREEN         = 0x0300,
     60 	CROSSLINE_BGCOLOR_YELLOW        = 0x0400,
     61 	CROSSLINE_BGCOLOR_BLUE          = 0x0500,
     62 	CROSSLINE_BGCOLOR_MAGENTA       = 0x0600,
     63 	CROSSLINE_BGCOLOR_CYAN          = 0x0700,
     64 	CROSSLINE_BGCOLOR_WHITE     	= 0x0800,
     65 	CROSSLINE_BGCOLOR_BRIGHT     	= 0x8000,
     66 	CROSSLINE_BGCOLOR_MASK     	    = 0x7F00,
     67 
     68 	CROSSLINE_UNDERLINE     	    = 0x10000,
     69 
     70 	CROSSLINE_COLOR_DEFAULT         = CROSSLINE_FGCOLOR_DEFAULT | CROSSLINE_BGCOLOR_DEFAULT
     71 } crossline_color_e;
     72 
     73 // Main API to read a line, return buf if get line, return NULL if EOF.
     74 extern char* crossline_readline (const char *prompt, char *buf, int size);
     75 
     76 // Same with crossline_readline except buf holding initial input for editing.
     77 extern char* crossline_readline2 (const char *prompt, char *buf, int size);
     78 
     79 // Set move/cut word delimiter, default is all not digital and alphabetic characters.
     80 extern void  crossline_delimiter_set (const char *delim);
     81 
     82 // Read a character from terminal without echo
     83 extern int	 crossline_getch (void);
     84 
     85 
     86 /* 
     87  * History APIs
     88  */
     89 
     90 // Save history to file
     91 extern int   crossline_history_save (const char *filename);
     92 
     93 // Load history from file
     94 extern int   crossline_history_load (const char *filename);
     95 
     96 // Show history in buffer
     97 extern void  crossline_history_show (void);
     98 
     99 // Clear history
    100 extern void  crossline_history_clear (void);
    101 
    102 
    103 /*
    104  * Completion APIs
    105  */
    106 
    107 typedef struct crossline_completions_t crossline_completions_t;
    108 typedef void (*crossline_completion_callback) (const char *buf, crossline_completions_t *pCompletions);
    109 
    110 // Register completion callback
    111 extern void  crossline_completion_register (crossline_completion_callback pCbFunc);
    112 
    113 // Add completion in callback. Word is must, help for word is optional.
    114 extern void  crossline_completion_add (crossline_completions_t *pCompletions, const char *word, const char *help);
    115 
    116 // Add completion with color.
    117 extern void  crossline_completion_add_color (crossline_completions_t *pCompletions, const char *word, 
    118 														crossline_color_e wcolor, const char *help, crossline_color_e hcolor);
    119 
    120 // Set syntax hints in callback
    121 extern void  crossline_hints_set (crossline_completions_t *pCompletions, const char *hints);
    122 
    123 // Set syntax hints with color
    124 extern void  crossline_hints_set_color (crossline_completions_t *pCompletions, const char *hints, crossline_color_e color);
    125 
    126 
    127 /*
    128  * Paging APIs
    129  */
    130 
    131 // Enable/Disble paging control
    132 extern int crossline_paging_set (int enable);
    133 
    134 // Check paging after print a line, return 1 means quit, 0 means continue
    135 // if you know only one line is printed, just give line_len = 1
    136 extern int  crossline_paging_check (int line_len);
    137 
    138 
    139 /* 
    140  * Cursor APIs
    141  */
    142 
    143 // Get screen rows and columns
    144 extern void crossline_screen_get (int *pRows, int *pCols);
    145 
    146 // Clear current screen
    147 extern void crossline_screen_clear (void);
    148 
    149 // Get cursor postion (0 based)
    150 extern int  crossline_cursor_get (int *pRow, int *pCol);
    151 
    152 // Set cursor postion (0 based)
    153 extern void crossline_cursor_set (int row, int col);
    154 
    155 // Move cursor with row and column offset, row_off>0 move up row_off lines, <0 move down abs(row_off) lines
    156 // =0 no move for row, similar with col_off
    157 extern void crossline_cursor_move (int row_off, int col_off);
    158 
    159 // Hide or show cursor
    160 extern void crossline_cursor_hide (int bHide);
    161 
    162 
    163 /* 
    164  * Color APIs
    165  */
    166 
    167 // Set text color, CROSSLINE_COLOR_DEFAULT will revert to default setting
    168 // `\t` is not supported in Linux terminal, same below. Don't use `\n` in Linux terminal, same below.
    169 extern void crossline_color_set (crossline_color_e color);
    170 
    171 // Set default prompt color
    172 extern void crossline_prompt_color_set (crossline_color_e color);
    173 
    174 #ifdef __cplusplus
    175 }
    176 #endif
    177 
    178 #endif /* __CROSSLINE_H */