elvish/eval/builtin_fn_misc.go

103 lines
1.7 KiB
Go
Raw Normal View History

package eval
// Builtin functions.
2013-10-02 22:10:07 +08:00
import (
"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"
"path/filepath"
2016-02-10 01:18:18 +08:00
"time"
2016-02-14 04:05:35 +08:00
2018-02-15 17:14:05 +08:00
"github.com/elves/elvish/eval/vals"
2013-10-02 22:10:07 +08:00
)
2017-12-17 13:20:03 +08:00
// Builtins that have not been put into their own groups go here.
var ErrArgs = errors.New("args error")
2015-01-23 06:19:27 +08:00
func init() {
2018-02-07 03:39:40 +08:00
addBuiltinFns(map[string]interface{}{
2018-02-05 15:35:49 +08:00
"nop": nop,
"kind-of": kindOf,
"constantly": constantly,
2015-03-01 21:43:35 +08:00
2018-02-05 15:35:49 +08:00
"-source": source,
2017-04-11 08:16:43 +08:00
// Time
2018-02-05 15:35:49 +08:00
"esleep": sleep,
"-time": _time,
2017-04-11 08:16:43 +08:00
2018-02-05 15:35:49 +08:00
"-ifaddrs": _ifaddrs,
2017-12-17 13:20:03 +08:00
})
2018-02-05 15:35:49 +08:00
2016-03-17 22:30:15 +08:00
// For rand and randint.
rand.Seed(time.Now().UTC().UnixNano())
}
func nop(opts RawOptions, args ...interface{}) {
2018-02-05 15:35:49 +08:00
// Do nothing
2015-03-01 21:43:35 +08:00
}
2018-02-05 15:35:49 +08:00
func kindOf(fm *Frame, args ...interface{}) {
out := fm.ports[1].Chan
2015-01-20 05:04:37 +08:00
for _, a := range args {
2018-02-15 17:14:05 +08:00
out <- vals.Kind(a)
2015-01-20 05:04:37 +08:00
}
}
2018-02-05 15:35:49 +08:00
func constantly(args ...interface{}) Callable {
2017-04-11 08:16:43 +08:00
// XXX Repr of this fn is not right
2018-02-06 16:02:27 +08:00
return NewBuiltinFn(
2017-04-11 08:16:43 +08:00
"created by constantly",
2018-02-05 15:35:49 +08:00
func(fm *Frame) {
out := fm.ports[1].Chan
2017-04-11 08:16:43 +08:00
for _, v := range args {
out <- v
}
},
2018-02-05 15:35:49 +08:00
)
}
2018-02-05 15:35:49 +08:00
func source(fm *Frame, fname string) error {
abs, err := filepath.Abs(fname)
2018-02-05 15:35:49 +08:00
if err != nil {
return err
}
return fm.Source(fname, abs)
2017-04-11 08:16:43 +08:00
}
2018-02-05 15:35:49 +08:00
func sleep(fm *Frame, t float64) error {
d := time.Duration(float64(time.Second) * t)
select {
2018-02-05 15:35:49 +08:00
case <-fm.Interrupts():
return ErrInterrupted
case <-time.After(d):
2018-02-05 15:35:49 +08:00
return nil
}
2016-02-10 01:18:18 +08:00
}
2018-02-05 15:35:49 +08:00
func _time(ec *Frame, f Callable) error {
2017-04-11 08:16:43 +08:00
t0 := time.Now()
err := f.Call(ec, NoArgs, NoOpts)
2017-04-11 08:16:43 +08:00
t1 := time.Now()
dt := t1.Sub(t0)
fmt.Fprintln(ec.ports[1].File, dt)
2018-02-05 15:35:49 +08:00
return err
}
2016-11-06 17:47:20 +08:00
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
}
out := fm.ports[1].Chan
2016-11-06 17:47:20 +08:00
for _, addr := range addrs {
out <- addr.String()
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
}