2013-10-01 21:34:49 +08:00
|
|
|
package eval
|
|
|
|
|
2019-04-19 05:57:14 +08:00
|
|
|
// Misc builtin functions.
|
2014-04-02 10:51:09 +08:00
|
|
|
|
2013-10-02 22:10:07 +08:00
|
|
|
import (
|
2020-08-18 13:40:30 +08:00
|
|
|
"errors"
|
2014-01-16 09:24:14 +08:00
|
|
|
"fmt"
|
2016-03-17 22:28:00 +08:00
|
|
|
"math/rand"
|
2016-11-06 17:47:20 +08:00
|
|
|
"net"
|
2021-08-23 07:36:26 +08:00
|
|
|
"os"
|
2020-08-17 06:02:51 +08:00
|
|
|
"sync"
|
2016-02-10 01:18:18 +08:00
|
|
|
"time"
|
2018-03-01 10:50:27 +08:00
|
|
|
"unicode/utf8"
|
2016-02-14 04:05:35 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/diag"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/parse"
|
2013-10-02 22:10:07 +08:00
|
|
|
)
|
|
|
|
|
2021-05-22 11:51:03 +08:00
|
|
|
var (
|
|
|
|
ErrNegativeSleepDuration = errors.New("sleep duration must be >= zero")
|
|
|
|
ErrInvalidSleepDuration = errors.New("invalid sleep duration")
|
|
|
|
)
|
|
|
|
|
2017-12-17 13:20:03 +08:00
|
|
|
// Builtins that have not been put into their own groups go here.
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func init() {
|
|
|
|
addBuiltinFns(map[string]interface{}{
|
|
|
|
"nop": nop,
|
|
|
|
"kind-of": kindOf,
|
|
|
|
"constantly": constantly,
|
|
|
|
|
|
|
|
"resolve": resolve,
|
|
|
|
|
2020-08-17 06:02:51 +08:00
|
|
|
"eval": eval,
|
2020-08-17 07:11:25 +08:00
|
|
|
"use-mod": useMod,
|
2020-08-17 01:46:29 +08:00
|
|
|
|
2021-01-24 23:32:24 +08:00
|
|
|
"deprecate": deprecate,
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
// Time
|
2021-04-09 06:07:50 +08:00
|
|
|
"sleep": sleep,
|
|
|
|
"time": timeCmd,
|
2020-08-17 01:46:29 +08:00
|
|
|
|
|
|
|
"-ifaddrs": _ifaddrs,
|
|
|
|
})
|
|
|
|
|
|
|
|
// For rand and randint.
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn nop
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// nop &any-opt= $value...
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Accepts arbitrary arguments and options and does exactly nothing.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> nop
|
|
|
|
// ~> nop a b c
|
|
|
|
// ~> nop &k=v
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Etymology: Various languages, in particular NOP in
|
|
|
|
// [assembly languages](https://en.wikipedia.org/wiki/NOP).
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func nop(opts RawOptions, args ...interface{}) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn kind-of
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// kind-of $value...
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Output the kinds of `$value`s. Example:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> kind-of lorem [] [&]
|
|
|
|
// ▶ string
|
|
|
|
// ▶ list
|
|
|
|
// ▶ map
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// The terminology and definition of "kind" is subject to change.
|
|
|
|
|
2021-06-18 07:11:51 +08:00
|
|
|
func kindOf(fm *Frame, args ...interface{}) error {
|
|
|
|
out := fm.ValueOutput()
|
2020-08-17 01:46:29 +08:00
|
|
|
for _, a := range args {
|
2021-06-18 07:11:51 +08:00
|
|
|
err := out.Put(vals.Kind(a))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
2021-06-18 07:11:51 +08:00
|
|
|
return nil
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn constantly
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// constantly $value...
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Output a function that takes no arguments and outputs `$value`s when called.
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> f=(constantly lorem ipsum)
|
|
|
|
// ~> $f
|
|
|
|
// ▶ lorem
|
|
|
|
// ▶ ipsum
|
|
|
|
// ```
|
|
|
|
//
|
2021-11-28 11:24:42 +08:00
|
|
|
// The above example is actually equivalent to simply `f = { put lorem ipsum }`;
|
2020-01-18 21:12:50 +08:00
|
|
|
// it is most useful when the argument is **not** a literal value, e.g.
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> f = (constantly (uname))
|
|
|
|
// ~> $f
|
|
|
|
// ▶ Darwin
|
|
|
|
// ~> $f
|
|
|
|
// ▶ Darwin
|
|
|
|
// ```
|
|
|
|
//
|
2021-11-28 11:24:42 +08:00
|
|
|
// The above code only calls `uname` once, while if you do `f = { put (uname) }`,
|
2020-01-18 21:12:50 +08:00
|
|
|
// every time you invoke `$f`, `uname` will be called.
|
|
|
|
//
|
|
|
|
// Etymology: [Clojure](https://clojuredocs.org/clojure.core/constantly).
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func constantly(args ...interface{}) Callable {
|
|
|
|
// TODO(xiaq): Repr of this function is not right.
|
|
|
|
return NewGoFn(
|
|
|
|
"created by constantly",
|
2021-06-18 07:11:51 +08:00
|
|
|
func(fm *Frame) error {
|
|
|
|
out := fm.ValueOutput()
|
2020-08-17 01:46:29 +08:00
|
|
|
for _, v := range args {
|
2021-06-18 07:11:51 +08:00
|
|
|
err := out.Put(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
2021-06-18 07:11:51 +08:00
|
|
|
return nil
|
2020-08-17 01:46:29 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn resolve
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// resolve $command
|
|
|
|
// ```
|
|
|
|
//
|
2020-10-18 11:58:06 +08:00
|
|
|
// Output what `$command` resolves to in symbolic form. Command resolution is
|
|
|
|
// described in the [language reference](language.html#ordinary-command).
|
2020-01-18 21:12:50 +08:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> resolve echo
|
|
|
|
// ▶ <builtin echo>
|
|
|
|
// ~> fn f { }
|
|
|
|
// ~> resolve f
|
|
|
|
// ▶ <closure 0xc4201c24d0>
|
|
|
|
// ~> resolve cat
|
|
|
|
// ▶ <external cat>
|
|
|
|
// ```
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func resolve(fm *Frame, head string) string {
|
2020-12-26 05:30:57 +08:00
|
|
|
special, fnRef := resolveCmdHeadInternally(fm, head, nil)
|
|
|
|
switch {
|
|
|
|
case special != nil:
|
2020-08-17 01:46:29 +08:00
|
|
|
return "special"
|
2020-12-26 05:30:57 +08:00
|
|
|
case fnRef != nil:
|
|
|
|
return "$" + head + FnSuffix
|
|
|
|
default:
|
|
|
|
return "(external " + parse.Quote(head) + ")"
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:02:51 +08:00
|
|
|
//elvdoc:fn eval
|
|
|
|
//
|
|
|
|
// ```elvish
|
2021-01-09 22:59:00 +08:00
|
|
|
// eval $code &ns=$nil &on-end=$nil
|
2020-08-17 06:02:51 +08:00
|
|
|
// ```
|
|
|
|
//
|
2021-01-09 22:59:00 +08:00
|
|
|
// Evaluates `$code`, which should be a string. The evaluation happens in a
|
|
|
|
// new, restricted namespace, whose initial set of variables can be specified by
|
|
|
|
// the `&ns` option. After evaluation completes, the new namespace is passed to
|
|
|
|
// the callback specified by `&on-end` if it is not nil.
|
|
|
|
//
|
|
|
|
// The namespace specified by `&ns` is never modified; it will not be affected
|
|
|
|
// by the creation or deletion of variables by `$code`. However, the values of
|
|
|
|
// the variables may be mutated by `$code`.
|
|
|
|
//
|
|
|
|
// If the `&ns` option is `$nil` (the default), a temporary namespace built by
|
|
|
|
// amalgamating the local and upvalue scopes of the caller is used.
|
2020-08-17 06:02:51 +08:00
|
|
|
//
|
|
|
|
// If `$code` fails to parse or compile, the parse error or compilation error is
|
|
|
|
// raised as an exception.
|
|
|
|
//
|
2021-01-09 22:59:00 +08:00
|
|
|
// Basic examples that do not modify the namespace or any variable:
|
2020-08-17 06:02:51 +08:00
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> eval 'put x'
|
|
|
|
// ▶ x
|
2021-01-09 22:59:00 +08:00
|
|
|
// ~> x = foo
|
|
|
|
// ~> eval 'put $x'
|
|
|
|
// ▶ foo
|
|
|
|
// ~> ns = (ns [&x=bar])
|
|
|
|
// ~> eval &ns=$ns 'put $x'
|
|
|
|
// ▶ bar
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Examples that modify existing variables:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> y = foo
|
|
|
|
// ~> eval 'y = bar'
|
|
|
|
// ~> put $y
|
|
|
|
// ▶ bar
|
2020-08-17 06:02:51 +08:00
|
|
|
// ```
|
|
|
|
//
|
2021-01-09 22:59:00 +08:00
|
|
|
// Examples that creates new variables and uses the callback to access it:
|
2020-08-17 06:02:51 +08:00
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
2021-01-09 22:59:00 +08:00
|
|
|
// ~> eval 'z = lorem'
|
|
|
|
// ~> put $z
|
|
|
|
// compilation error: variable $z not found
|
|
|
|
// [ttz 2], line 1: put $z
|
|
|
|
// ~> saved-ns = $nil
|
2021-11-29 05:32:41 +08:00
|
|
|
// ~> eval &on-end={|ns| saved-ns = $ns } 'z = lorem'
|
2021-01-09 22:59:00 +08:00
|
|
|
// ~> put $saved-ns[z]
|
|
|
|
// ▶ lorem
|
2020-08-17 06:02:51 +08:00
|
|
|
// ```
|
|
|
|
|
2021-01-09 22:59:00 +08:00
|
|
|
type evalOpts struct {
|
|
|
|
Ns *Ns
|
|
|
|
OnEnd Callable
|
|
|
|
}
|
2020-08-17 06:02:51 +08:00
|
|
|
|
|
|
|
func (*evalOpts) SetDefaultOptions() {}
|
|
|
|
|
|
|
|
func eval(fm *Frame, opts evalOpts, code string) error {
|
|
|
|
src := parse.Source{Name: fmt.Sprintf("[eval %d]", nextEvalCount()), Code: code}
|
|
|
|
ns := opts.Ns
|
|
|
|
if ns == nil {
|
2021-01-10 08:54:02 +08:00
|
|
|
ns = CombineNs(fm.up, fm.local)
|
2021-01-09 22:59:00 +08:00
|
|
|
}
|
|
|
|
// The stacktrace already contains the line that calls "eval", so we pass
|
|
|
|
// nil as the second argument.
|
|
|
|
newNs, exc := fm.Eval(src, nil, ns)
|
|
|
|
if opts.OnEnd != nil {
|
|
|
|
newFm := fm.fork("on-end callback of eval")
|
|
|
|
errCb := opts.OnEnd.Call(newFm, []interface{}{newNs}, NoOpts)
|
|
|
|
if exc == nil {
|
|
|
|
return errCb
|
|
|
|
}
|
2020-08-17 06:02:51 +08:00
|
|
|
}
|
2021-01-09 22:59:00 +08:00
|
|
|
return exc
|
2020-08-17 06:02:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used to generate unique names for each source passed to eval.
|
|
|
|
var (
|
|
|
|
evalCount int
|
|
|
|
evalCountMutex sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func nextEvalCount() int {
|
|
|
|
evalCountMutex.Lock()
|
|
|
|
defer evalCountMutex.Unlock()
|
|
|
|
evalCount++
|
|
|
|
return evalCount
|
|
|
|
}
|
|
|
|
|
2020-08-17 07:11:25 +08:00
|
|
|
//elvdoc:fn use-mod
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// use-mod $use-spec
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Imports a module, and outputs the namespace for the module.
|
|
|
|
//
|
|
|
|
// Most code should use the [use](language.html#importing-modules-with-use)
|
|
|
|
// special command instead.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> echo 'x = value' > a.elv
|
|
|
|
// ~> put (use-mod ./a)[x]
|
|
|
|
// ▶ value
|
|
|
|
// ```
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func useMod(fm *Frame, spec string) (*Ns, error) {
|
2021-01-09 22:59:00 +08:00
|
|
|
return use(fm, spec, nil)
|
2020-08-17 07:11:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 10:50:27 +08:00
|
|
|
func readFileUTF8(fname string) (string, error) {
|
2021-08-23 07:36:26 +08:00
|
|
|
bytes, err := os.ReadFile(fname)
|
2018-03-01 10:50:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !utf8.Valid(bytes) {
|
|
|
|
return "", fmt.Errorf("%s: source is not valid UTF-8", fname)
|
|
|
|
}
|
|
|
|
return string(bytes), nil
|
2017-04-11 08:16:43 +08:00
|
|
|
}
|
|
|
|
|
2021-01-24 23:32:24 +08:00
|
|
|
//elvdoc:fn deprecate
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// deprecate $msg
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Shows the given deprecation message to stderr. If called from a function
|
|
|
|
// or module, also shows the call site of the function or import site of the
|
|
|
|
// module. Does nothing if the combination of the call site and the message has
|
|
|
|
// been shown before.
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> deprecate msg
|
|
|
|
// deprecation: msg
|
|
|
|
// ~> fn f { deprecate msg }
|
|
|
|
// ~> f
|
|
|
|
// deprecation: msg
|
|
|
|
// [tty 19], line 1: f
|
|
|
|
// ~> exec
|
|
|
|
// ~> deprecate msg
|
|
|
|
// deprecation: msg
|
|
|
|
// ~> fn f { deprecate msg }
|
|
|
|
// ~> f
|
|
|
|
// deprecation: msg
|
|
|
|
// [tty 3], line 1: f
|
|
|
|
// ~> f # a different call site; shows deprecate message
|
|
|
|
// deprecation: msg
|
|
|
|
// [tty 4], line 1: f
|
|
|
|
// ~> fn g { f }
|
|
|
|
// ~> g
|
|
|
|
// deprecation: msg
|
|
|
|
// [tty 5], line 1: fn g { f }
|
|
|
|
// ~> g # same call site, no more deprecation message
|
|
|
|
// ```
|
|
|
|
|
|
|
|
func deprecate(fm *Frame, msg string) {
|
|
|
|
var ctx *diag.Context
|
|
|
|
if fm.traceback.Next != nil {
|
|
|
|
ctx = fm.traceback.Next.Head
|
|
|
|
}
|
|
|
|
fm.Deprecate(msg, ctx, 0)
|
|
|
|
}
|
|
|
|
|
2020-09-03 13:48:39 +08:00
|
|
|
// TimeAfter is used by the sleep command to obtain a channel that is delivered
|
|
|
|
// a value after the specified time.
|
|
|
|
//
|
|
|
|
// It is a variable to allow for unit tests to efficiently test the behavior of
|
|
|
|
// the `sleep` command, both by eliminating an actual sleep and verifying the
|
|
|
|
// duration was properly parsed.
|
|
|
|
var TimeAfter = func(fm *Frame, d time.Duration) <-chan time.Time {
|
2020-08-18 13:40:30 +08:00
|
|
|
return time.After(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
//elvdoc:fn sleep
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// sleep $duration
|
|
|
|
// ```
|
|
|
|
//
|
2020-09-01 03:27:29 +08:00
|
|
|
// Pauses for at least the specified duration. The actual pause duration depends
|
|
|
|
// on the system.
|
|
|
|
//
|
|
|
|
// This only affects the current Elvish context. It does not affect any other
|
|
|
|
// contexts that might be executing in parallel as a consequence of a command
|
|
|
|
// such as [`peach`](#peach).
|
2020-08-18 13:40:30 +08:00
|
|
|
//
|
2021-11-30 04:03:09 +08:00
|
|
|
// A duration can be a simple [number](language.html#number) (with optional
|
2020-09-01 03:27:29 +08:00
|
|
|
// fractional value) without an explicit unit suffix, with an implicit unit of
|
|
|
|
// seconds.
|
2020-08-18 13:40:30 +08:00
|
|
|
//
|
|
|
|
// A duration can also be a string written as a sequence of decimal numbers,
|
|
|
|
// each with optional fraction, plus a unit suffix. For example, "300ms",
|
|
|
|
// "1.5h" or "1h45m7s". Valid time units are "ns", "us" (or "µs"), "ms", "s",
|
|
|
|
// "m", "h".
|
|
|
|
//
|
2020-09-01 03:27:29 +08:00
|
|
|
// Passing a negative duration causes an exception; this is different from the
|
|
|
|
// typical BSD or GNU `sleep` command that silently exits with a success status
|
|
|
|
// without pausing when given a negative duration.
|
2020-08-18 13:40:30 +08:00
|
|
|
//
|
2020-09-01 03:27:29 +08:00
|
|
|
// See the [Go documentation](https://golang.org/pkg/time/#ParseDuration) for
|
|
|
|
// more information about how durations are parsed.
|
2020-08-18 13:40:30 +08:00
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> sleep 0.1 # sleeps 0.1 seconds
|
|
|
|
// ~> sleep 100ms # sleeps 0.1 seconds
|
|
|
|
// ~> sleep 1.5m # sleeps 1.5 minutes
|
|
|
|
// ~> sleep 1m30s # sleeps 1.5 minutes
|
|
|
|
// ~> sleep -1
|
|
|
|
// Exception: sleep duration must be >= zero
|
|
|
|
// [tty 8], line 1: sleep -1
|
|
|
|
// ```
|
|
|
|
|
|
|
|
func sleep(fm *Frame, duration interface{}) error {
|
2021-05-24 11:17:51 +08:00
|
|
|
var f float64
|
2020-08-18 13:40:30 +08:00
|
|
|
var d time.Duration
|
|
|
|
|
2021-05-24 11:17:51 +08:00
|
|
|
if err := vals.ScanToGo(duration, &f); err == nil {
|
|
|
|
d = time.Duration(f * float64(time.Second))
|
|
|
|
} else {
|
|
|
|
// See if it is a duration string rather than a simple number.
|
|
|
|
switch duration := duration.(type) {
|
|
|
|
case string:
|
2020-08-18 13:40:30 +08:00
|
|
|
d, err = time.ParseDuration(duration)
|
|
|
|
if err != nil {
|
2021-05-22 11:51:03 +08:00
|
|
|
return ErrInvalidSleepDuration
|
2020-08-18 13:40:30 +08:00
|
|
|
}
|
2021-05-24 11:17:51 +08:00
|
|
|
default:
|
2021-06-07 04:52:27 +08:00
|
|
|
return ErrInvalidSleepDuration
|
2020-08-18 13:40:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d < 0 {
|
2021-05-22 11:51:03 +08:00
|
|
|
return ErrNegativeSleepDuration
|
2020-08-18 13:40:30 +08:00
|
|
|
}
|
|
|
|
|
2016-02-23 00:39:03 +08:00
|
|
|
select {
|
2018-02-05 15:35:49 +08:00
|
|
|
case <-fm.Interrupts():
|
|
|
|
return ErrInterrupted
|
2020-09-03 13:48:39 +08:00
|
|
|
case <-TimeAfter(fm, d):
|
2018-02-05 15:35:49 +08:00
|
|
|
return nil
|
2016-02-23 00:39:03 +08:00
|
|
|
}
|
2016-02-10 01:18:18 +08:00
|
|
|
}
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
//elvdoc:fn time
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// time &on-end=$nil $callable
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Runs the callable, and call `$on-end` with the duration it took, as a
|
|
|
|
// number in seconds. If `$on-end` is `$nil` (the default), prints the
|
|
|
|
// duration in human-readable form.
|
|
|
|
//
|
|
|
|
// If `$callable` throws an exception, the exception is propagated after the
|
|
|
|
// on-end or default printing is done.
|
|
|
|
//
|
|
|
|
// If `$on-end` throws an exception, it is propagated, unless `$callable` has
|
|
|
|
// already thrown an exception.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> time { sleep 1 }
|
|
|
|
// 1.006060647s
|
|
|
|
// ~> time { sleep 0.01 }
|
|
|
|
// 1.288977ms
|
|
|
|
// ~> t = ''
|
2021-11-28 11:24:42 +08:00
|
|
|
// ~> time &on-end={|x| t = $x } { sleep 1 }
|
2020-08-17 01:46:29 +08:00
|
|
|
// ~> put $t
|
|
|
|
// ▶ (float64 1.000925004)
|
2021-11-28 11:24:42 +08:00
|
|
|
// ~> time &on-end={|x| t = $x } { sleep 0.01 }
|
2020-08-17 01:46:29 +08:00
|
|
|
// ~> put $t
|
|
|
|
// ▶ (float64 0.011030208)
|
|
|
|
// ```
|
|
|
|
|
2020-04-27 04:01:56 +08:00
|
|
|
type timeOpt struct{ OnEnd Callable }
|
|
|
|
|
|
|
|
func (o *timeOpt) SetDefaultOptions() {}
|
|
|
|
|
|
|
|
func timeCmd(fm *Frame, opts timeOpt, f Callable) error {
|
2017-04-11 08:16:43 +08:00
|
|
|
t0 := time.Now()
|
2018-03-01 10:17:56 +08:00
|
|
|
err := f.Call(fm, NoArgs, NoOpts)
|
2017-04-11 08:16:43 +08:00
|
|
|
t1 := time.Now()
|
|
|
|
|
|
|
|
dt := t1.Sub(t0)
|
2020-04-27 04:01:56 +08:00
|
|
|
if opts.OnEnd != nil {
|
|
|
|
newFm := fm.fork("on-end callback of time")
|
|
|
|
errCb := opts.OnEnd.Call(newFm, []interface{}{dt.Seconds()}, NoOpts)
|
|
|
|
if err == nil {
|
|
|
|
err = errCb
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-12 05:20:27 +08:00
|
|
|
_, errWrite := fmt.Fprintln(fm.ByteOutput(), dt)
|
|
|
|
if err == nil {
|
|
|
|
err = errWrite
|
|
|
|
}
|
2020-04-27 04:01:56 +08:00
|
|
|
}
|
2017-04-11 08:16:43 +08:00
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-11-06 17:47:20 +08:00
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
//elvdoc:fn -ifaddrs
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// -ifaddrs
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Output all IP addresses of the current host.
|
|
|
|
//
|
|
|
|
// This should be part of a networking module instead of the builtin module.
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func _ifaddrs(fm *Frame) error {
|
2016-11-06 17:47:20 +08:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
2018-02-05 15:35:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-18 07:11:51 +08:00
|
|
|
out := fm.ValueOutput()
|
2016-11-06 17:47:20 +08:00
|
|
|
for _, addr := range addrs {
|
2021-06-18 07:11:51 +08:00
|
|
|
err := out.Put(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-06 17:47:20 +08:00
|
|
|
}
|
2018-02-05 15:35:49 +08:00
|
|
|
return nil
|
2016-11-06 17:47:20 +08:00
|
|
|
}
|