]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/gui/cli/linenoise.h
Add line editing support to the CLI UI
[jubjub.git] / src / gui / cli / 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.m 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 #import <ObjFW/ObjFW.h>
38
39 #include <termios.h>
40 #include <unistd.h>
41
42 typedef void(linenoiseCompletionCallback)(OFString *, OFList*);
43
44 enum linenoiseDirection {
45         LINENOISE_HISTORY_NEXT,
46         LINENOISE_HISTORY_PREV
47 };
48
49 @interface Linenoise : OFObject <OFApplicationDelegate>
50 {
51         bool _multiline;
52
53         bool _rawmode; // For atexit() function to check if restore is needed
54         struct termios _orig_termios; // In order to restore at exit
55
56         linenoiseCompletionCallback *_completionCallback;
57
58         size_t _maximalHistoryLength;
59         ssize_t _historyIndex;  // The history index we are currently editing
60         OFMutableArray *_history;
61
62         OFFile *_term;          // Terminal file descriptor
63
64         OFMutableString *_buf;  // Edited line buffer
65         OFString *_prompt;      // Prompt to display
66
67         size_t _pos;            // Current cursor position
68         size_t _oldpos;         // Previous refresh cursor position
69
70         size_t _cols;           // Number of columns in terminal
71         size_t _maxrows;        // Maximum num of rows used so far (multiline)
72 }
73 @property (assign) bool multiline;
74 @property (assign) size_t maximalHistoryLength;
75 @property (assign) linenoiseCompletionCallback *completionCallback;
76
77 + (Linenoise*)sharedLinenoise;
78
79 - (OFString*)readInputWithPrompt: (OFString*)prompt;
80 - (void)refreshLine;
81 - (void)clearScreen;
82 - (void)setCompletionCallback: (linenoiseCompletionCallback*)fn;
83 - (int)addHistoryItem: (OFString*)line;
84 - (void)saveHistoryToFile: (OFString*)filename;
85 - (void)loadHistoryFromFile: (OFString*)filename;
86
87 - (int)LN_editInsertCharacter: (int)c;
88 - (void)LN_editMoveLeft;
89 - (void)LN_editMoveRight;
90 - (void)LN_editHistoryNextInDirection: (enum linenoiseDirection)dir;
91 - (void)LN_editDelete;
92 - (void)LN_editDeletePreviousWord;
93 - (OFString*)LN_editWithFD: (int)fd
94                     prompt: (OFString*)prompt;
95 - (OFString*)LN_editRawWithPrompt: (OFString*)prompt;
96
97 - (int)LN_completeLine;
98 - (void)LN_refreshSingleLine;
99 - (void)LN_refreshMultiLine;
100 - (bool)LN_isUnsupportedTerm;
101 - (int)LN_enableRawModeForFD: (int)fd;
102 - (void)LN_disableRawModeForFD: (int)fd;
103 - (int)LN_getColumns;
104 - (void)LN_beep;
105 @end