Skip to content

Commit

Permalink
Merge pull request #8 from thiagokokada/fix-batch
Browse files Browse the repository at this point in the history
Fix batch command support
  • Loading branch information
labi-le authored Jul 21, 2024
2 parents d6f7e8a + 1dcd26c commit 04557a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
12 changes: 11 additions & 1 deletion argv.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func (q *PriorityQueue[T]) Back(value T) {

type ByteQueue struct {
*PriorityQueue[[]byte]
batch bool
command []byte
}

func NewByteQueue() *ByteQueue {
Expand All @@ -117,9 +119,17 @@ func NewByteQueue() *ByteQueue {
func (q *ByteQueue) Glue() []byte {
var glued []byte

if q.batch {
glued = append(glued, []byte("[[BATCH]]")...)
}

for !q.IsEmpty() {
if q.command != nil {
glued = append(glued, q.command...)
glued = append(glued, ' ')
}
glued = append(glued, q.Pop()...)
glued = append(glued, ' ')
glued = append(glued, ';')
}

if len(glued) > 0 {
Expand Down
4 changes: 3 additions & 1 deletion argv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (

func Test_priorityQueue_Add(t *testing.T) {
queue := NewByteQueue()
queue.batch = true
queue.command = []byte("command")
queue.Add([]byte("name"))
queue.Add([]byte("ivan"))
queue.Back([]byte("my"))
queue.Back([]byte("hello"))

glued := queue.Glue()
if !bytes.Equal(glued, []byte("hello my name ivan")) {
if !bytes.Equal(glued, []byte("[[BATCH]]command hello;command my;command name;command ivan")) {
t.Errorf("got %s", glued)
}
}
14 changes: 7 additions & 7 deletions ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewClient(readSock, writeSock string) *IPCClient {
}

func (c *IPCClient) request(q *ByteQueue) ([]byte, error) {
if q.Len() == 0 {
if q.command == nil && q.Len() == 0 {
return nil, errors.New("wtfuq man you need to pass some args")
}

Expand All @@ -79,7 +79,7 @@ func (c *IPCClient) request(q *ByteQueue) ([]byte, error) {
}

if q.Len() > 1 {
q.Back([]byte("[[BATCH]]"))
q.batch = true
}

glued := q.Glue()
Expand Down Expand Up @@ -175,7 +175,7 @@ func (c *IPCClient) Receive() ([]ReceivedData, error) {
}

func (c *IPCClient) Dispatch(a *ByteQueue) ([]byte, error) {
a.Back([]byte("dispatch"))
a.command = []byte("dispatch")

return c.request(a)
}
Expand Down Expand Up @@ -216,7 +216,7 @@ func (c *IPCClient) Devices() (Devices, error) {
}

func (c *IPCClient) Keyword(args *ByteQueue) error {
args.Back([]byte("keyword"))
args.command = []byte("keyword")

response, err := c.request(args)
if err != nil {
Expand Down Expand Up @@ -255,7 +255,7 @@ func (c *IPCClient) SetCursor(theme, size string) error {
q := NewByteQueue()
q.Add(UnsafeBytes(theme))
q.Add(UnsafeBytes(size))
q.Back([]byte("setcursor"))
q.command = []byte("setcursor")

_, err := c.request(q)
return err
Expand All @@ -264,7 +264,7 @@ func (c *IPCClient) SetCursor(theme, size string) error {
func (c *IPCClient) GetOption(name string) (string, error) {
q := NewByteQueue()
q.Add(UnsafeBytes(name))
q.Back([]byte("getoption"))
q.command = []byte("getoption")

buf, err := c.request(q)
if err != nil {
Expand All @@ -276,7 +276,7 @@ func (c *IPCClient) GetOption(name string) (string, error) {

func (c *IPCClient) Splash() (string, error) {
q := NewByteQueue()
q.Back([]byte("splash"))
q.command = []byte("splash")

buf, err := c.request(q)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion ipc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ func Test_ipc_CursorPos(t *testing.T) {

func TestIpc_Dispatch(t *testing.T) {
q := NewByteQueue()
q.Add([]byte("exec"))
q.Add([]byte("exec kitty"))
q.Add([]byte("exec kitty"))
q.Add([]byte("exec kitty"))
_, err := ipctest.Dispatch(q)
if err != nil {
t.Error(err)
Expand Down

0 comments on commit 04557a5

Please sign in to comment.