mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:37:34 +00:00
Userland: Less: emulate cat when stdout is not a tty
This is the most logical behavior when less is used in a pipe.
This commit is contained in:
parent
1ec061d666
commit
a11658737a
1 changed files with 23 additions and 2 deletions
|
@ -286,10 +286,26 @@ static String get_key_sequence()
|
||||||
return String(buff, n);
|
return String(buff, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cat_file(FILE* file)
|
||||||
|
{
|
||||||
|
ByteBuffer buffer = ByteBuffer::create_uninitialized(4096);
|
||||||
|
while (!feof(file)) {
|
||||||
|
size_t n = fread(buffer.data(), 1, buffer.size(), file);
|
||||||
|
if (n == 0 && ferror(file)) {
|
||||||
|
perror("fread");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
n = fwrite(buffer.data(), 1, n, stdout);
|
||||||
|
if (n == 0 && ferror(stdout)) {
|
||||||
|
perror("fwrite");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
VERIFY(isatty(STDOUT_FILENO));
|
|
||||||
|
|
||||||
char const* filename = "-";
|
char const* filename = "-";
|
||||||
char const* prompt = "?f%f :.(line %l)?e (END):.";
|
char const* prompt = "?f%f :.(line %l)?e (END):.";
|
||||||
bool dont_switch_buffer = false;
|
bool dont_switch_buffer = false;
|
||||||
|
@ -321,6 +337,11 @@ int main(int argc, char** argv)
|
||||||
prompt = "--More--";
|
prompt = "--More--";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isatty(STDOUT_FILENO)) {
|
||||||
|
cat_file(file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
setup_tty(!dont_switch_buffer);
|
setup_tty(!dont_switch_buffer);
|
||||||
|
|
||||||
Pager pager(file, stdout, g_wsize.ws_col, g_wsize.ws_row);
|
Pager pager(file, stdout, g_wsize.ws_col, g_wsize.ws_row);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue