From 49b7c6f53f1354a747ff16910ceb5db69bf39a9a Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Fri, 26 Jul 2024 20:19:03 +0100 Subject: [PATCH 1/2] request: improve error report when unmarshal fails --- request.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/request.go b/request.go index 66e98aa..45d8e14 100644 --- a/request.go +++ b/request.go @@ -184,7 +184,11 @@ func unmarshalResponse[T any](response RawResponse, v *T) (T, error) { err := json.Unmarshal(response, &v) if err != nil { - return *v, fmt.Errorf("error during unmarshal: %w", err) + return *v, fmt.Errorf( + "error while unmarshal: %w, response: %s", + err, + response, + ) } return *v, nil } From a1fde7b9c73a8ba43536f978a6fcacd93e632bfc Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Fri, 26 Jul 2024 20:32:20 +0100 Subject: [PATCH 2/2] request: implement SwitchXkbLayout --- request.go | 14 +++++++++++++- request_test.go | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/request.go b/request.go index 45d8e14..4e66769 100644 --- a/request.go +++ b/request.go @@ -482,7 +482,7 @@ func (c *RequestClient) Reload() (r Response, err error) { } // Set cursor command, similar to 'hyprctl setcursor'. -// Returns a [Response], that may be useful for further validations +// Returns a [Response], that may be useful for further validations. func (c *RequestClient) SetCursor(theme string, size int) (r Response, err error) { raw, err := c.doRequest("setcursor", fmt.Sprintf("%s %d", theme, size)) if err != nil { @@ -492,6 +492,18 @@ func (c *RequestClient) SetCursor(theme string, size int) (r Response, err error return response[0], err // should return only one response } +// Set cursor command, similar to 'hyprctl switchxkblayout'. +// Returns a [Response], that may be useful for further validations. +// Param cmd can be either 'next', 'prev' or an ID (e.g: 0). +func (c *RequestClient) SwitchXkbLayout(device string, cmd string) (r Response, err error) { + raw, err := c.doRequest("switchxkblayout", fmt.Sprintf("%s %s", device, cmd)) + if err != nil { + return r, err + } + response, err := parseAndValidateResponse(nil, raw) + return response[0], err // should return only one response +} + // Splash command, similar to 'hyprctl splash'. func (c *RequestClient) Splash() (s string, err error) { response, err := c.doRequest("splash") diff --git a/request_test.go b/request_test.go index 0c82c93..ffb4a81 100644 --- a/request_test.go +++ b/request_test.go @@ -353,6 +353,17 @@ func TestSetCursor(t *testing.T) { }) } +func TestSwitchXkbLayout(t *testing.T) { + // Need to find a keyboard, call Devices() + checkEnvironment(t) + devices, err := c.Devices() + assert.NoError(t, err) + + testCommandR(t, func() (Response, error) { + return c.SwitchXkbLayout(devices.Keyboards[0].Name, "next") + }) +} + func TestSplash(t *testing.T) { testCommand(t, c.Splash, "") }