Skip to content

Commit

Permalink
feat: registers
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 8, 2024
1 parent cca917d commit 8e716cb
Show file tree
Hide file tree
Showing 20 changed files with 235 additions and 54 deletions.
26 changes: 26 additions & 0 deletions pkg/bind/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,29 @@ func TestRegex(t *testing.T) {
})
require.Equal(t, false, matched)
}

func TestMultipleRegex(t *testing.T) {
trie := New[int](nil)
trie.Set([]interface{}{
re("[abc]"),
"t",
}, 2)
trie.Set([]interface{}{
re("[abc]"),
"j",
}, 3)

_, re, matched := trie.Get([]string{
"a",
"t",
})
require.Equal(t, []string{"a"}, re)
require.Equal(t, true, matched)

_, _, matched = trie.Get([]string{
"a",
"j",
})
require.Equal(t, []string{"a"}, re)
require.Equal(t, true, matched)
}
27 changes: 21 additions & 6 deletions pkg/cy/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,31 @@ func (c *CyModule) Trace(user interface{}) error {
return nil
}

func (c *CyModule) Paste(user interface{}) {
func (c *CyModule) Paste(user interface{}, register string) error {
client, ok := user.(*Client)
if !ok {
return
return fmt.Errorf("no user")
}

buffer := client.buffer
if len(buffer) == 0 {
return
if register != "+" {
buffer := client.buffer
if len(buffer) == 0 {
return nil
}

client.binds.Input([]byte(buffer))
return nil
}

client.binds.Input([]byte(buffer))
// + reads from the system clipboard instead
data, err := c.cy.options.Clipboard.Read()
if err != nil {
return fmt.Errorf(
"failed to read system clipboard: %w",
err,
)
}

client.binds.Input([]byte(data))
return nil
}
17 changes: 0 additions & 17 deletions pkg/cy/api/clipboard.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/cy/api/clipboard_test.janet

This file was deleted.

9 changes: 0 additions & 9 deletions pkg/cy/api/docs-clipboard.md

This file was deleted.

11 changes: 11 additions & 0 deletions pkg/cy/api/docs-register.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# doc: Set

(register/set register value)

Store a string in the given register, which can be any string. The "+" register refers to the system clipboard.

# doc: Get

(register/get register)

Get the value stored in the given register. The "+" register refers to the system clipboard.
10 changes: 5 additions & 5 deletions pkg/cy/api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ func (t *ColorMapsModule) Documentation() string {
return DOCS_COLORMAPS
}

//go:embed docs-clipboard.md
var DOCS_CLIPBOARD string
//go:embed docs-register.md
var DOCS_REGISTER string

var _ janet.Documented = (*ClipboardModule)(nil)
var _ janet.Documented = (*RegisterModule)(nil)

func (c *ClipboardModule) Documentation() string {
return DOCS_CLIPBOARD
func (c *RegisterModule) Documentation() string {
return DOCS_REGISTER
}
6 changes: 6 additions & 0 deletions pkg/cy/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ func getClient(context interface{}) (Client, error) {

return client, nil
}

type Registers interface {
Set(register string, text string) error
Get(register string) (string, error)
GetAll() (map[string]string, error)
}
18 changes: 18 additions & 0 deletions pkg/cy/api/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package api

type RegisterModule struct {
Registers Registers
}

func (c *RegisterModule) Set(
register string,
text string,
) error {
return c.Registers.Set(register, text)
}

func (c *RegisterModule) Get(
register string,
) (string, error) {
return c.Registers.Get(register)
}
7 changes: 7 additions & 0 deletions pkg/cy/api/register_test.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(test "registers"
(register/set "a" "a")
(register/set "b" "b")
(register/set "+" "test")
(assert (= "a" (register/get "a")))
(assert (= "b" (register/get "b")))
(assert (= "test" (register/get "+"))))
6 changes: 4 additions & 2 deletions pkg/cy/api/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ func (m *ReplayModule) TimePlaybackRate(context interface{}, rate int) error {
})
}

func (m *ReplayModule) Copy(context interface{}) error {
return m.sendAction(context, replay.ActionCopy)
func (m *ReplayModule) Copy(context interface{}, register string) error {
return m.send(context, replay.CopyEvent{
Register: register,
})
}

func (m *ReplayModule) Select(context interface{}) error {
Expand Down
7 changes: 5 additions & 2 deletions pkg/cy/boot/binds.janet
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
[prefix "p"] action/open-replay
[prefix "r"] action/reload-config
[prefix "S"] action/search-borg
[prefix "P"] cy/paste)
[prefix "\"" [:re "[a-zA-Z0-9+]"] "p"] register/insert
[prefix "P"] action/paste)

(key/bind-many-tag :root "panes"
[prefix "ctrl+i"] pane/history-forward
Expand Down Expand Up @@ -108,7 +109,9 @@

(key/bind-many-tag :copy "general"
["v"] replay/select
["y"] replay/copy)
["y"] replay/copy-default
["\"" "+" "y"] replay/copy-clipboard
["\"" [:re "[a-zA-Z0-9]"] "y"] replay/copy)

(key/bind-many-tag :copy "motion"
["g" "g"] replay/beginning
Expand Down
29 changes: 29 additions & 0 deletions pkg/cy/boot/register.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(defn
clipboard/get
"Get the contents of the system clipboard."
[]
(register/get "+"))

(defn
clipboard/set
"Set the contents of the system clipboard."
[text]
(register/set "+" text))

(defn
register/insert
"Insert the contents of the given register in the current pane."
[register]
(pane/send-keys
(pane/current)
@[(register/get register)]))

(key/action
action/paste
"Insert the contents of the default register."
(register/insert ""))

(key/action
action/paste-clipboard
"Insert the contents of the system clipboard."
(register/insert "+"))
11 changes: 11 additions & 0 deletions pkg/cy/boot/replay.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(defn
replay/copy-default
"Yank the selection into the default register."
[]
(replay/copy ""))

(defn
replay/copy-clipboard
"Yank the selection into the system clipboard."
[]
(replay/copy "+"))
6 changes: 4 additions & 2 deletions pkg/cy/janet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (c *Cy) initJanet(ctx context.Context) (*janet.VM, error) {
}

modules := map[string]interface{}{
"clipboard": &api.ClipboardModule{
Clipboard: c.options.Clipboard,
"register": &api.RegisterModule{
Registers: c.registers,
},
"cmd": &api.CmdModule{
Server: c,
Expand Down Expand Up @@ -84,6 +84,8 @@ func (c *Cy) initJanet(ctx context.Context) (*janet.VM, error) {
"style.janet",
"colors.janet",
"layout.janet",
"register.janet",
"replay.janet",
"binds.janet",
}

Expand Down
22 changes: 19 additions & 3 deletions pkg/cy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ type Options struct {
SocketPath string
// The name of the socket (before calculating the real path.)
SocketName string
// The Clipboard that will be used by (clipboard/set) and (clipboard/get).
Clipboard clipboard.Clipboard
// The current working directory where the first shell will start.
// If not set, defaults to $HOME.
Cwd string
Clipboard clipboard.Clipboard
Cwd string
}

type historyEvent struct {
Expand All @@ -60,6 +61,7 @@ type Cy struct {
util.Lifetime
deadlock.RWMutex
*janet.VM
registers *memoryRegisters

cmdStore *cmd.Store

Expand Down Expand Up @@ -322,7 +324,20 @@ func (c *Cy) pollNodeEvents(ctx context.Context, events <-chan events.Msg) {

switch event := nodeEvent.Event.(type) {
case replay.CopyEvent:
client.buffer = event.Text
err := c.registers.Set(
event.Register,
event.Text,
)

if err == nil {
continue
}

c.log.
Error().
Err(err).Msg(
"error copying to clipboard",
)
case bind.BindEvent:
go client.runAction(event)
case cmd.CommandEvent:
Expand Down Expand Up @@ -363,6 +378,7 @@ func Start(ctx context.Context, options Options) (*Cy, error) {
t := tree.NewTree(tree.WithParams(defaults.NewChild()))
cy := Cy{
Lifetime: util.NewLifetime(ctx),
registers: newMemoryRegisters(options.Clipboard),
cmdStore: cmd.NewStore(),
copyBinds: copyBinds,
defaults: defaults,
Expand Down
62 changes: 62 additions & 0 deletions pkg/cy/registers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cy

import (
"github.com/cfoust/cy/pkg/clipboard"
"github.com/cfoust/cy/pkg/cy/api"

"github.com/sasha-s/go-deadlock"
)

type memoryRegisters struct {
deadlock.RWMutex
clipboard clipboard.Clipboard
registers map[string]string
}

var _ api.Registers = (*memoryRegisters)(nil)

func (r *memoryRegisters) Set(register string, text string) error {
if register == "+" {
return r.clipboard.Write(text)
}

r.Lock()
defer r.Unlock()
r.registers[register] = text
return nil
}

func (r *memoryRegisters) Get(register string) (string, error) {
if register == "+" {
return r.clipboard.Read()
}

r.RLock()
defer r.RUnlock()
return r.registers[register], nil
}

func (r *memoryRegisters) GetAll() (map[string]string, error) {
r.RLock()
defer r.RUnlock()
cloned := make(map[string]string)

for k, v := range r.registers {
cloned[k] = v
}

// Attempt to get system clipboard, but don't worry if we can't
clipboard, err := r.clipboard.Read()
if err == nil {
cloned["+"] = clipboard
}

return cloned, nil
}

func newMemoryRegisters(clipboard clipboard.Clipboard) *memoryRegisters {
return &memoryRegisters{
registers: make(map[string]string),
clipboard: clipboard,
}
}
3 changes: 2 additions & 1 deletion pkg/replay/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type frameDoneEvent struct {
}

type CopyEvent struct {
Text string
Register string
Text string
}

type Mode uint8
Expand Down
Loading

0 comments on commit 8e716cb

Please sign in to comment.