From 7a496752f32edc147576939d89926f42466e94d5 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Sun, 24 Jan 2021 12:28:39 +0100 Subject: [PATCH] fs: add support for flag --no-console on windows to hide the console window --- cmd/cmd.go | 5 +++++ fs/config.go | 1 + fs/config/configflags/configflags.go | 1 + lib/terminal/hidden_other.go | 7 +++++++ lib/terminal/hidden_windows.go | 19 +++++++++++++++++++ 5 files changed, 33 insertions(+) create mode 100644 lib/terminal/hidden_other.go create mode 100644 lib/terminal/hidden_windows.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 7a93f159e..9ce1acd15 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -382,6 +382,11 @@ func initConfig() { // Finish parsing any command line flags configflags.SetFlags(ci) + // Hide console window + if ci.NoConsole { + terminal.HideConsole() + } + // Load filters err := filterflags.Reload(ctx) if err != nil { diff --git a/fs/config.go b/fs/config.go index 356479731..f913de579 100644 --- a/fs/config.go +++ b/fs/config.go @@ -121,6 +121,7 @@ type ConfigInfo struct { DownloadHeaders []*HTTPOption Headers []*HTTPOption RefreshTimes bool + NoConsole bool } // NewConfig creates a new config with everything set to the default diff --git a/fs/config/configflags/configflags.go b/fs/config/configflags/configflags.go index e5fc527bf..2cdbbe3d7 100644 --- a/fs/config/configflags/configflags.go +++ b/fs/config/configflags/configflags.go @@ -124,6 +124,7 @@ func AddFlags(ci *fs.ConfigInfo, flagSet *pflag.FlagSet) { flags.StringArrayVarP(flagSet, &downloadHeaders, "header-download", "", nil, "Set HTTP header for download transactions") flags.StringArrayVarP(flagSet, &headers, "header", "", nil, "Set HTTP header for all transactions") flags.BoolVarP(flagSet, &ci.RefreshTimes, "refresh-times", "", ci.RefreshTimes, "Refresh the modtime of remote files.") + flags.BoolVarP(flagSet, &ci.NoConsole, "no-console", "", ci.NoConsole, "Hide console window. Supported on Windows only.") } // ParseHeaders converts the strings passed in via the header flags into HTTPOptions diff --git a/lib/terminal/hidden_other.go b/lib/terminal/hidden_other.go new file mode 100644 index 000000000..2a15b21b1 --- /dev/null +++ b/lib/terminal/hidden_other.go @@ -0,0 +1,7 @@ +// +build !windows + +package terminal + +// HideConsole is only supported on windows +func HideConsole() { +} diff --git a/lib/terminal/hidden_windows.go b/lib/terminal/hidden_windows.go new file mode 100644 index 000000000..0745e87ed --- /dev/null +++ b/lib/terminal/hidden_windows.go @@ -0,0 +1,19 @@ +// +build windows + +package terminal + +import ( + "syscall" +) + +// HideConsole hides the console window and activates another window +func HideConsole() { + getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow") + showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow") + if getConsoleWindow.Find() == nil && showWindow.Find() == nil { + hwnd, _, _ := getConsoleWindow.Call() + if hwnd != 0 { + showWindow.Call(hwnd, 0) + } + } +}