forked from antirez/linenoise
-
Notifications
You must be signed in to change notification settings - Fork 10
/
linenoise.3
148 lines (124 loc) · 3.91 KB
/
linenoise.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
.Dd $Mdocdate$
.Dt linenoise 3
.Os
.Sh NAME
.Nm linenoise
.Nd readline replacement
.Sh SYNOPSIS
.In linenoise.h
Link with
.Ar -llinenoise
.Ft char *
.Fn linenoise "const char *prompt"
.Ft void
.Fn linenoiseFree "void *ptr"
.Ft void
.Fn linenoiseSetMultiLine "int ml"
.Ft int
.Fn linenoiseHistoryAdd "const char *line"
.Ft int
.Fn linenoiseHistorySetMaxLen "int len"
.Ft int
.Fn linenoiseHistorySave "const char *filename"
.Ft int
.Fn linenoiseHistoryLoad "const char *filename"
.Ft void
.Fn linenoiseSetCompletionCallback "linenoiseCompletionCallback *"
.Ft void
.Fn linenoiseAddCompletion "linenoiseCompletions *" "const char *"
.Ft void
.Fn linenoiseSetHintsCallback "linenoiseHintsCallback *"
.Ft void
.Fn linenoiseSetFreeHintsCallback "linenoiseFreeHintsCallback *"
.Ft void
.Fn linenoiseClearScreen "void"
.Ft void
.Fn linenoisePrintKeyCodes "void"
.Sh DESCRIPTION
.Fn linenoise
shows the specified prompt to the user and takes input with line editing and
history capabilities.
It returns a buffer with the users input, or NULL on eof or out of memory.
The buffer must be freed.
.Fn linenoiseFree
If your program uses a different dynamic allocation library, you may also use
.Fn linenoiseFree
to make sure the line is freed with the same allocator it was created.
.Fn linenoiseSetMultiLine
set or unset multiline editing, where multiple screens rows are used.
Enable by passing `1` and disable with `0`.
When disabled the text will scroll towards left as the user types more.
.Fn linenoiseHistoryAdd
adds a new element to the top of the history.
It will be the first the user sees when using the up arrow.
.Fn linenoiseHistorySetMaxLen
sets a length for the history.
Default is zero, meaning history is disabled.
.Fn linenoiseHistorySave
saves the history into a file, returning -1 on error and 0 on success.
.Fn linenoiseHistoryLoad
loads a history file, returning -1 on error and 0 on success.
.Fn linenoiseSetCompletionCallback
sets the callback function to be used when the user presses the TAB key.
The callback is implemented like
.Ft void
.Fn completion "const char *buf" "linenoiseCompletions *lc"
where buf is the users input and lc is used for adding completions.
.Fn linenoiseAddCompletion
may be used in the completion callback to add completions.
It can be called several times to add to the list of completions
.Fn linenoiseSetHintsCallback
specifies a callback function that can be used for hints.
Hints try to guess usefull completions to what the user is typing.
the callback is implemented like
.Ft char
.Fn *hints "const char *buf" "int *color" "int *bold"
where buf is the users input, color and bold specifies aesthetics.
.Fn linenoiseSetFreeHintsCallback
sets a deallocater to free the returned hint if it was dynamically allocated.
.Fn linenoiseClearScreen
clears the screen.
.Fn linenoisePrintKeyCodes
is used in debugging mode to print key codes.
.Ss Input length
When a tty is detected (user typing into terminal), maximum editable
line length is `LINENOISE_MAX_LINE`,
otherwise (pipes or redirection) there are no limits.
.Ss xterm color terminal codes
.Bd -literal
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
white = 37;
.Ed
.Sh EXAMPLES
.Ss Canonical loop in program using Linenoise
.Bd -literal
while((line = linenoise("hello> ")) != NULL) {
printf("You wrote: %s\n", line);
linenoiseFree(line); /* Or free(line) for libc malloc. */
}
.Ed
.Ss Completion callback function
.Bd -literal
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
}
}
.Ed
.Ss Hint callback function
.Bd -literal
char *hints(const char *buf, int *color, int *bold) {
if (!strcasecmp(buf,"git remote add")) {
*color = 35;
*bold = 0;
return " <name> <url>";
}
return NULL;
}
.Ed