-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Process.txt
250 lines (180 loc) · 8.24 KB
/
Process.txt
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
*Vital.Async.Observable.Process.txt* System.Job wrapper with Observable
Author: Alisue <lambdalisue@hashnote.net>
=============================================================================
CONTENTS *Vital.Async.Observable.Process-contents*
Introduction |Vital.Async.Observable.Process-introduction|
Usage |Vital.Async.Observable.Process-usage|
Functions |Vital.Async.Observable.Process-functions|
Emissions |Vital.Async.Observable.Process-emissions|
Operators |Vital.Async.Observable.Process-operators|
=============================================================================
INTRODUCTION *Vital.Async.Observable.Process-introduction*
*Vital.Async.Observable.Process* is a module to provide a way to execute an
external program with a |Vital.Async.Observable| interface.
It internally uses |Vital.System.Job| to execute a program so it works both
on Vim and Neovim.
While it follows the Observable interface, the power of |Vital.Rx| module can
be applied to manipulate the process.
Note that if intermediate data is not necessary, see
|Vital.Async.Promise.Process| instead.
=============================================================================
USAGE *Vital.Async.Observable.Process-usage*
Create a Process instance by |Vital.Async.Observable.Process.new()| function
by appling command arguments like:
>
let Process = vital#{plugin}#import('Async.Observable.Process')
let p = Process.new(['echo', 'Hello'])
<
Then subscribe the instance to start the program
>
call p.subscribe({ v -> execute('echo v', '') })
" { 'pid': xxxxx }
" { 'stdout': ['Hello'] }
<
While the Process instance is an Observable, users can use operators to
substitute the stream.
>
let Operators = vital#{plugin}#import('Rx.Operators')
let p = p.pipe(
\ Operators.filter({ -> has_key(v:val, 'stdout') }),
\ Operators.map({ -> v:val.stdout })
\)
call p.subscribe({ v -> execute('echo v', '') })
" Hello
<
Note that the emissions of stdout or stderr are as-is from System.Job, so
the data is received as a newline (\n) separated list like:
1. ["Hello", "Good", "Bye", ""]
2. ["Hello", ""], ["Good", ""], and ["Bye", ""]
3. ["Hello", "Go"], ["od", "Bye"], and ["", ""]
To normalize data like above, Process module provides original operators:
>
" Example stream:
" next -> {'pid': 100}
" next -> {'stdout': ['Hello', 'World', 'Go']}
" next -> {'stderr': ['FOOBAR']}
" next -> {'stdout': ['od', 'Bye']}
" next -> {'stderr': ['FOOBAR']}
" next -> {'stdout': ['', '']}
" complete
" Focus to the stdout emissions
call p.pipe(
\ Process.operators.focus('stdout'),
\).subscribe({ v -> execute('echo v', '') })
" {'stdout': ['Hello', 'World', 'Go']}
" {'stdout': ['od', 'Bye']}
" {'stdout': ['', '']}
" Focus and squash stdout emissions to emit complete line blocks
call p.pipe(
\ Process.operators.focus('stdout'),
\ Process.operators.squash('stdout'),
\).subscribe({ v -> execute('echo v', '') })
" {'stdout': ['Hello', 'World']}
" {'stdout': ['Good']}
" {'stdout': ['Bye']}
" Focus, squash, and stretch stdout emissions to emit complete lines
call p.pipe(
\ Process.operators.focus('stdout'),
\ Process.operators.squash('stdout'),
\ Process.operators.stretch('stdout'),
\).subscribe({ v -> execute('echo v', '') })
" {'stdout': 'Hello'}
" {'stdout': 'World'}
" {'stdout': 'Good'}
" {'stdout': 'Bye'}
" Emits piles of complete lines of stdout
call p.pipe(Process.operators.pile('stdout'))
" ['Hello', 'World']
" ['Good']
" ['Bye']
" Emits complete lines of stdout
call p.pipe(Process.operators.line('stdout'))
" 'Hello'
" 'World'
" 'Good'
" 'Bye'
<
=============================================================================
FUNCTIONS *Vital.Async.Observable.Process-functions*
.new({args}[, {options}]) *Vital.Async.Observable.Process.new()*
Create a new Process instance (Observable) which will execute a
command {args} when subscribed.
The following arguments are available in {options}.
"cwd" Force to execute a process in a given directory when
subscribed. Otherwise the process is executed under
a current directory on subscribing.
"raw" TRUE to use raw stream instead of \r removed stream.
"stdin" An Observable like (Observable, Promise, List) which
is used to send messages into stdin of the process.
Note that a command {args} will be executed once the process is
subscribed.
It throws an exception when called from non supported Vim/Neovim.
.start({args}[, {options}]) *Vital.Async.Observable.Process.start()*
DEPRECATED: Use |Vital.Async.Promise.Process.start()|
Execute a command {args} and return a Promise instance which will
be resolved when the process is terminated.
The promise instance will be resolved/rejected with an object which
has the following attributes.
"pid" PID of the process
"exitval" Exit value of the process
"stdout" Stdout of the process in newline separated list.
"stderr" Stderr of the process in newline separated list.
The promise instance will be resolved when exitval is 0, otherwise
it will be rejected.
See |Vital.Async.Observable.Process.new()| for available {options}.
=============================================================================
EMISSIONS *Vital.Async.Observable.Process-emissions*
Once a Process instance is subscribed, the observer/subscriber will receive
the following emissions
next:pid *Vital.Async.Observable.Process-emissions.next-pid*
Received as a next event with {'pid': ...} which indicate the PID of
the process.
It will be emitted immediately after subscribing.
next:stdout *Vital.Async.Observable.Process-emissions.next-stdout*
next:stderr *Vital.Async.Observable.Process-emissions.next-stderr*
Received as a next event with {'stdout': [...]} or {'stderr': [...]}
which indicate the stdout/stderr data received.
Note that the data is a newline (\n) separated list so users need
to normalize the data to make complete data.
See also:
|Vital.Async.Observable.Process.operators.squash()|
|Vital.Async.Observable.Process.operators.stretch()|
error *Vital.Async.Observable.Process-emissions.error*
Receveid as an error event with exitval which indicate that the
process with some exception.
complete *Vital.Async.Observable.Process-emissions.complete*
Receveid as a complete event which indicate that the process exit
successfully.
=============================================================================
OPERATORS *Vital.Async.Observable.Process-operators*
Process module provides some original operators under .operators attribute
to handle stdout/stderr.
.focus({key}) *Vital.Async.Observable.Process.operators.focus()*
Focus a given {key} of emissions in next events and emits emission
object which has only a given {key}.
In other words, it eliminate emissions which does not have the given
{key} and emit new emissions without non specified attributes.
It is similar to "pluck" operator in Rx and the differences are
1. focus does not support a nested property
2. focus does not flatten a property
.squash({key}) *Vital.Async.Observable.Process.operators.squash()*
Squash data of a given {key} of emissions in next events and emits
emission object with line blocks (list).
Note that it pass-through emissions which does not have a given {key}
attribute. Use focus() operator to filter emissions.
.stretch({key}) *Vital.Async.Observable.Process.operators.stretch()*
Stretch data of a given {key} of emissions in next events and emits
emission object with a single line.
Note that it pass-through emissions which does not have a given {key}
attribute. Use focus() operator to filter emissions.
Note that stretching the original stdout/stderr data does not make
sense. Use squash() operator to make correct line blocks first.
.pile({key}) *Vital.Async.Observable.Process.operators.pile()*
Emits piles (line blocks) of each complete lines of a given {key} of
emissions in next events.
It is a combination operator equivalent to squash/pluck.
.line({key}) *Vital.Async.Observable.Process.operators.line()*
Emits complete lines of a given {key} of emissions in next events.
It is a combination operator equivalent to squash/stretch/pluck.
=============================================================================
vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl