-
Notifications
You must be signed in to change notification settings - Fork 15
/
system.jl
120 lines (79 loc) · 2.19 KB
/
system.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
### Now
## TODO take 1 arg as well
@mkapprule Now :nargs => 0
@doap Now() = Dates.now()
### Timing
@sjdoc Timing """
Timing(expr)
evaluate `expr` and return a `List` of the elapsed CPU time
and the result.
"""
@mkapprule Timing :nodefault => true
@sjseealso_group(Timing,Allocated,Time,Trace)
@doap function Timing(exprs...)
local mxnew
t = @elapsed begin
reset_meval_count()
for x in exprs
mxnew = doeval(x)
end
setsymval(:ans,mxnew)
end
mxpr(:List,t,mxnew)
end
### Pause
@sjdoc Pause """
Pause(x)
pauses (i.e.sleeps) for `x` seconds.
"""
@mkapprule Pause :nargs => 1
@doap Pause(x::T) where {T<:Real} = sleep(x)
## Partial implementations of these...
### UnixTime
@mkapprule UnixTime :nargs => 0
@doap UnixTime() = round(Int,time())
### AbsoluteTime
#mma2datetime(t) =
@mkapprule AbsoluteTime :nargs => 0
## FIXME: works in v0.6. Will break for earlier versions
@doap AbsoluteTime() = Dates.value(Dates.now() - Dates.DateTime(1900))/1000
### DateList
## Note: we can use DateParser.jl at some point.
function _datelist(t)
MList(Dates.year(t),Dates.month(t),Dates.day(t),Dates.hour(t),Dates.minute(t),Dates.second(t))
end
@mkapprule DateList
@doap DateList(x::ListT) = _datelist(Dates.DateTime(margs(x)...))
@doap DateList() = _datelist(Dates.now())
#@doap DateList(t::AbstractFloat) = _datelist(Dates.DateTime(t))
### Run
@sjdoc Run """
Run(\`cmd\`)
runs the symstem command `cmd`. Note the backticks around `cmd`.
"""
@mkapprule Run :nargs => 1
# eg Run(`echo hello`)
@doap Run(x::Cmd) = run(x)
### Environment
@mkapprule Environment :nargs => 1
## Mma returns $Failed on failure
@doap Environment(x::String) = get(ENV,x,Null)
### GetEnvironment
@mkapprule GetEnvironment
## Mma returns a rule. We just return the val here.
@doap GetEnvironment(x::String) = get(ENV,x,Null)
## We return a Dict, not a list of Rules
@doap GetEnvironment() = ENV
## Mma gets and receives objects somehow
### CopyToClipboard
@mkapprule CopyToClipboard :nargs => 1
@doap CopyToClipboard(x) = clipboard(wrapout(x))
### Paste
@mkapprule Paste :nargs => 0
@doap function Paste()
try
clipboard()
catch
Null
end
end