Last active
October 5, 2024 03:54
-
-
Save bokwoon95/79d879f7b75b018b4e77118ad223928c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//go:build windows | |
package main | |
import ( | |
"fmt" | |
"os" | |
"syscall" | |
"unsafe" | |
) | |
var ( | |
kernel32 = syscall.NewLazyDLL("kernel32.dll") | |
// https://learn.microsoft.com/en-us/windows/console/getconsoleprocesslist | |
getConsoleProcessList = kernel32.NewProc("GetConsoleProcessList") | |
// https://learn.microsoft.com/en-us/windows/console/setconsolemode | |
setConsoleMode = kernel32.NewProc("SetConsoleMode") | |
) | |
func init() { | |
// When an error happens, don't immediately exit the program because that would | |
// cause the command prompt window to disappear and not give the user a chance | |
// to look at the error message. Instead, present the user with a "Press any | |
// key to exit..." prompt. But only if the user double clicked on the binary in | |
// the first place. If run from the command line, do not present the user with | |
// the prompt. | |
exit = func(exitErr error) { | |
fmt.Println(exitErr) | |
// Detect if windows golang executable file is running via double click or | |
// from cmd/shell terminator. | |
// https://gist.github.com/yougg/213250cc04a52e2b853590b06f49d865 | |
// | |
// Read a character from standard input in Go (without pressing Enter). | |
// https://stackoverflow.com/a/17289208 | |
var pids [2]uint32 | |
var maxCount uint32 = 2 | |
processCount, _, _ := getConsoleProcessList.Call(uintptr(unsafe.Pointer(&pids)), uintptr(maxCount)) | |
if processCount > 1 { | |
os.Exit(1) | |
} | |
h := syscall.Handle(os.Stdin.Fd()) | |
var mode uint32 | |
err := syscall.GetConsoleMode(h, &mode) | |
if err != nil { | |
return | |
} | |
success, _, _ := setConsoleMode.Call(uintptr(h), 0) | |
if success == 0 { | |
return | |
} | |
defer setConsoleMode.Call(uintptr(h), uintptr(mode)) | |
fmt.Print("Press any key to exit...") | |
os.Stdin.Read(make([]byte, 1)) | |
os.Exit(1) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//go:build open_browser | |
package main | |
import ( | |
"os/exec" | |
"runtime" | |
) | |
func init() { | |
openBrowser = func(address string) { | |
switch runtime.GOOS { | |
case "windows": | |
exec.Command("explorer.exe", address).Run() | |
case "darwin": | |
exec.Command("open", address).Run() | |
default: | |
exec.Command("xdg-open", address).Run() | |
} | |
} | |
startMessage = ` | |
_ _ | |
_ __ ___ | |_ ___| |__ _ __ _____ __ | |
| '_ \ / _ \| __/ _ \ '_ \| '__/ _ \ \ /\ / / | |
| | | | (_) | || __/ |_) | | | __/\ V V / | |
|_| |_|\___/ \__\___|_.__/|_| \___| \_/\_/ | |
notebrew is running on %s | |
Please do not close this window (except to quit notebrew). | |
` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment