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.
4 * See linenoise.m for more information.
6 * ------------------------------------------------------------------------
8 * Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
9 * Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
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.
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.
37 #import <ObjFW/ObjFW.h>
42 typedef void(linenoiseCompletionCallback)(OFString *, OFList*);
44 enum linenoiseDirection {
45 LINENOISE_HISTORY_NEXT,
46 LINENOISE_HISTORY_PREV
49 @interface Linenoise : OFObject <OFApplicationDelegate>
53 bool _rawmode; // For atexit() function to check if restore is needed
54 struct termios _orig_termios; // In order to restore at exit
56 linenoiseCompletionCallback *_completionCallback;
58 size_t _maximalHistoryLength;
59 ssize_t _historyIndex; // The history index we are currently editing
60 OFMutableArray *_history;
62 OFFile *_term; // Terminal file descriptor
64 OFMutableString *_buf; // Edited line buffer
65 OFString *_prompt; // Prompt to display
67 size_t _pos; // Current cursor position
68 size_t _oldpos; // Previous refresh cursor position
70 size_t _cols; // Number of columns in terminal
71 size_t _maxrows; // Maximum num of rows used so far (multiline)
73 @property (assign) bool multiline;
74 @property (assign) size_t maximalHistoryLength;
75 @property (assign) linenoiseCompletionCallback *completionCallback;
77 + (Linenoise*)sharedLinenoise;
79 - (OFString*)readInputWithPrompt: (OFString*)prompt;
82 - (void)setCompletionCallback: (linenoiseCompletionCallback*)fn;
83 - (int)addHistoryItem: (OFString*)line;
84 - (void)saveHistoryToFile: (OFString*)filename;
85 - (void)loadHistoryFromFile: (OFString*)filename;
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;
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;