Skip to content

Commit

Permalink
Implements the 'times' builtin (#580)
Browse files Browse the repository at this point in the history
Uses os.times() underneath, which uses floating point objects
  • Loading branch information
timetoplatypus authored and andychu committed Jan 21, 2020
1 parent 72fd055 commit 393b705
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions bin/oil.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ def ShellMain(lang, argv0, argv, login_shell):
builtin_e.DIRS: builtin.Dirs(mem, dir_stack, errfmt),
builtin_e.PWD: builtin.Pwd(mem, errfmt),

builtin_e.TIMES: builtin.Times(),
builtin_e.READ: builtin.Read(splitter, mem),
builtin_e.HELP: builtin.Help(loader, errfmt),
builtin_e.HISTORY: builtin.History(line_input),
Expand Down
15 changes: 14 additions & 1 deletion osh/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import libc
import posix_ as posix

import os

from mycpp import mylib
if mylib.PYTHON:
# Hack because we don't want libcmark.so dependency for build/dev.sh minimal
Expand All @@ -67,7 +69,7 @@

"set": builtin_e.SET,
"shift": builtin_e.SHIFT,
#"times": builtin_e.TIMES, # no implemented
"times": builtin_e.TIMES,
"trap": builtin_e.TRAP,
"unset": builtin_e.UNSET,

Expand Down Expand Up @@ -277,6 +279,17 @@ def _AppendParts(s, spans, max_results, join_next, parts):
return done, join_next


TIMES_SPEC = _Register('times')

class Times(object):
def __call__(self, arg_vec):
utime, stime, cutime, cstime, elapsed = os.times()
print("%dm%1.3fs %dm%1.3fs" % (utime / 60, utime % 60, stime / 60, stime % 60))
print("%dm%1.3fs %dm%1.3fs" % (cutime / 60, cutime % 60, cstime / 60, cstime % 60))

return 0


READ_SPEC = _Register('read')
READ_SPEC.ShortFlag('-r')
READ_SPEC.ShortFlag('-n', args.Int)
Expand Down
2 changes: 1 addition & 1 deletion osh/runtime.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module runtime
{
builtin =
NONE | READ | ECHO | PRINTF | SHIFT
NONE | TIMES | READ | ECHO | PRINTF | SHIFT
| CD | PWD | PUSHD | POPD | DIRS
| EXPORT | READONLY | LOCAL | DECLARE | TYPESET
| UNSET | SET | SHOPT
Expand Down
16 changes: 16 additions & 0 deletions spec/builtin-times.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

#### `times` yields two formatted lines
output=$(times)
echo "$output" | while read line
do
echo "$line" | egrep -q '[0-9]+m[0-9]+.[0-9]{3}s [0-9]+m[0-9]+.[0-9]{3}s' && echo "pass"
done

echo "$output" | wc -l
## status: 0
## STDOUT:
pass
pass
2
## END
4 changes: 4 additions & 0 deletions test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ builtin-special() {
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}

builtin-times() {
sh-spec spec/builtin-times.test.sh $BASH $ZSH $OSH_LIST "$@"
}

command-parsing() {
sh-spec spec/command-parsing.test.sh ${REF_SHELLS[@]} $OSH_LIST "$@"
}
Expand Down

0 comments on commit 393b705

Please sign in to comment.