mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +00:00
Shell: Add a "time" builtin to show how long a command took to run
This commit is contained in:
parent
f2b6e1b577
commit
3596522d23
1 changed files with 25 additions and 0 deletions
|
@ -25,6 +25,8 @@
|
||||||
GlobalState g;
|
GlobalState g;
|
||||||
static LineEditor editor;
|
static LineEditor editor;
|
||||||
|
|
||||||
|
static int run_command(const String&);
|
||||||
|
|
||||||
static String prompt()
|
static String prompt()
|
||||||
{
|
{
|
||||||
if (g.uid == 0)
|
if (g.uid == 0)
|
||||||
|
@ -133,6 +135,25 @@ static int sh_history(int, char**)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int sh_time(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc == 1) {
|
||||||
|
printf("usage: time <command>\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
StringBuilder builder;
|
||||||
|
for (int i = 1; i < argc; ++i) {
|
||||||
|
builder.append(argv[i]);
|
||||||
|
if (i != argc - 1)
|
||||||
|
builder.append(' ');
|
||||||
|
}
|
||||||
|
CElapsedTimer timer;
|
||||||
|
timer.start();
|
||||||
|
int exit_code = run_command(builder.to_string());
|
||||||
|
printf("Time: %d ms\n", timer.elapsed());
|
||||||
|
return exit_code;
|
||||||
|
}
|
||||||
|
|
||||||
static int sh_umask(int argc, char** argv)
|
static int sh_umask(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
|
@ -392,6 +413,10 @@ static bool handle_builtin(int argc, char** argv, int& retval)
|
||||||
retval = sh_popd(argc, argv);
|
retval = sh_popd(argc, argv);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (!strcmp(argv[0], "time")) {
|
||||||
|
retval = sh_time(argc, argv);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue