From e66a8c9d3cb905d1a5a1c9c1f1ab7fcad76f881b Mon Sep 17 00:00:00 2001 From: ziggie Date: Mon, 14 Nov 2022 01:04:47 +0100 Subject: [PATCH] add go build flags --- helpmessage.go => helpmessage/message.go | 13 +--- helpmessage/termsize.go | 16 +++++ helpmessage/termsize_nosysioctl.go | 8 +++ helpmessage/termsize_windows.go | 86 ++++++++++++++++++++++++ main.go | 7 +- 5 files changed, 116 insertions(+), 14 deletions(-) rename helpmessage.go => helpmessage/message.go (95%) create mode 100644 helpmessage/termsize.go create mode 100644 helpmessage/termsize_nosysioctl.go create mode 100644 helpmessage/termsize_windows.go diff --git a/helpmessage.go b/helpmessage/message.go similarity index 95% rename from helpmessage.go rename to helpmessage/message.go index 3132649..fb371c5 100644 --- a/helpmessage.go +++ b/helpmessage/message.go @@ -1,4 +1,4 @@ -package main +package helpmessage import ( "bufio" @@ -11,7 +11,6 @@ import ( "unicode/utf8" "github.com/jessevdk/go-flags" - "golang.org/x/sys/unix" ) const ( @@ -40,7 +39,7 @@ type Options struct { options []*Option } -func scanStruct(realval reflect.Value, opt *Options) error { +func ScanStruct(realval reflect.Value, opt *Options) error { stype := realval.Type() @@ -144,14 +143,6 @@ func getAlignmentInfo(options *Options, p *flags.Parser) alignmentInfo { return ret } -func getTerminalColumns() int { - ws, err := unix.IoctlGetWinsize(0, unix.TIOCGWINSZ) - if err != nil { - return 80 - } - return int(ws.Col) -} - type alignmentInfo struct { maxLongLen int hasShort bool diff --git a/helpmessage/termsize.go b/helpmessage/termsize.go new file mode 100644 index 0000000..220ece9 --- /dev/null +++ b/helpmessage/termsize.go @@ -0,0 +1,16 @@ +//go:build !windows && !plan9 && !appengine && !wasm +// +build !windows,!plan9,!appengine,!wasm + +package helpmessage + +import ( + "golang.org/x/sys/unix" +) + +func getTerminalColumns() int { + ws, err := unix.IoctlGetWinsize(0, unix.TIOCGWINSZ) + if err != nil { + return 80 + } + return int(ws.Col) +} diff --git a/helpmessage/termsize_nosysioctl.go b/helpmessage/termsize_nosysioctl.go new file mode 100644 index 0000000..fb1bdb5 --- /dev/null +++ b/helpmessage/termsize_nosysioctl.go @@ -0,0 +1,8 @@ +//go:build plan9 || appengine || wasm +// +build plan9 appengine wasm + +package helpmessage + +func getTerminalColumns() int { + return 80 +} diff --git a/helpmessage/termsize_windows.go b/helpmessage/termsize_windows.go new file mode 100644 index 0000000..9d05c1f --- /dev/null +++ b/helpmessage/termsize_windows.go @@ -0,0 +1,86 @@ +//go:build windows +// +build windows + +package helpmessage + +import ( + "syscall" + "unsafe" +) + +type ( + SHORT int16 + WORD uint16 + + SMALL_RECT struct { + Left SHORT + Top SHORT + Right SHORT + Bottom SHORT + } + + COORD struct { + X SHORT + Y SHORT + } + + CONSOLE_SCREEN_BUFFER_INFO struct { + Size COORD + CursorPosition COORD + Attributes WORD + Window SMALL_RECT + MaximumWindowSize COORD + } +) + +var kernel32DLL = syscall.NewLazyDLL("kernel32.dll") +var getConsoleScreenBufferInfoProc = kernel32DLL.NewProc("GetConsoleScreenBufferInfo") + +func getError(r1, r2 uintptr, lastErr error) error { + // If the function fails, the return value is zero. + if r1 == 0 { + if lastErr != nil { + return lastErr + } + return syscall.EINVAL + } + return nil +} + +func getStdHandle(stdhandle int) (uintptr, error) { + handle, err := syscall.GetStdHandle(stdhandle) + if err != nil { + return 0, err + } + return uintptr(handle), nil +} + +// GetConsoleScreenBufferInfo retrieves information about the specified console screen buffer. +// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx +func GetConsoleScreenBufferInfo(handle uintptr) (*CONSOLE_SCREEN_BUFFER_INFO, error) { + var info CONSOLE_SCREEN_BUFFER_INFO + if err := getError(getConsoleScreenBufferInfoProc.Call(handle, uintptr(unsafe.Pointer(&info)), 0)); err != nil { + return nil, err + } + return &info, nil +} + +func getTerminalColumns() int { + defaultWidth := 80 + + stdoutHandle, err := getStdHandle(syscall.STD_OUTPUT_HANDLE) + if err != nil { + return defaultWidth + } + + info, err := GetConsoleScreenBufferInfo(stdoutHandle) + if err != nil { + return defaultWidth + } + + if info.MaximumWindowSize.X > 0 { + return int(info.MaximumWindowSize.X) + } + + return defaultWidth +} diff --git a/main.go b/main.go index e1c59a6..0feb292 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "github.com/lightninglabs/lndclient" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" + "github.com/rkfg/regolancer/helpmessage" ) type configParams struct { @@ -465,16 +466,16 @@ func main() { // Print own Help message instead of using the builtin Help function of goflags to group output if params.Help { - var opt Options + var opt helpmessage.Options if reflect.ValueOf(params).Kind() == reflect.Struct { v := reflect.ValueOf(params) - err := scanStruct(v, &opt) + err := helpmessage.ScanStruct(v, &opt) if err != nil { fmt.Println(err) os.Exit(1) } var b bytes.Buffer - WriteHelp(&opt, parser, &b) + helpmessage.WriteHelp(&opt, parser, &b) } os.Exit(1) }