Thanks for such detailed answer!
My pleasure.
I did some more thinking about this and unfortunately, the tool option above will not work if the Go program asks for user input:
For example, consider this Go program that does ask for input:
Code: Select all
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
for i := 0; i < len(os.Args); i++ {
fmt.Printf("Argument[%d] := %s\n", i, os.Args[i])
}
fmt.Print("Enter some text: ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
fmt.Println(text)
}
If you run this via the tool it will not stop to ask for the user input, whereas if you run it via the macro it will.
The short answer for the difference in the two is the macro uses the
tee.exe to duplicate the user input handle and this allows it to work.
So a better option would be to change the
go_exec.py macro itself.
You can make that code change as follows:
1) With a Go file active go to the Macros panel and find the Execute macro command
2) Right click on the Execute command and select
View Source Code option.
3) Replace that
go_exec.py code with the code found below.
4) Right click on the Execute command and select the
Add/Edit Templates option.
5) Edit the Execute macro and change the arguments from
run to
run with_args
Now when you run the Execute macro it will prompt for user input
Cheers Jussi
Code: Select all
import os
import zeus
def key_macro():
file_name = ""
action = "run"
arguments = "run"
if zeus.argc() >= 1:
# get the action passed in
action = zeus.argv(0)
if zeus.argc() == 2:
# ask for arguments
arguments = zeus.user_input("Agruments:", "").strip()
if str(arguments) > 0:
arguments = " " + arguments + " "
if zeus.is_document() == 1:
# see if this is a new file
named = zeus.is_named()
# save the current file
result = zeus.FileSave()
if (result == 0):
if (named == 1):
zeus.message("Unexpected error saving the file current document.")
zeus.beep()
return
# use the currently active document
file_name = zeus.macro_tag("$fn")
# check that the file constains no whitespacw
if (file_name.find(' ') <> -1):
file_name = "\"" + file_name + "\""
# if we have no document then ask the user
if (len(file_name) == 0):
# this is a run action
action = "run"
# the initial value
nill = ""
# ask the user to name the script that is to be run
file_name = zeus.user_input("Go Script to Executed:", nill)
if (len(file_name) == 0):
zeus.message("The operation cancelled by user.")
zeus.beep()
return
# the output file name
file_output = file_name + ".ztow"
# check that the file constains no whitespace
if (file_output.find(' ') <> -1):
# keep the quote marks
file_output_ex = file_output
else:
# remove the file quotes to allow the file to be deleted
file_output_ex = file_output.translate(None, '"')
# remove any previous output file
if os.access(file_output_ex, os.F_OK):
os.remove(file_output_ex)
# the Go 'run/build' the script using tee to capture the output to a file
cmd = "Go.exe " + action + " " + file_name + arguments + " 2>&1 | tee " + file_output
#zeus.message_box(0, cmd, "Test")
# run the command as a MS-DOS command in a visible DOS window and wait for it to finish
flags = 16+32
if action == "run":
# for the 'run' action use a visible window
flags = flags + 64
# build up the caption
caption = "Go " + str.capitalize(action) + ": " + file_name
# run the command which should be displayed in a DOS window
result = zeus.system(cmd, "", flags, caption)
# make sure the cmd.exe command completed successfully
if result == 0:
if action == "build" or action == "fix" or action == "vet":
statinfo = os.stat(file_output)
# check for any errors
if statinfo.st_size == 0:
zeus.message(str.capitalize(action) + " completed successfully.")
else:
# open the output file in a Zeus project window
zeus.file_open_compiler(file_output, "Go.exe " + action + " " + file_name, file_name)
else:
# open the output file in a Zeus tool window
zeus.file_open_tool(file_output, "Go.exe " + action + " " + file_name)
else:
# make sure the user will see the error
if zeus.is_debug_enabled() == 0:
zeus.debug_enable()
# log the command to the debug window
zeus.debug_output(cmd + "\n\n" + "Error executing the 'Go Format' command!\n")
# set capture flags
flags = 1+32+2+4
# run the format again but this time capture the error output
zeus.system(cmd, "", flags)
# remove the output file
if os.access(file_output_ex, os.F_OK):
os.remove(file_output_ex)
key_macro() # run the macro