Can I run jshell inside Unix expect?

久未见 提交于 2019-12-01 11:15:59

Managed to make it work (tested on Debian 9.3 with jshell 9.0 and Expect 5.45):

[STEP 103] # cat jshell.exp
proc expect_prompt {} {
    upvar spawn_id spawn_id

    expect -ex "jshell> "

    # the CPR (cursor position report) code
    expect -ex "\x1b\[6n"

    # read the CPR result and send it the application
    expect_tty -re {\x1b\[[0-9]+;[0-9]+R}
    send $expect_out(0,string)
}

stty raw; # give tty's full control to jshell since it's crazy

spawn jshell
expect_prompt

send "3\r"
expect_prompt

send "/exit\n"
expect eof
[STEP 104] # expect jshell.exp
spawn jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

jshell> 3
$1 ==> 3

jshell> /exit
|  Goodbye
[STEP 105] #

The magic is about CPR (cursor position report) (search CPR on the page).

  1. The ^[[6n (^[ == ESC == 0x1b == \u001b) is the CPR request (sent by jshell).
  2. Strings like ^[[32;1R (row 32, column 1) is the current cursor position (generated by terminal driver and read back by jshell).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!