Skip to content

Commit

Permalink
更新Readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
min0911Y committed Dec 12, 2024
1 parent f3a381e commit 0e3f405
Showing 1 changed file with 3 additions and 96 deletions.
99 changes: 3 additions & 96 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,9 @@

## Example

**NOTE**: This example is for Linux only.
See `example/echo.c` for details.

```c
#include <pl_readline.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termio.h>

int getch(void) {
struct termios tm, tm_old;
int fd = 0, ch;

if (tcgetattr(fd, &tm) < 0) return -1; // 保存现在的终端设置

tm_old = tm;
cfmakeraw(&tm); // 更改终端设置为原始模式,让所有输入数据以字节为单位处理

if (tcsetattr(fd, TCSANOW, &tm) < 0) { // 设置上更改之后的设置
return -1;
}

ch = getchar();
if (tcsetattr(fd, TCSANOW, &tm_old) < 0) { // 更改设置为最初的样子
return -1;
}
if (ch == 0x0d) {
return PL_READLINE_KEY_ENTER;
}
if (ch == 0x7f) {
return PL_READLINE_KEY_BACKSPACE;
}
if (ch == 0x9) {
return PL_READLINE_KEY_TAB;
}
if (ch == 0x1b) {
ch = getch();
if (ch == '[') {
ch = getch();
switch (ch) {
case 'A':
return PL_READLINE_KEY_UP;
case 'B':
return PL_READLINE_KEY_DOWN;
case 'C':
return PL_READLINE_KEY_RIGHT;
case 'D':
return PL_READLINE_KEY_LEFT;
case 'H':
return PL_READLINE_KEY_HOME;
case 'F':
return PL_READLINE_KEY_END;
case '5':
ch = getch();
if (ch == '~') {
return PL_READLINE_KEY_PAGE_UP;
}
return -1;
case '6':
ch = getch();
if (ch == '~') {
return PL_READLINE_KEY_PAGE_DOWN;
}
return -1;
}
}
}
return ch;
}

void flush() { fflush(stdout); }

void handle_tab(char *buf, pl_readline_words_t words) {
pl_readline_word_maker_add("hello", words, true, ' ');
pl_readline_word_maker_add("world", words, false, ' ');
pl_readline_word_maker_add("foo", words, false, ' ');
pl_readline_word_maker_add("bar", words, false, ' ');
pl_readline_word_maker_add("baz", words, false, ' ');
pl_readline_word_maker_add("qux", words, false, ' ');
}

int main() {
pl_readline_t n =
pl_readline_init(getch, (void *)putchar, flush, handle_tab);
char *buffer = malloc(255);
while (1) {
pl_readline(n, "input: ", buffer, 255);
printf("you input: %s\n", buffer);
if (strcmp(buffer, "exit") == 0)
break;
}
pl_readline_uninit(n);
free(buffer);
}
```
**NOTE**: This example is for Linux only.

## Feature

Expand Down Expand Up @@ -143,4 +50,4 @@ int main() {

## Additional

**HAVE FUN! 祝你玩的开心!**
**HAVE FUN! 祝你玩的开心!**

0 comments on commit 0e3f405

Please sign in to comment.