From ba2b2f35cc3f772005cd51426c6efe1fab099bf9 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Mon, 22 Jul 2024 23:39:08 +0100 Subject: [PATCH] request: make all commands return at least RawResponse --- request.go | 40 +++++++++++++++++++++++++--------------- request_test.go | 24 ++++++++++++++++++------ 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/request.go b/request.go index 1eebe5b..2b9b2c9 100644 --- a/request.go +++ b/request.go @@ -250,12 +250,14 @@ func (c *RequestClient) CursorPos() (cu CursorPos, err error) { // Dispatch commands, similar to 'hyprctl dispatch'. // Accept multiple commands at the same time, in this case it will use batch // mode, similar to 'hyprctl dispatch --batch'. -func (c *RequestClient) Dispatch(params ...string) error { +// Returns the raw response, that may be useful for further validations, +// especially when [RequestClient] 'Validation' is set to false. +func (c *RequestClient) Dispatch(params ...string) (r RawResponse, err error) { response, err := c.doRequest("dispatch", params...) if err != nil { - return err + return response, err } - return c.validateResponse(params, response) + return response, c.validateResponse(params, response) } // Get option command, similar to 'hyprctl getoption'. @@ -271,22 +273,26 @@ func (c *RequestClient) GetOption(name string) (o Option, err error) { // Keyword command, similar to 'hyprctl keyword'. // Accept multiple commands at the same time, in this case it will use batch // mode, similar to 'hyprctl keyword --batch'. -func (c *RequestClient) Keyword(params ...string) error { +// Returns the raw response, that may be useful for further validations, +// especially when [RequestClient] 'Validation' is set to false. +func (c *RequestClient) Keyword(params ...string) (r RawResponse, err error) { response, err := c.doRequest("keyword", params...) if err != nil { - return err + return response, err } - return c.validateResponse(nil, response) + return response, c.validateResponse(nil, response) } // Kill command, similar to 'hyprctl kill'. // Will NOT wait for the user to click in the window. -func (c *RequestClient) Kill() error { +// Returns the raw response, that may be useful for further validations, +// especially when [RequestClient] 'Validation' is set to false. +func (c *RequestClient) Kill() (r RawResponse, err error) { response, err := c.doRequest("kill") if err != nil { - return err + return response, err } - return c.validateResponse(nil, response) + return response, c.validateResponse(nil, response) } // Monitors command, similar to 'hyprctl monitors'. @@ -300,21 +306,25 @@ func (c *RequestClient) Monitors() (m []Monitor, err error) { } // Reload command, similar to 'hyprctl reload'. -func (c *RequestClient) Reload() error { +// Returns the raw response, that may be useful for further validations, +// especially when [RequestClient] 'Validation' is set to false. +func (c *RequestClient) Reload() (r RawResponse, err error) { response, err := c.doRequest("reload") if err != nil { - return err + return response, err } - return c.validateResponse(nil, response) + return response, c.validateResponse(nil, response) } // Set cursor command, similar to 'hyprctl setcursor'. -func (c *RequestClient) SetCursor(theme string, size int) error { +// Returns the raw response, that may be useful for further validations, +// especially when [RequestClient] 'Validation' is set to false. +func (c *RequestClient) SetCursor(theme string, size int) (r RawResponse, err error) { response, err := c.doRequest("setcursor", fmt.Sprintf("%s %d", theme, size)) if err != nil { - return err + return response, err } - return c.validateResponse(nil, response) + return response, c.validateResponse(nil, response) } // Splash command, similar to 'hyprctl splash'. diff --git a/request_test.go b/request_test.go index f73b74c..2889ce1 100644 --- a/request_test.go +++ b/request_test.go @@ -33,12 +33,15 @@ func checkEnvironment(t *testing.T) { } } -func testCommand(t *testing.T, command func() error) { +func testCommand(t *testing.T, command func() (RawResponse, error)) { checkEnvironment(t) - err := command() + response, err := command() if err != nil { t.Error(err) } + if len(response) == 0 { + t.Error("empty response") + } } func testCommand1[T any](t *testing.T, command func() (T, error), v any) { @@ -164,16 +167,19 @@ func TestCursorPos(t *testing.T) { func TestDispatch(t *testing.T) { checkEnvironment(t) - err := c.Dispatch("exec kitty") + response, err := c.Dispatch("exec kitty") if err != nil { t.Error(err) } + if len(response) == 0 { + t.Error("empty response") + } if testing.Short() { t.Skip("skipping slow test") } - err = c.Dispatch(genParams("exec kitty", 40)...) + _, err = c.Dispatch(genParams("exec kitty", 40)...) if err != nil { t.Error(err) } @@ -202,7 +208,10 @@ func TestGetOption(t *testing.T) { func TestKeyword(t *testing.T) { checkEnvironment(t) - err := c.Keyword("general:border_size 1", "general:border_size 5") + response, err := c.Keyword("general:border_size 1", "general:border_size 5") + if len(response) == 0 { + t.Error("empty response") + } if err != nil { t.Error(err) } @@ -222,10 +231,13 @@ func TestReload(t *testing.T) { func TestSetCursor(t *testing.T) { checkEnvironment(t) - err := c.SetCursor("Nordzy-cursors", 32) + response, err := c.SetCursor("Nordzy-cursors", 32) if err != nil { t.Error(err) } + if len(response) == 0 { + t.Error("empty response") + } } func TestSplash(t *testing.T) {