MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / ReadLine

Function ReadLine

Applications/LSh/main.cpp:64–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

62builtin_t builtinClear = {.name = "clear", .func = LShBuiltin_Clear};
63
64void ReadLine(){
65 tcsetattr(STDOUT_FILENO, TCSANOW, &readAttributes);
66
67 bool esc = false;
68 bool csi = false;
69 int lnPos = 0;
70 ln.clear();
71
72 for(int i = 0; ; i++){
73 char ch;
74 while((ch = getchar()) == EOF);
75
76 if(esc){
77 if(ch == '['){
78 csi = true;
79 }
80 esc = false;
81 } else if (csi) {
82 switch(ch){
83 case 'A': // Cursor Up
84 break;
85 case 'B': // Cursor Down
86 break;
87 case 'C': // Cursor Right
88 if(lnPos < static_cast<int>(ln.length())){
89 lnPos++;
90 printf("\e[C");
91 }
92 break;
93 case 'D': // Cursor Left
94 lnPos--;
95 if(lnPos < 0){
96 lnPos = 0;
97 } else {
98 printf("\e[D");
99 }
100 break;
101 case 'F': // End
102 printf("\e[%uC", static_cast<int>(ln.length()) - lnPos);
103 lnPos = ln.length();
104 break;
105 case 'H': // Home
106 printf("\e[%dD", lnPos);
107 lnPos = 0;
108 break;
109 }
110 csi = false;
111 } else if(ch == '\b'){
112 if(lnPos > 0){
113 lnPos--;
114 ln.erase(lnPos, 1);
115 putc(ch, stdout);
116 }
117 } else if(ch == '\n'){
118 putc(ch, stdout);
119 break;
120 } else if(ch == '\e'){
121 esc = true;

Callers 1

mainFunction · 0.85

Calls 3

lengthMethod · 0.80
clearMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected