Skip to content

Commit

Permalink
request: make all commands return at least RawResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Jul 22, 2024
1 parent c92ac22 commit ba2b2f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
40 changes: 25 additions & 15 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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'.
Expand All @@ -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'.
Expand Down
24 changes: 18 additions & 6 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down

0 comments on commit ba2b2f3

Please sign in to comment.