Skip to content

Commit d567ec3

Browse files
authored
feat(wasm): add configuration to terminal size (#3208)
1 parent 305cefc commit d567ec3

13 files changed

Lines changed: 151 additions & 10 deletions

File tree

cmd/scw-wasm/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func main() {
2222
cliPackage := js.ValueOf(map[string]any{})
2323
cliPackage.Set("run", js.FuncOf(jshelpers.AsPromise(wasm.Run)))
2424
cliPackage.Set("complete", js.FuncOf(jshelpers.AsPromise(wasm.Autocomplete)))
25+
cliPackage.Set("configureOutput", js.FuncOf(jshelpers.AsPromise(wasm.ConfigureOutput)))
2526
cliPackage.Set("stop", js.FuncOf(jshelpers.AsyncJsFunc(stop)))
2627
js.Global().Set(args.targetObject, cliPackage)
2728
}

internal/human/marshal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func computeMaxCols(grid [][]string) int {
418418
maxCols := len(grid[0])
419419
width := terminal.GetWidth()
420420
// If we are not writing to Stdout or through a tty Stdout, returns max length
421-
if color.NoColor || width == 0 {
421+
if !terminal.IsTerm() || width == 0 {
422422
return maxCols
423423
}
424424
colMaxSize := make([]int, len(grid[0]))

internal/interactive/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ var (
1818
// IsInteractive must be set to print anything with Printer functions (Print, Printf,...).
1919
IsInteractive = isInteractive()
2020

21+
// TerminalOutput define if the
22+
TerminalOutput = IsInteractive
23+
2124
// outputWriter is the writer used by Printer functions (Print, Printf,...).
2225
outputWriter io.Writer
2326

internal/jshelpers/objects.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ import (
88
"syscall/js"
99
)
1010

11-
func asString(value js.Value) (string, error) {
12-
if value.Type() == js.TypeString {
13-
return value.String(), nil
14-
}
15-
return "", fmt.Errorf("value type should be string")
16-
}
17-
1811
func goValue(typ reflect.Type, value js.Value) (any, error) {
1912
switch typ.Kind() {
2013
case reflect.Pointer:
@@ -25,6 +18,10 @@ func goValue(typ reflect.Type, value js.Value) (any, error) {
2518
return asObject(typ, value)
2619
case reflect.Slice:
2720
return asSlice(typ.Elem(), value)
21+
case reflect.Int:
22+
return asInt(value)
23+
case reflect.Bool:
24+
return asBool(value)
2825
}
2926
return nil, fmt.Errorf("value type is unknown")
3027
}

internal/jshelpers/scalar.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build js
2+
3+
package jshelpers
4+
5+
import (
6+
"fmt"
7+
"math"
8+
"syscall/js"
9+
)
10+
11+
func asString(value js.Value) (string, error) {
12+
if value.Type() == js.TypeString {
13+
return value.String(), nil
14+
}
15+
return "", fmt.Errorf("value type should be string")
16+
}
17+
18+
func asInt(value js.Value) (int, error) {
19+
if value.Type() != js.TypeNumber {
20+
return 0, fmt.Errorf("value type should be number")
21+
}
22+
f := value.Float()
23+
24+
if f != math.Trunc(f) {
25+
return 0, fmt.Errorf("expected an int")
26+
}
27+
28+
return int(f), nil
29+
}
30+
31+
func asBool(value js.Value) (bool, error) {
32+
if value.Type() == js.TypeBoolean {
33+
return value.Bool(), nil
34+
}
35+
36+
return false, fmt.Errorf("value type should be boolean")
37+
}

internal/terminal/terminal.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
//go:build !wasm
2+
13
package terminal
24

35
import (
46
"os"
57

6-
"github.com/fatih/color"
78
"golang.org/x/term"
9+
10+
"github.com/fatih/color"
811
)
912

1013
func Style(msg string, styles ...color.Attribute) string {
@@ -26,3 +29,8 @@ func GetHeight() int {
2629
}
2730
return h
2831
}
32+
33+
// IsTerm returns if stdout is considered a tty
34+
func IsTerm() bool {
35+
return !color.NoColor
36+
}

internal/terminal/terminal_wasm.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build wasm
2+
3+
package terminal
4+
5+
import (
6+
"github.com/fatih/color"
7+
)
8+
9+
var (
10+
Width int = 0
11+
Height int = 0
12+
)
13+
14+
func Style(msg string, styles ...color.Attribute) string {
15+
return color.New(styles...).Sprint(msg)
16+
}
17+
18+
func GetWidth() int {
19+
return Width
20+
}
21+
22+
func GetHeight() int {
23+
return Height
24+
}
25+
26+
// IsTerm returns if stdout is considered a tty
27+
func IsTerm() bool {
28+
return true
29+
}

internal/wasm/autocomplete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build wasm
2+
13
package wasm
24

35
import (

internal/wasm/output.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build wasm
2+
3+
package wasm
4+
5+
import (
6+
"github.com/fatih/color"
7+
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
8+
)
9+
10+
type ConfigureOutputConfig struct {
11+
Width int `js:"width"`
12+
Color bool `js:"color"`
13+
}
14+
15+
type ConfigureOutputResponse struct {
16+
}
17+
18+
func ConfigureOutput(cfg *ConfigureOutputConfig) (*ConfigureOutputResponse, error) {
19+
terminal.Width = cfg.Width
20+
color.NoColor = !cfg.Color
21+
22+
return &ConfigureOutputResponse{}, nil
23+
}

internal/wasm/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build wasm
2+
13
package wasm
24

35
import (

0 commit comments

Comments
 (0)