Saturday, December 9, 2023

Re: Out_cb callback handler in vim9

Hi all,

According to Bram example in the job's help now, 
Is it possible to pass additional parameters to job's handlers callback in vim9script ?
Thank you
Nicolas

This minimalist example WORKING as Bram said:
def Compress_OnExit(job_id: job, exit_status: number): void
    echom 'Job OnExit ' .. job_id->string() .. ' exited with status ' .. exit_status
enddef
var job = job_start(cmd, { 'exit_cb': Compress_OnExit}) 

This minimal example with additional params: 
def Compress_OnExit(job_id: job, exit_status: number, foo: string): void
    echom 'Job OnExit ' .. job_id->string() .. ' exited with status ' .. exit_status
    # Compress_CopyToDrive('PATRIOT')
enddef
var job = job_start(cmd, { 'exit_cb': function('Compress_OnExit', ['foo']) } )



Le dimanche 27 février 2022 à 14:25:43 UTC+1, Bram Moolenaar a écrit :

> In §9 of channel help is mentionned the legacy vimscript out_cb handler.
>
>
> " let job = job_start(command, {"out_cb": "MyHandler"})
> The function will be called with the channel and a message. You would define
> it like this: >
> func MyHandler(channel, msg)
> "
>
>
> I tried this def OutCb function but got the following message error :
>
> " def OutCb(l: list<any>)
> # echomsg 'OutCb' .. string(l)
> # eval [][0]
> enddef
>
> def ExitCb(l: list<any>)
> # echomsg 'ExitCb' .. string(l)
> # sleep 1m
> # source += l
> # echomsg 'Exiting ' .. string(l) .. ' ' .. string(@z)
> enddef
>
> jobid = job_start(zip_cmd, { out_cb: OutCb, exit_cb: ExitCb, mode:
> 'raw', timeout: 1200000 } ) "
>
> Error message:
> " This is a valid directory .
> Jobid: process 14328 run
> Press ENTER or type command to continue
> E118: Too many arguments for function: <lambda>4 "
>
>
> How to port and deal with this in vim9 script that seems to have only one
> argue ?

In Vim9 script the type of the arguments is checked. That helps you
writing correct functions, and once written it is easier to read back.
It does require a bit of extra text.

Here is an exmple that will help you:

vim9script

# Create a channel log so we can see what happens.
ch_logfile('logfile', 'w')

var shell_job: job

# Function handling a line of text that has been typed.
def TextEntered(text: string)
# Send the text to a shell with Enter appended.
ch_sendraw(shell_job, text .. "\n")
enddef

# Function handling output from the shell: Add it above the prompt.
def GotOutput(channel: channel, msg: string)
append(line("$") - 1, "- " .. msg)
enddef

# Function handling the shell exits: close the window.
def JobExit(job: job, status: number)
quit!
enddef

# Start a shell in the background.
shell_job = job_start(["/bin/sh"], {
out_cb: GotOutput,
err_cb: GotOutput,
exit_cb: JobExit,
})

new
set buftype=prompt
var buf = bufnr('')
prompt_setcallback(buf, TextEntered)
prompt_setprompt(buf, "shell command: ")

# start accepting shell commands
startinsert


--
hundred-and-one symptoms of being an internet addict:
115. You are late picking up your kid from school and try to explain
to the teacher you were stuck in Web traffic.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/b08ff15b-4603-4448-877d-2fb31b658103n%40googlegroups.com.

No comments:

Post a Comment