linenoise.h
1 /* linenoise.h -- guerrilla line editing library against the idea that a 2 * line editing lib needs to be 20,000 lines of C code. 3 * 4 * See linenoise.c for more information. 5 * 6 * ------------------------------------------------------------------------ 7 * 8 * Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com> 9 * Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com> 10 * 11 * All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions are 15 * met: 16 * 17 * * Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * * Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #ifndef __LINENOISE_H 38 #define __LINENOISE_H 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 #ifndef NO_COMPLETION 45 typedef struct linenoiseCompletions { 46 size_t len; 47 char **cvec; 48 } linenoiseCompletions; 49 50 /* 51 * The callback type for tab completion handlers. 52 */ 53 typedef void(linenoiseCompletionCallback)(const char *prefix, linenoiseCompletions *comp, void *userdata); 54 55 /* 56 * Sets the current tab completion handler and returns the previous one, or NULL 57 * if no prior one has been set. 58 */ 59 linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletionCallback *comp, void *userdata); 60 61 /* 62 * Adds a copy of the given string to the given completion list. The copy is owned 63 * by the linenoiseCompletions object. 64 */ 65 void linenoiseAddCompletion(linenoiseCompletions *comp, const char *str); 66 67 typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold, void *userdata); 68 typedef void(linenoiseFreeHintsCallback)(void *hint, void *userdata); 69 void linenoiseSetHintsCallback(linenoiseHintsCallback *callback, void *userdata); 70 void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *callback); 71 72 #endif 73 74 /* 75 * Prompts for input using the given string as the input 76 * prompt. Returns when the user has tapped ENTER or (on an empty 77 * line) EOF (Ctrl-D on Unix, Ctrl-Z on Windows). Returns either 78 * a copy of the entered string (for ENTER) or NULL (on EOF). The 79 * caller owns the returned string and must eventually free() it. 80 */ 81 char *linenoise(const char *prompt); 82 83 /** 84 * Like linenoise() but starts with an initial buffer. 85 */ 86 char *linenoiseWithInitial(const char *prompt, const char *initial); 87 88 /** 89 * Clear the screen. 90 */ 91 void linenoiseClearScreen(void); 92 93 /* 94 * Adds a copy of the given line of the command history. 95 */ 96 int linenoiseHistoryAdd(const char *line); 97 98 /* 99 * Sets the maximum length of the command history, in lines. 100 * If the history is currently longer, it will be trimmed, 101 * retaining only the most recent entries. If len is 0 or less 102 * then this function does nothing. 103 */ 104 int linenoiseHistorySetMaxLen(int len); 105 106 /* 107 * Returns the current maximum length of the history, in lines. 108 */ 109 int linenoiseHistoryGetMaxLen(void); 110 111 /* 112 * Saves the current contents of the history to the given file. 113 * Returns 0 on success. 114 */ 115 int linenoiseHistorySave(const char *filename); 116 117 /* 118 * Replaces the current history with the contents 119 * of the given file. Returns 0 on success. 120 */ 121 int linenoiseHistoryLoad(const char *filename); 122 123 /* 124 * Frees all history entries, clearing the history. 125 */ 126 void linenoiseHistoryFree(void); 127 128 /* 129 * Returns a pointer to the list of history entries, writing its 130 * length to *len if len is not NULL. The memory is owned by linenoise 131 * and must not be freed. 132 */ 133 char **linenoiseHistory(int *len); 134 135 /* 136 * Returns the number of display columns in the current terminal. 137 */ 138 int linenoiseColumns(void); 139 140 /** 141 * Enable or disable multiline mode (disabled by default) 142 */ 143 void linenoiseSetMultiLine(int enableml); 144 145 #ifdef __cplusplus 146 } 147 #endif 148 149 #endif /* __LINENOISE_H */