Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format example for better readability #228

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
linenoise_example: linenoise.h linenoise.c
all: linenoise.h linenoise.c

linenoise_example: linenoise.c example.c
all: linenoise.c example.c
$(CC) -Wall -W -Os -g -o linenoise_example linenoise.c example.c

clean:
Expand Down
56 changes: 28 additions & 28 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
linenoiseAddCompletion(lc, "hello");
linenoiseAddCompletion(lc, "hello there");
}
}

char *hints(const char *buf, int *color, int *bold) {
if (!strcasecmp(buf,"hello")) {
if (!strcasecmp(buf, "hello")) {
*color = 35;
*bold = 0;
return " World";
Expand All @@ -26,16 +26,16 @@ int main(int argc, char **argv) {
int async = 0;

/* Parse options, with --multiline we enable multi line editing. */
while(argc > 1) {
while (argc > 1) {
argc--;
argv++;
if (!strcmp(*argv,"--multiline")) {
if (!strcmp(*argv, "--multiline")) {
linenoiseSetMultiLine(1);
printf("Multi-line mode enabled.\n");
} else if (!strcmp(*argv,"--keycodes")) {
} else if (!strcmp(*argv, "--keycodes")) {
linenoisePrintKeyCodes();
exit(0);
} else if (!strcmp(*argv,"--async")) {
} else if (!strcmp(*argv, "--async")) {
async = 1;
} else {
fprintf(stderr, "Usage: %s [--multiline] [--keycodes] [--async]\n", prgname);
Expand All @@ -59,7 +59,7 @@ int main(int argc, char **argv) {
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */

while(1) {
while (1) {
if (!async) {
line = linenoise("hello> ");
if (line == NULL) break;
Expand All @@ -69,34 +69,34 @@ int main(int argc, char **argv) {
* 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;
linenoiseEditStart(&ls, -1, -1, buf, sizeof(buf), "hello> ");
while (1) {
fd_set readfds;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabs to whitespaces for consistent indent

struct timeval tv;
int retval;

FD_ZERO(&readfds);
FD_SET(ls.ifd, &readfds);
tv.tv_sec = 1; // 1 sec timeout
tv.tv_usec = 0;
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()");
retval = select(ls.ifd + 1, &readfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
exit(1);
} else if (retval) {
line = linenoiseEditFeed(&ls);
} 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
} else {
// Timeout occurred
static int counter = 0;
linenoiseHide(&ls);
printf("Async output %d.\n", counter++);
printf("Async output %d.\n", counter++);
linenoiseShow(&ls);
}
}
}
linenoiseEditStop(&ls);
if (line == NULL) exit(0); /* Ctrl+D/C. */
Expand All @@ -107,9 +107,9 @@ int main(int argc, char **argv) {
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)) {
} else if (!strncmp(line, "/historylen", 11)) {
/* The "/historylen" command will change the history len. */
int len = atoi(line+11);
int len = atoi(line + 11);
linenoiseHistorySetMaxLen(len);
} else if (!strncmp(line, "/mask", 5)) {
linenoiseMaskModeEnable();
Expand Down