Skip to content

Commit 7ef3810

Browse files
committed
patch 8.0.0015
Problem: Can't tell which part of a channel has "buffered" status. Solution: Add an optional argument to ch_status(). Let ch_info() also return "buffered" for out_status and err_status.
1 parent 1eceada commit 7ef3810

6 files changed

Lines changed: 82 additions & 19 deletions

File tree

runtime/doc/eval.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,8 @@ ch_sendraw({handle}, {string} [, {options}])
20312031
any send {string} over raw {handle}
20322032
ch_setoptions({handle}, {options})
20332033
none set options for {handle}
2034-
ch_status({handle}) String status of channel {handle}
2034+
ch_status({handle} [, {options}])
2035+
String status of channel {handle}
20352036
changenr() Number current change number
20362037
char2nr({expr}[, {utf8}]) Number ASCII/UTF8 value of first char in {expr}
20372038
cindent({lnum}) Number C indent for line {lnum}
@@ -3042,7 +3043,8 @@ ch_info({handle}) *ch_info()*
30423043
Returns a Dictionary with information about {handle}. The
30433044
items are:
30443045
"id" number of the channel
3045-
"status" "open" (any part is open) or "closed"
3046+
"status" "open", "buffered" or "closed", like
3047+
ch_status()
30463048
When opened with ch_open():
30473049
"hostname" the hostname of the address
30483050
"port" the port of the address
@@ -3051,11 +3053,11 @@ ch_info({handle}) *ch_info()*
30513053
"sock_io" "socket"
30523054
"sock_timeout" timeout in msec
30533055
When opened with job_start():
3054-
"out_status" "open" or "closed"
3056+
"out_status" "open", "buffered" or "closed"
30553057
"out_mode" "NL", "RAW", "JSON" or "JS"
30563058
"out_io" "null", "pipe", "file" or "buffer"
30573059
"out_timeout" timeout in msec
3058-
"err_status" "open" or "closed"
3060+
"err_status" "open", "buffered" or "closed"
30593061
"err_mode" "NL", "RAW", "JSON" or "JS"
30603062
"err_io" "out", "null", "pipe", "file" or "buffer"
30613063
"err_timeout" timeout in msec
@@ -3140,7 +3142,7 @@ ch_setoptions({handle}, {options}) *ch_setoptions()*
31403142
These options cannot be changed:
31413143
"waittime" only applies to |ch_open()|
31423144

3143-
ch_status({handle}) *ch_status()*
3145+
ch_status({handle} [, {options}]) *ch_status()*
31443146
Return the status of {handle}:
31453147
"fail" failed to open the channel
31463148
"open" channel can be used
@@ -3150,6 +3152,11 @@ ch_status({handle}) *ch_status()*
31503152
"buffered" is used when the channel was closed but there is
31513153
still data that can be obtained with |ch_read()|.
31523154

3155+
If {options} is given it can contain a "part" entry to specify
3156+
the part of the channel to return the status for: "out" or
3157+
"err". For example, to get the error status: >
3158+
ch_status(job, {"part": "err"})
3159+
<
31533160
*copy()*
31543161
copy({expr}) Make a copy of {expr}. For Numbers and Strings this isn't
31553162
different from using {expr} directly.

src/channel.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,23 +2590,41 @@ channel_has_readahead(channel_T *channel, int part)
25902590

