mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 02:47:34 +00:00
top: Add support for quitting top by pressing q
This commit is contained in:
parent
3818840ee6
commit
cd2f67c4ea
1 changed files with 29 additions and 1 deletions
|
@ -177,6 +177,27 @@ static void parse_args(Main::Arguments arguments, TopOption& top_option)
|
|||
args_parser.parse(arguments);
|
||||
}
|
||||
|
||||
static bool check_quit()
|
||||
{
|
||||
char c = '\0';
|
||||
read(STDIN_FILENO, &c, sizeof(c));
|
||||
return c == 'q' || c == 'Q';
|
||||
}
|
||||
|
||||
static int g_old_stdin;
|
||||
|
||||
static void restore_stdin()
|
||||
{
|
||||
fcntl(STDIN_FILENO, F_SETFL, g_old_stdin);
|
||||
}
|
||||
|
||||
static void enable_nonblocking_stdin()
|
||||
{
|
||||
g_old_stdin = fcntl(STDIN_FILENO, F_GETFL);
|
||||
fcntl(STDIN_FILENO, F_SETFL, g_old_stdin | O_NONBLOCK);
|
||||
atexit(restore_stdin);
|
||||
}
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio rpath tty sigaction"));
|
||||
|
@ -192,6 +213,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
TopOption top_option;
|
||||
parse_args(arguments, top_option);
|
||||
|
||||
enable_nonblocking_stdin();
|
||||
|
||||
Vector<ThreadData*> threads;
|
||||
auto prev = get_snapshot();
|
||||
usleep(10000);
|
||||
|
@ -277,6 +300,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
threads.clear_with_capacity();
|
||||
prev = move(current);
|
||||
sleep(top_option.delay_time);
|
||||
|
||||
for (int sleep_slice = 0; sleep_slice < top_option.delay_time * 1000; sleep_slice += 100) {
|
||||
if (check_quit())
|
||||
exit(0);
|
||||
usleep(100 * 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue