forked from antirez/linenoise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
executable file
·152 lines (138 loc) · 4.63 KB
/
example.c
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
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include "linenoise.h"
#define UTF8
#ifdef UTF8
#include "encodings/utf8.h"
#endif
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
#ifdef UTF8
linenoiseAddCompletion(lc,"hello こんにちは");
linenoiseAddCompletion(lc,"hello こんにちは there");
linenoiseAddCompletion(lc,"hello こんにちは 👨💻");
#else
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
#endif
}
}
char *hints(const char *buf, int *color, int *bold) {
if (!strcasecmp(buf,"hello")) {
*color = 35;
*bold = 0;
return " World";
}
if (!strcasecmp(buf,"こんにちは")) {
*color = 35;
*bold = 0;
return " 世界";
}
return NULL;
}
int main(int argc, char **argv) {
char *line;
char *prgname = argv[0];
int async = 0;
/* Parse options, with --multiline we enable multi line editing. */
while(argc > 1) {
argc--;
argv++;
if (!strcmp(*argv,"--multiline")) {
linenoiseSetMultiLine(1);
printf("Multi-line mode enabled.\n");
} else if (!strcmp(*argv,"--keycodes")) {
linenoisePrintKeyCodes();
exit(0);
} else if (!strcmp(*argv,"--async")) {
async = 1;
} else {
fprintf(stderr, "Usage: %s [--multiline] [--keycodes] [--async]\n", prgname);
exit(1);
}
}
#ifdef UTF8
linenoiseSetEncodingFunctions(
linenoiseUtf8PrevCharLen,
linenoiseUtf8NextCharLen,
linenoiseUtf8ReadCode);
#endif
/* Set the completion callback. This will be called every time the
* user uses the <tab> key. */
linenoiseSetCompletionCallback(completion);
linenoiseSetHintsCallback(hints);
/* Load history from file. The history file is just a plain text file
* where entries are separated by newlines. */
linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
/* Now this is the main loop of the typical linenoise-based application.
* The call to linenoise() will block as long as the user types something
* and presses enter.
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
while(1) {
if (!async) {
#ifdef UTF8
line = linenoise("\033[32mこんにちは\x1b[0m> ");
#else
line = linenoise("\033[32mhello\x1b[0m> ");
#endif
if (line == NULL) break;
} else {
/* Asynchronous mode using the multiplexing API: wait for
* data on stdin, and simulate async data coming from some source
* using the select(2) timeout. */
struct linenoiseState ls;
char buf[1024];
linenoiseEditStart(&ls,-1,-1,buf,sizeof(buf),"hello> ");
while(1) {
fd_set readfds;
struct timeval tv;
int retval;
FD_ZERO(&readfds);
FD_SET(ls.ifd, &readfds);
tv.tv_sec = 1; // 1 sec timeout
tv.tv_usec = 0;
retval = select(ls.ifd+1, &readfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
exit(1);
} else if (retval) {
line = linenoiseEditFeed(&ls);
/* A NULL return means: line editing is continuing.
* Otherwise the user hit enter or stopped editing
* (CTRL+C/D). */
if (line != linenoiseEditMore) break;
} else {
// Timeout occurred
static int counter = 0;
linenoiseHide(&ls);
printf("Async output %d.\n", counter++);
linenoiseShow(&ls);
}
}
linenoiseEditStop(&ls);
if (line == NULL) exit(0); /* Ctrl+D/C. */
}
/* Do something with the string. */
if (line[0] != '\0' && line[0] != '/') {
printf("echo: '%s'\n", line);
linenoiseHistoryAdd(line); /* Add to the history. */
linenoiseHistorySave("history.txt"); /* Save the history on disk. */
} else if (!strncmp(line,"/historylen",11)) {
/* The "/historylen" command will change the history len. */
int len = atoi(line+11);
linenoiseHistorySetMaxLen(len);
} else if (!strncmp(line, "/mask", 5)) {
linenoiseMaskModeEnable();
} else if (!strncmp(line, "/unmask", 7)) {
linenoiseMaskModeDisable();
} else if (line[0] == '/') {
printf("Unreconized command: %s\n", line);
}
free(line);
}
return 0;
}