Windows 应用程序:显示预定命令的结果(stdout?)的实时输出

Windows 应用程序:显示预定命令的结果(stdout?)的实时输出

我正在寻找按以下方式工作的应用程序:

  1. 该应用程序显示/有一个输入区域/框。
  2. 它运行预先指定的命令/应用程序(使用输入作为参数)并在我输入时并行(实时)显示命令输出。

因此,它类似于浏览器中的“边输入边查找”功能,只不过它不是搜索 html 页面,而是运行自定义命令并显示命令输出(在另一个窗口/记事本/任何东西中 - 我不是特别清楚如何操作)

该应用程序可以基于 gui/console/cygwin/autohotkey 脚本。我同意所有

有这样的事情存在吗?

我确信 autohotkey 专家可以很容易地完成这样的事情。所以如果有人在身边,请分享如何做到这一点/任何关于要寻找什么的提示。

谢谢

编辑:回答问题:

当您说希望输出是实时的时,如果仅在每个命令完全执行后才显示输出,这样可以接受吗?

我认为不是,该命令应该在每次按下键盘按钮时运行,而不必按回车键。

尝试重新执行命令的变体?每次执行新命令时,您是否会清除输出窗口,以便输出窗口仅显示最后执行的命令的输出?

是的!确实如此。

命令本身并不复杂,会在几毫秒内返回一些信息......

答案1

您可以在 AutoHotkey 中尝试此操作。它甚至无法执行诸如调整窗口大小之类的基本操作,但它可能对您有用。此外,第一个输入框没有标签,但它是要使用的工作目录。输入命令并按下回车键后,它将打开自动更新,并在此后每次击键时自动运行。

;---------------------------------------------------------------------------------------------
; CommandConsole.ahk
;---------------------------------------------------------------------------------------------
Main:
    myTitle := "Command Console"
    myFont  := "Courier New"
    myFontSize := "10"
    xMargin := 10, yMargin := 10
    xSpace  := 10, ySpace  := 10

    txtWidth  := 700, butWidth := 100

    inputHeight  := 20
    outputHeight := 600

    firstRun := True
    immediateExec := False

    Gui, Add, Button, x0 y0 w0 h0 default gbutSubmit,       ; default button used for grabbing text entry

    vertical := yMargin
    Gui, Font, % "s" myFontSize, % myFont
    Gui, Add, Edit, % "x" xMargin " y" vertical " w" txtWidth " h" inputHeight " vWorkingDir", .        ; current directory is default working directory, i.e., "."
    Gui, Font

    Gui, Add, Button, % "x" xMargin+txtWidth+xSpace " y" vertical " w" butWidth " h" 2*inputHeight+yspace " gButAuto vButCaption", % "Auto-Update`n" . (immediateExec ? "On" : "Off")

    vertical += inputHeight + ySpace
    Gui, Font, % "s" myFontSize, % myFont
    Gui, Add, Edit, % "x" xMargin " y" vertical " w" txtWidth " h" inputHeight " gEditUpdated vtxtInput",  
    Gui, Font


    Gui, Font, % "s" myFontSize, % myFont
    vertical += inputHeight + ySpace
    Gui, Add, Edit, % "x" xMargin " y" vertical " w" txtWidth+xSpace+butWidth " h" outputHeight " vtxtOutput " ; disabled"
    Gui, Font

    vertical += OutputHeight+yMargin
    Gui, Show, % "w" txtWidth+xSpace+butWidth+2*xMargin " h" vertical, % myTitle

    GuiControl, focus, txtInput     ; focus on the command input for entry
return

;---------------------------------------------------------------------------------------------
; GuiClose() - Exit app when GUI closes
;---------------------------------------------------------------------------------------------
GuiClose() {
    ExitApp
}

;---------------------------------------------------------------------------------------------
; butSubmit() - No-size button to capture Enter keystrokes when auto-update is off
;---------------------------------------------------------------------------------------------
butSubmit() {
    global
    EnterWasPressed := True
    if firstRun and !immediateExec  ; set auto-update after the first command is executed if it's not set already
        ButAuto()
    else
        EditUpdated()           ; on subsequent
}

;---------------------------------------------------------------------------------------------
; EditUpdated()
;---------------------------------------------------------------------------------------------
EditUpdated() {
    global      ; easy access to GUI vars

    Gui, Submit, NoHide
    ;tooltip Edit was updated: %txtInput%   

    if immediateExec or EnterWasPressed { 
        guiControl,, txtOutput, % RunWaitOneB(txtInput, WorkingDir)
        EnterWasPressed := False
    }

    lasttxtInput := txtInput
    Gui, Submit, NoHide
    if (txtInput<>lastTxtInput)
        setTimer, EditUpdated, -1

    return
}

;---------------------------------------------------------------------------------------------
; ButGo() - Called every time the user clicks auto-execute True/False Button
;---------------------------------------------------------------------------------------------
ButAuto() {
    global
    immediateExec := !immediateExec
    guiControl,, butCaption, % "Auto-Update`n" . (immediateExec ? "On" : "Off")
    if immediateExec
        EditUpdated()
}

;---------------------------------------------------------------------------------------------
; RunWaitOne() - From AutoHotkey Help for RunWait (has annoying command prompt flash up)
;---------------------------------------------------------------------------------------------
RunWaitOne(command) {
    shell := ComObjCreate("WScript.Shell")      ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    exec := shell.Exec(ComSpec " /C " command)  ; Execute a single command via cmd.exe
    return exec.StdOut.ReadAll()                ; Read and return the command's output
}

;---------------------------------------------------------------------------------------------
; RunWaitOneB() - Get stdout by writing to a temp file vs. reading the buffer
;---------------------------------------------------------------------------------------------
RunWaitOneB(command, WorkingDir:=".") {
    tmpFile := A_Temp . "\temp.txt"
    FileDelete, %tmpFile%
    RunWait, %comspec% /c %command% > %tmpFile%, %WorkingDir%, Hide
    FileRead, output, %tmpFile%
    return output
}

相关内容