Skip to content

Commit 5abf789

Browse files
authored
feat set cursor visibility (#36)
# Description This PR adds a new browsers computer set-cursor command to the CLI that allows users to show or hide the mouse cursor in browser computer sessions. This is useful for automations where cursor visibility may interfere with the user experience or for recording purposes. The implementation includes a new SetCursorVisibility method in the BrowserComputerService interface, proper error handling for invalid inputs and non-existent browsers, and updates the kernel-go-sdk from v0.18.0 to v0.19.0 to support the new API endpoint. ## Tested Tested locally it toggles cursor visibility terminal output is <img width="560" height="90" alt="image" src="https://github.com/user-attachments/assets/7c265462-7e77-4ef7-a4b9-69e9cd3cda60" />
1 parent 29988ae commit 5abf789

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

cmd/browsers.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type BrowserComputerService interface {
8686
MoveMouse(ctx context.Context, id string, body kernel.BrowserComputerMoveMouseParams, opts ...option.RequestOption) (err error)
8787
PressKey(ctx context.Context, id string, body kernel.BrowserComputerPressKeyParams, opts ...option.RequestOption) (err error)
8888
Scroll(ctx context.Context, id string, body kernel.BrowserComputerScrollParams, opts ...option.RequestOption) (err error)
89+
SetCursorVisibility(ctx context.Context, id string, body kernel.BrowserComputerSetCursorVisibilityParams, opts ...option.RequestOption) (res *kernel.BrowserComputerSetCursorVisibilityResponse, err error)
8990
TypeText(ctx context.Context, id string, body kernel.BrowserComputerTypeTextParams, opts ...option.RequestOption) (err error)
9091
}
9192

@@ -567,6 +568,11 @@ type BrowsersComputerDragMouseInput struct {
567568
HoldKeys []string
568569
}
569570

571+
type BrowsersComputerSetCursorInput struct {
572+
Identifier string
573+
Hidden bool
574+
}
575+
570576
func (b BrowsersCmd) ComputerClickMouse(ctx context.Context, in BrowsersComputerClickMouseInput) error {
571577
if b.computer == nil {
572578
pterm.Error.Println("computer service not available")
@@ -789,6 +795,32 @@ func (b BrowsersCmd) ComputerDragMouse(ctx context.Context, in BrowsersComputerD
789795
return nil
790796
}
791797

798+
func (b BrowsersCmd) ComputerSetCursor(ctx context.Context, in BrowsersComputerSetCursorInput) error {
799+
if b.computer == nil {
800+
pterm.Error.Println("computer service not available")
801+
return nil
802+
}
803+
br, err := b.resolveBrowserByIdentifier(ctx, in.Identifier)
804+
if err != nil {
805+
return util.CleanedUpSdkError{Err: err}
806+
}
807+
if br == nil {
808+
pterm.Error.Printf("Browser '%s' not found\n", in.Identifier)
809+
return nil
810+
}
811+
body := kernel.BrowserComputerSetCursorVisibilityParams{Hidden: in.Hidden}
812+
_, err = b.computer.SetCursorVisibility(ctx, br.SessionID, body)
813+
if err != nil {
814+
return util.CleanedUpSdkError{Err: err}
815+
}
816+
if in.Hidden {
817+
pterm.Success.Println("Cursor hidden")
818+
} else {
819+
pterm.Success.Println("Cursor shown")
820+
}
821+
return nil
822+
}
823+
792824
// Replays
793825
type BrowsersReplaysListInput struct {
794826
Identifier string
@@ -1955,7 +1987,12 @@ func init() {
19551987
computerDrag.Flags().String("button", "left", "Mouse button: left,middle,right")
19561988
computerDrag.Flags().StringSlice("hold-key", []string{}, "Modifier keys to hold (repeatable)")
19571989

1958-
computerRoot.AddCommand(computerClick, computerMove, computerScreenshot, computerType, computerPressKey, computerScroll, computerDrag)
1990+
// computer set-cursor
1991+
computerSetCursor := &cobra.Command{Use: "set-cursor <id|persistent-id>", Short: "Hide or show the cursor", Args: cobra.ExactArgs(1), RunE: runBrowsersComputerSetCursor}
1992+
computerSetCursor.Flags().String("hidden", "", "Whether to hide the cursor: true or false")
1993+
_ = computerSetCursor.MarkFlagRequired("hidden")
1994+
1995+
computerRoot.AddCommand(computerClick, computerMove, computerScreenshot, computerType, computerPressKey, computerScroll, computerDrag, computerSetCursor)
19591996
browsersCmd.AddCommand(computerRoot)
19601997

19611998
// playwright
@@ -2462,6 +2499,26 @@ func runBrowsersComputerDragMouse(cmd *cobra.Command, args []string) error {
24622499
return b.ComputerDragMouse(cmd.Context(), BrowsersComputerDragMouseInput{Identifier: args[0], Path: path, Delay: delay, StepDelayMs: stepDelayMs, StepsPerSegment: stepsPerSegment, Button: button, HoldKeys: holdKeys})
24632500
}
24642501

2502+
func runBrowsersComputerSetCursor(cmd *cobra.Command, args []string) error {
2503+
client := getKernelClient(cmd)
2504+
svc := client.Browsers
2505+
hiddenStr, _ := cmd.Flags().GetString("hidden")
2506+
2507+
var hidden bool
2508+
switch strings.ToLower(hiddenStr) {
2509+
case "true", "1", "yes":
2510+
hidden = true
2511+
case "false", "0", "no":
2512+
hidden = false
2513+
default:
2514+
pterm.Error.Printf("Invalid value for --hidden: %s (expected true or false)\n", hiddenStr)
2515+
return nil
2516+
}
2517+
2518+
b := BrowsersCmd{browsers: &svc, computer: &svc.Computer}
2519+
return b.ComputerSetCursor(cmd.Context(), BrowsersComputerSetCursorInput{Identifier: args[0], Hidden: hidden})
2520+
}
2521+
24652522
func truncateURL(url string, maxLen int) string {
24662523
if len(url) <= maxLen {
24672524
return url

cmd/browsers_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ type FakeComputerService struct {
533533
ScrollFunc func(ctx context.Context, id string, body kernel.BrowserComputerScrollParams, opts ...option.RequestOption) error
534534
DragMouseFunc func(ctx context.Context, id string, body kernel.BrowserComputerDragMouseParams, opts ...option.RequestOption) error
535535
TypeTextFunc func(ctx context.Context, id string, body kernel.BrowserComputerTypeTextParams, opts ...option.RequestOption) error
536+
SetCursorVisibilityFunc func(ctx context.Context, id string, body kernel.BrowserComputerSetCursorVisibilityParams, opts ...option.RequestOption) (*kernel.BrowserComputerSetCursorVisibilityResponse, error)
536537
}
537538

538539
func (f *FakeComputerService) ClickMouse(ctx context.Context, id string, body kernel.BrowserComputerClickMouseParams, opts ...option.RequestOption) error {
@@ -578,6 +579,12 @@ func (f *FakeComputerService) TypeText(ctx context.Context, id string, body kern
578579
}
579580
return nil
580581
}
582+
func (f *FakeComputerService) SetCursorVisibility(ctx context.Context, id string, body kernel.BrowserComputerSetCursorVisibilityParams, opts ...option.RequestOption) (*kernel.BrowserComputerSetCursorVisibilityResponse, error) {
583+
if f.SetCursorVisibilityFunc != nil {
584+
return f.SetCursorVisibilityFunc(ctx, id, body, opts...)
585+
}
586+
return &kernel.BrowserComputerSetCursorVisibilityResponse{}, nil
587+
}
581588

582589
// --- Tests for Logs ---
583590

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/charmbracelet/fang v0.2.0
99
github.com/golang-jwt/jwt/v5 v5.2.2
1010
github.com/joho/godotenv v1.5.1
11-
github.com/onkernel/kernel-go-sdk v0.18.0
11+
github.com/onkernel/kernel-go-sdk v0.19.0
1212
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
1313
github.com/pterm/pterm v0.12.80
1414
github.com/samber/lo v1.51.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ github.com/muesli/mango-pflag v0.1.0 h1:UADqbYgpUyRoBja3g6LUL+3LErjpsOwaC9ywvBWe
9191
github.com/muesli/mango-pflag v0.1.0/go.mod h1:YEQomTxaCUp8PrbhFh10UfbhbQrM/xJ4i2PB8VTLLW0=
9292
github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8=
9393
github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig=
94-
github.com/onkernel/kernel-go-sdk v0.18.0 h1:hlBqxL2sEUto6h449b93C0YkAQeRdxrhn5cAScbhjaQ=
95-
github.com/onkernel/kernel-go-sdk v0.18.0/go.mod h1:MjUR92i8UPqjrmneyVykae6GuB3GGSmnQtnjf1v74Dc=
94+
github.com/onkernel/kernel-go-sdk v0.19.0 h1:kfLHmcye/zEF9INP3zIXffmVLGFVZiZNFs4NeVAgya0=
95+
github.com/onkernel/kernel-go-sdk v0.19.0/go.mod h1:t80buN1uCA/hwvm4D2SpjTJzZWcV7bWOFo9d7qdXD8M=
9696
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
9797
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
9898
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

0 commit comments

Comments
 (0)