From 39ea6021ad23506952abb021ebb4a2e0ec55cee6 Mon Sep 17 00:00:00 2001 From: FlUxIuS Date: Thu, 1 Aug 2024 20:35:46 +0200 Subject: [PATCH] Fixing resize feature based on system --- go/rfswift/common/common.go | 4 ++-- go/rfswift/dock/rfdock.go | 11 +++++------ go/rfswift/dock/terminal_darwin.go | 9 +++++++++ go/rfswift/dock/terminal_linux.go | 9 +++++++++ go/rfswift/dock/terminal_windows.go | 17 +++++++++++++++++ go/rfswift/go.mod | 3 +-- go/rfswift/go.sum | 7 ++----- 7 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 go/rfswift/dock/terminal_darwin.go create mode 100644 go/rfswift/dock/terminal_linux.go create mode 100644 go/rfswift/dock/terminal_windows.go diff --git a/go/rfswift/common/common.go b/go/rfswift/common/common.go index da80c7f..2ba9852 100644 --- a/go/rfswift/common/common.go +++ b/go/rfswift/common/common.go @@ -5,7 +5,7 @@ import ( "strings" ) -var Version = "0.4.7" +var Version = "0.4.8" var Branch = "main" var ascii_art = ` @@ -82,4 +82,4 @@ func PrintSuccessMessage(message string) { white := "\033[37m" reset := "\033[0m" fmt.Printf("%s[+] %s%s%s\n", green, white, message, reset) -} \ No newline at end of file +} diff --git a/go/rfswift/dock/rfdock.go b/go/rfswift/dock/rfdock.go index f681092..8da7101 100644 --- a/go/rfswift/dock/rfdock.go +++ b/go/rfswift/dock/rfdock.go @@ -95,13 +95,12 @@ func updateDockerObjFromConfig() { } func resizeTty(ctx context.Context, cli *client.Client, contid string, fd int) { - /** - * Resizes TTY to handle larger terminal window - */ for { - width, height, err := terminal.GetSize(fd) + width, height, err := getTerminalSize(fd) if err != nil { - panic(err) + log.Printf("Error getting terminal size: %v", err) + time.Sleep(1 * time.Second) + continue } err = cli.ContainerResize(ctx, contid, container.ResizeOptions{ @@ -109,7 +108,7 @@ func resizeTty(ctx context.Context, cli *client.Client, contid string, fd int) { Width: uint(width), }) if err != nil { - panic(err) + log.Printf("Error resizing container TTY: %v", err) } time.Sleep(1 * time.Second) diff --git a/go/rfswift/dock/terminal_darwin.go b/go/rfswift/dock/terminal_darwin.go new file mode 100644 index 0000000..91da075 --- /dev/null +++ b/go/rfswift/dock/terminal_darwin.go @@ -0,0 +1,9 @@ +package dock + +import ( + "golang.org/x/crypto/ssh/terminal" +) + +func getTerminalSize(fd int) (int, int, error) { + return terminal.GetSize(fd) +} \ No newline at end of file diff --git a/go/rfswift/dock/terminal_linux.go b/go/rfswift/dock/terminal_linux.go new file mode 100644 index 0000000..4c7344d --- /dev/null +++ b/go/rfswift/dock/terminal_linux.go @@ -0,0 +1,9 @@ +package dock + +import ( + "golang.org/x/crypto/ssh/terminal" +) + +func getTerminalSize(fd int) (int, int, error) { + return terminal.GetSize(fd) +} diff --git a/go/rfswift/dock/terminal_windows.go b/go/rfswift/dock/terminal_windows.go new file mode 100644 index 0000000..9cf390d --- /dev/null +++ b/go/rfswift/dock/terminal_windows.go @@ -0,0 +1,17 @@ +package dock + +import ( + "github.com/Azure/go-ansiterm/winterm" +) + +func getTerminalSize(fd int) (int, int, error) { + var info winterm.CONSOLE_SCREEN_BUFFER_INFO + handle := winterm.GetStdHandle(winterm.STD_OUTPUT_HANDLE) + err := winterm.GetConsoleScreenBufferInfo(handle, &info) + if err != nil { + return 0, 0, err + } + width := int(info.Window.Right - info.Window.Left + 1) + height := int(info.Window.Bottom - info.Window.Top + 1) + return width, height, nil +} \ No newline at end of file diff --git a/go/rfswift/go.mod b/go/rfswift/go.mod index 54006cf..d576664 100644 --- a/go/rfswift/go.mod +++ b/go/rfswift/go.mod @@ -3,6 +3,7 @@ module penthertz/rfswift go 1.22.5 require ( + github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 github.com/cheggaaa/pb/v3 v3.1.5 github.com/docker/docker v27.1.1+incompatible github.com/fatih/color v1.17.0 @@ -10,14 +11,12 @@ require ( github.com/lawl/pulseaudio v0.0.0-20220626105240-976bed5e247c github.com/mattn/go-runewidth v0.0.16 github.com/moby/term v0.5.0 - github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.8.1 golang.org/x/crypto v0.25.0 golang.org/x/term v0.22.0 ) require ( - github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/Microsoft/go-winio v0.4.14 // indirect github.com/VividCortex/ewma v1.2.0 // indirect github.com/containerd/log v0.1.0 // indirect diff --git a/go/rfswift/go.sum b/go/rfswift/go.sum index d8ac8f5..af2ae30 100644 --- a/go/rfswift/go.sum +++ b/go/rfswift/go.sum @@ -1,5 +1,5 @@ -github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= -github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= +github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= @@ -54,7 +54,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= @@ -63,8 +62,6 @@ github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=