Skip to content

Commit

Permalink
event: create a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Aug 31, 2024
1 parent 2dc46dc commit ca14584
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
8 changes: 6 additions & 2 deletions event.go → event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"strings"

"github.com/thiagokokada/hyprland-go/internal/assert"
"github.com/thiagokokada/hyprland-go/internal/helpers"
)

const sep = ">>"
const (
bufSize = 8192
sep = ">>"
)

// Initiate a new client or panic.
// This should be the preferred method for user scripts, since it will
Expand All @@ -18,7 +22,7 @@ const sep = ">>"
// will not panic on error, use [NewEventClient] instead.
// Experimental: WIP
func MustEventClient() *EventClient {
return assert.Must1(NewEventClient(mustSocket(".socket2.sock")))
return assert.Must1(NewEventClient(helpers.MustSocket(".socket2.sock")))
}

// Initiate a new event client.
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package helpers

import (
"os"
"os/user"
"path/filepath"

"github.com/thiagokokada/hyprland-go/internal/assert"
)

// Returns a Hyprland socket or panics.
func MustSocket(socket string) string {
his := os.Getenv("HYPRLAND_INSTANCE_SIGNATURE")
if his == "" {
panic("HYPRLAND_INSTANCE_SIGNATURE is empty, are you using Hyprland?")
}

// https://github.com/hyprwm/Hyprland/blob/83a5395eaa99fecef777827fff1de486c06b6180/hyprctl/main.cpp#L53-L62
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
if runtimeDir == "" {
user := assert.Must1(user.Current()).Uid
runtimeDir = filepath.Join("/run/user", user)
}
return filepath.Join(runtimeDir, "hypr", his, socket)
}
22 changes: 2 additions & 20 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import (
"fmt"
"io"
"net"
"os"
"os/user"
"path/filepath"
"strings"

"github.com/thiagokokada/hyprland-go/internal/assert"
"github.com/thiagokokada/hyprland-go/internal/helpers"
)

const (
Expand Down Expand Up @@ -207,29 +204,14 @@ func (c *RequestClient) doRequest(command string, params ...string) (response Ra
return buf.Bytes(), nil
}

func mustSocket(socket string) string {
his := os.Getenv("HYPRLAND_INSTANCE_SIGNATURE")
if his == "" {
panic("HYPRLAND_INSTANCE_SIGNATURE is empty, are you using Hyprland?")
}

// https://github.com/hyprwm/Hyprland/blob/83a5395eaa99fecef777827fff1de486c06b6180/hyprctl/main.cpp#L53-L62
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
if runtimeDir == "" {
user := assert.Must1(user.Current()).Uid
runtimeDir = filepath.Join("/run/user", user)
}
return filepath.Join(runtimeDir, "hypr", his, socket)
}

// Initiate a new client or panic.
// This should be the preferred method for user scripts, since it will
// automatically find the proper socket to connect and use the
// HYPRLAND_INSTANCE_SIGNATURE for the current user.
// If you need to connect to arbitrary user instances or need a method that
// will not panic on error, use [NewClient] instead.
func MustClient() *RequestClient {
return NewClient(mustSocket(".socket.sock"))
return NewClient(helpers.MustSocket(".socket.sock"))
}

// Initiate a new client.
Expand Down

0 comments on commit ca14584

Please sign in to comment.