25912591
/*
25922592
* Return a string indicating the status of the channel.
2593+
* If "req_part" is not negative check that part.
25932594
*/
25942595
char *
2595-
channel_status(channel_T *channel)
2596+
channel_status(channel_T *channel, int req_part)
25962597
{
25972598
int part;
25982599
int has_readahead = FALSE;
25992600

26002601
if (channel == NULL)
26012602
return "fail";
2602-
if (channel_is_open(channel))
2603-
return "open";
2604-
for (part = PART_SOCK; part <= PART_ERR; ++part)
2605-
if (channel_has_readahead(channel, part))
2606-
{
2603+
if (req_part == PART_OUT)
2604+
{
2605+
if (channel->CH_OUT_FD != INVALID_FD)
2606+
return "open";
2607+
if (channel_has_readahead(channel, PART_OUT))
26072608
has_readahead = TRUE;
2608-
break;
2609-
}
2609+
}
2610+
else if (req_part == PART_ERR)
2611+
{
2612+
if (channel->CH_ERR_FD != INVALID_FD)
2613+
return "open";
2614+
if (channel_has_readahead(channel, PART_ERR))
2615+
has_readahead = TRUE;
2616+
}
2617+
else
2618+
{
2619+
if (channel_is_open(channel))
2620+
return "open";
2621+
for (part = PART_SOCK; part <= PART_ERR; ++part)
2622+
if (channel_has_readahead(channel, part))
2623+
{
2624+
has_readahead = TRUE;
2625+
break;
2626+
}
2627+
}
26102628

26112629
if (has_readahead)
26122630
return "buffered";
@@ -2619,15 +2637,21 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
26192637
chanpart_T *chanpart = &channel->ch_part[part];
26202638
char namebuf[20]; /* longest is "sock_timeout" */
26212639
size_t tail;
2640+
char *status;
26222641
char *s = "";
26232642

26242643
vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
26252644
STRCAT(namebuf, "_");
26262645
tail = STRLEN(namebuf);
26272646

26282647
STRCPY(namebuf + tail, "status");
2629-
dict_add_nr_str(dict, namebuf, 0,
2630-
(char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2648+
if (chanpart->ch_fd != INVALID_FD)
2649+
status = "open";
2650+
else if (channel_has_readahead(channel, part))
2651+
status = "buffered";
2652+
else
2653+
status = "closed";
2654+
dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
26312655

26322656
STRCPY(namebuf + tail, "mode");
26332657
switch (chanpart->ch_mode)
@@ -2660,7 +2684,7 @@ channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
26602684
channel_info(channel_T *channel, dict_T *dict)
26612685
{
26622686
dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2663-
dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2687+
dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
26642688

26652689
if (channel->ch_hostname != NULL)
26662690
{
@@ -4244,6 +4268,8 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
42444268
val = get_tv_string(item);
42454269
if (STRCMP(val, "err") == 0)
42464270
opt->jo_part = PART_ERR;
4271+
else if (STRCMP(val, "out") == 0)
4272+
opt->jo_part = PART_OUT;
42474273
else
42484274
{
42494275
EMSG2(_(e_invarg2), val);

src/evalfunc.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ static struct fst
514514
{"ch_sendexpr", 2, 3, f_ch_sendexpr},
515515
{"ch_sendraw", 2, 3, f_ch_sendraw},
516516
{"ch_setoptions", 2, 2, f_ch_setoptions},
517-
{"ch_status", 1, 1, f_ch_status},
517+
{"ch_status", 1, 2, f_ch_status},
518518
#endif
519519
{"changenr", 0, 0, f_changenr},
520520
{"char2nr", 1, 2, f_char2nr},
@@ -1985,13 +1985,24 @@ f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
19851985
f_ch_status(typval_T *argvars, typval_T *rettv)
19861986
{
19871987
channel_T *channel;
1988+
jobopt_T opt;
1989+
int part = -1;
19881990

19891991
/* return an empty string by default */
19901992
rettv->v_type = VAR_STRING;
19911993
rettv->vval.v_string = NULL;
19921994

19931995
channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1994-
rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
1996+
1997+
if (argvars[1].v_type != VAR_UNKNOWN)
1998+
{
1999+
clear_job_options(&opt);
2000+
if (get_job_options(&argvars[1], &opt, JO_PART) == OK
2001+
&& (opt.jo_set & JO_PART))
2002+
part = opt.jo_part;
2003+
}
2004+
2005+
rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
19952006
}
19962007
#endif
19972008

src/proto/channel.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void channel_consume(channel_T *channel, int part, int len);
2424
int channel_collapse(channel_T *channel, int part, int want_nl);
2525
int channel_can_write_to(channel_T *channel);
2626
int channel_is_open(channel_T *channel);
27-
char *channel_status(channel_T *channel);
27+
char *channel_status(channel_T *channel, int req_part);
2828
void channel_info(channel_T *channel, dict_T *dict);
2929
void channel_close(channel_T *channel, int invoke_close_cb);
3030
void channel_close_in(channel_T *channel);

src/testdir/test_channel.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,23 @@ func Test_raw_pipe()
434434
let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
435435
call assert_equal(v:t_job, type(job))
436436
call assert_equal("run", job_status(job))
437+
438+
call assert_equal("open", ch_status(job))
439+
call assert_equal("open", ch_status(job), {"part": "out"})
440+
call assert_equal("open", ch_status(job), {"part": "err"})
441+
call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
442+
call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
443+
444+
let dict = ch_info(job)
445+
call assert_true(dict.id != 0)
446+
call assert_equal('open', dict.status)
447+
call assert_equal('open', dict.out_status)
448+
call assert_equal('RAW', dict.out_mode)
449+
call assert_equal('pipe', dict.out_io)
450+
call assert_equal('open', dict.err_status)
451+
call assert_equal('RAW', dict.err_mode)
452+
call assert_equal('pipe', dict.err_io)
453+
437454
try
438455
" For a change use the job where a channel is expected.
439456
call ch_sendraw(job, "echo something\n")

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
15,
767769
/**/
768770
14,
769771
/**/

0 commit comments

Comments
 (0)