TCL 获取带有某种 -nohang 选项的命令?

标签 tcl stdin interactive gets

下面是一段代码,它通过命令提示符 MyShell > 实现了一个交互式 TCL session 。

puts -nonewline stdout "MyShell > "
flush stdout
catch { eval [gets stdin] } got
if { $got ne "" } {
    puts stderr $got
}

此代码在终端提示 MyShell > 并等待输入按钮被点击;虽然它没有被击中,但代码什么都不做。这就是 gets 命令的作用。

我需要的是 gets 命令的替代方法,比如 coolgetcoolget 命令不应该等待回车按钮,而是注册一些当它被点击时调用的插槽,然后继续执行。所需的代码应如下所示:

proc evaluate { string } \
{
    catch { eval $string } got
    if { $got ne "" } {
        puts stderr $got
    }
}

puts -nonewline stdout "MyShell > "
flush stdout
coolgets stdin evaluate; # this command should not wait for the enter button
# here goes some code which is to be executed before the enter button is hit

这是我需要的:

proc prompt { } \
{
   puts -nonewline stdout "MyShell > "
   flush stdout
}


proc process { } \
{
   catch { uplevel #0 [gets stdin] } got
   if { $got ne "" } {
       puts stderr $got
       flush stderr
   }
   prompt
}

fileevent stdin readable process

prompt
while { true } { update; after 100 }

最佳答案

我认为您需要查看 fileevent、fconfigure 和 vwait 命令。使用这些,您可以执行以下操作:

proc GetData {chan} {
    if {[gets $chan line] >= 0} {
       puts -nonewline "Read data: "
       puts $line
    }
}

fconfigure stdin -blocking 0 -buffering line -translation crlf
fileevent stdin readable [list GetData stdin]

vwait x

此代码将 GetData 注册为 stdin 的可读文件事件处理程序,因此只要有数据可供读取,它就会被调用。

关于TCL 获取带有某种 -nohang 选项的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719899/

相关文章:

C stdin 限制使我无法阅读超过 1200 个字符的行

mysql - 在 MySQL 中执行交互式查询(主要来自 GUI)

python - Python中的TCL : can't find package

tcl - 将 xml 文件解析为变量

bash - 从 tcl 脚本调用 bash 脚本并返回和退出状态

python - 使用 unix 套接字作为子进程 stdin、stdout、stderr

c - 为 2D 数组分配内存时出现段错误(核心转储)

ios - 为什么我无法使用 [transitionContext viewControllerForKey :UITransitionContextFromViewKey] 获取 FromVC 和 ToVC

python - 从标准输入读取密码

python - Tkinter:root.after(), root.after_cancel()