Shell: use printf and putchar instead of fprintf and fputc

This commit is contained in:
Bananymous 2024-09-14 22:37:09 +03:00
parent abed41b8fa
commit 4ca147699d
1 changed files with 18 additions and 20 deletions

View File

@ -4,8 +4,6 @@
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <ctype.h> #include <ctype.h>
#include <inttypes.h>
#include <math.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -735,7 +733,7 @@ int prompt_length()
void print_prompt() void print_prompt()
{ {
auto prompt = get_prompt(); auto prompt = get_prompt();
fprintf(stdout, "%.*s", (int)prompt.size(), prompt.data()); printf("%.*s", (int)prompt.size(), prompt.data());
fflush(stdout); fflush(stdout);
} }
@ -807,7 +805,7 @@ int main(int argc, char** argv)
buffers = history; buffers = history;
MUST(buffers.emplace_back(""_sv)); MUST(buffers.emplace_back(""_sv));
col = 0; col = 0;
printf("\n"); putchar('\n');
print_prompt(); print_prompt();
continue; continue;
} }
@ -820,11 +818,11 @@ int main(int argc, char** argv)
ASSERT((ch & 0xC0) == 0x80); ASSERT((ch & 0xC0) == 0x80);
fputc(ch, stdout); putchar(ch);
MUST(buffers[index].insert(ch, col++)); MUST(buffers[index].insert(ch, col++));
if (waiting_utf8 == 0) if (waiting_utf8 == 0)
{ {
fprintf(stdout, "\e[s%s\e[u", buffers[index].data() + col); printf("\e[s%s\e[u", buffers[index].data() + col);
fflush(stdout); fflush(stdout);
} }
continue; continue;
@ -840,7 +838,7 @@ int main(int argc, char** argv)
else else
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
fputc(ch, stdout); putchar(ch);
MUST(buffers[index].insert(ch, col++)); MUST(buffers[index].insert(ch, col++));
continue; continue;
} }
@ -854,18 +852,18 @@ int main(int argc, char** argv)
ch = getchar(); ch = getchar();
switch (ch) switch (ch)
{ {
case 'A': if (index > 0) { index--; col = buffers[index].size(); fprintf(stdout, "\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break; case 'A': if (index > 0) { index--; col = buffers[index].size(); printf("\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break;
case 'B': if (index < buffers.size() - 1) { index++; col = buffers[index].size(); fprintf(stdout, "\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break; case 'B': if (index < buffers.size() - 1) { index++; col = buffers[index].size(); printf("\e[%dG%s\e[K", prompt_length() + 1, buffers[index].data()); fflush(stdout); } break;
case 'C': if (col < buffers[index].size()) { col++; while ((buffers[index][col - 1] & 0xC0) == 0x80) col++; fprintf(stdout, "\e[C"); fflush(stdout); } break; case 'C': if (col < buffers[index].size()) { col++; while ((buffers[index][col - 1] & 0xC0) == 0x80) col++; printf("\e[C"); fflush(stdout); } break;
case 'D': if (col > 0) { while ((buffers[index][col - 1] & 0xC0) == 0x80) col--; col--; fprintf(stdout, "\e[D"); fflush(stdout); } break; case 'D': if (col > 0) { while ((buffers[index][col - 1] & 0xC0) == 0x80) col--; col--; printf("\e[D"); fflush(stdout); } break;
} }
break; break;
case '\x0C': // ^L case '\x0C': // ^L
{ {
int x = prompt_length() + character_length(buffers[index].sv().substring(col)) + 1; int x = prompt_length() + character_length(buffers[index].sv().substring(col)) + 1;
fprintf(stdout, "\e[H\e[J"); printf("\e[H\e[J");
print_prompt(); print_prompt();
fprintf(stdout, "%s\e[u\e[1;%dH", buffers[index].data(), x); printf("%s\e[u\e[1;%dH", buffers[index].data(), x);
fflush(stdout); fflush(stdout);
break; break;
} }
@ -875,27 +873,27 @@ int main(int argc, char** argv)
while ((buffers[index][col - 1] & 0xC0) == 0x80) while ((buffers[index][col - 1] & 0xC0) == 0x80)
buffers[index].remove(--col); buffers[index].remove(--col);
buffers[index].remove(--col); buffers[index].remove(--col);
fprintf(stdout, "\b\e[s%s \e[u", buffers[index].data() + col); printf("\b\e[s%s \e[u", buffers[index].data() + col);
fflush(stdout); fflush(stdout);
} }
break; break;
case '\x01': // ^A case '\x01': // ^A
col = 0; col = 0;
fprintf(stdout, "\e[%dG", prompt_length() + 1); printf("\e[%dG", prompt_length() + 1);
fflush(stdout); fflush(stdout);
break; break;
case '\x03': // ^C case '\x03': // ^C
fputc('\n', stdout); putchar('\n');
print_prompt(); print_prompt();
buffers[index].clear(); buffers[index].clear();
col = 0; col = 0;
break; break;
case '\x04': // ^D case '\x04': // ^D
fprintf(stdout, "\n"); putchar('\n');
clean_exit(0); clean_exit(0);
break; break;
case '\n': case '\n':
fputc('\n', stdout); putchar('\n');
if (!buffers[index].empty()) if (!buffers[index].empty())
{ {
last_return = parse_and_execute_command(buffers[index]); last_return = parse_and_execute_command(buffers[index]);
@ -913,9 +911,9 @@ int main(int argc, char** argv)
default: default:
MUST(buffers[index].insert(ch, col++)); MUST(buffers[index].insert(ch, col++));
if (col == buffers[index].size()) if (col == buffers[index].size())
fputc(ch, stdout); putchar(ch);
else else
fprintf(stdout, "%c\e[s%s\e[u", ch, buffers[index].data() + col); printf("%c\e[s%s\e[u", ch, buffers[index].data() + col);
fflush(stdout); fflush(stdout);
break; break;
} }