Skip to content

Commit

Permalink
feat: send keys to server
Browse files Browse the repository at this point in the history
  • Loading branch information
bomgar committed Sep 27, 2024
1 parent 4c0be7a commit 4857e59
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 8 deletions.
30 changes: 30 additions & 0 deletions components/base_page.templ
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,36 @@ templ BasePage() {
</head>
<body class={ page() }>
{ children... }
<script>
// Listen for global keypresses on the entire document
document.addEventListener("keydown", function(event) {

// F keys should stay in the browser. for now
if(event.keyCode >= 112 && event.keyCode <= 123) {
return
}

event.preventDefault()
// Send the keypress information to the server
fetch('/keypress', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
key: event.key,
keyCode: event.keyCode,
shiftKey: event.shiftKey,
ctrlKey: event.ctrlKey,
altKey: event.altKey,
metaKey: event.metaKey
})
})
.then(response => response.json())
.then(data => console.log('Server response:', data))
.catch((error) => console.error('Error:', error));
});
</script>
</body>
</html>
}
2 changes: 1 addition & 1 deletion components/base_page_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions key/keypress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package key

type KeyPress struct {
Key string `json:"key"` // The key that was pressed (e.g., "a", "A", "Shift")
KeyCode int `json:"keyCode"` // The numerical code for the key
ShiftKey bool `json:"shiftKey"` // True if the Shift key was pressed
CtrlKey bool `json:"ctrlKey"` // True if the Ctrl key was pressed
AltKey bool `json:"altKey"` // True if the Alt key was pressed
MetaKey bool `json:"metaKey"` // True if the Meta (Command on Mac) key was pressed
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
slog.SetDefault(logger)

server.Run()
Expand Down
17 changes: 14 additions & 3 deletions nvimwrapper/nvim.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"strings"

"github.com/brocode/neoweb/key"
"github.com/brocode/neoweb/nvimwrapper/raster"
"github.com/neovim/go-client/nvim"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func (n *NvimWrapper) handleRedraw(updates ...[]interface{}) {
continue
}

slog.Info("Redraw Event", "name", eventName)
slog.Debug("Redraw Event", "name", eventName)
switch eventName {
case "grid_resize":
va := update[1].([]interface{})
Expand All @@ -103,7 +104,7 @@ func (n *NvimWrapper) handleRedraw(updates ...[]interface{}) {
row := line_data[1].(int64)
col := line_data[2].(int64)

slog.Info("put grid_line", "line", line)
slog.Debug("put grid_line", "line", line)
var buffer bytes.Buffer
// cells is an array of arrays each with 1 to 3 items: [text(, hl_id, repeat)]
for _, cell := range line_data[3].([]interface{}) {
Expand All @@ -114,7 +115,7 @@ func (n *NvimWrapper) handleRedraw(updates ...[]interface{}) {
}
buffer.WriteString(text)
}
slog.Info("put grid_line interpreted", "row", row, "col", col, "text", buffer.String())
slog.Debug("put grid_line interpreted", "row", row, "col", col, "text", buffer.String())
n.r.Put(int(row), int(col), []rune(buffer.String()))
}
}
Expand Down Expand Up @@ -148,6 +149,16 @@ func (w *NvimWrapper) Input(input string) error {
return nil
}

func (w *NvimWrapper) SendKey(keyPress key.KeyPress) {
// TODO actually consider modifiers. "shift" is also a key
err := w.Input(keyPress.Key)

if err != nil {
slog.Error("Failed to process keypress.", "press", keyPress)
}

}

func (w *NvimWrapper) Render() (NvimResult, error) {

lines := w.r.Render()
Expand Down
6 changes: 3 additions & 3 deletions nvimwrapper/raster/raster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ func New() *Raster {
}

func (r *Raster) Resize(cols, rows int) {
slog.Info("Resize raster", "rows", rows, "cols", cols)
slog.Debug("Resize raster", "rows", rows, "cols", cols)
r.raster = make([][]rune, rows)
for i := range r.raster {
r.raster[i] = make([]rune, cols)
}
}

func (r *Raster) CursorGoto(row, col int) {
slog.Info("Cursor Goto", "row", row, "col", col)
slog.Debug("Cursor Goto", "row", row, "col", col)
r.Row = row
r.Col = col
}

func (r *Raster) Put(rowIdx, colIdx int, runes []rune) {
slog.Info("Put", "text", string(runes))
slog.Debug("Put", "text", string(runes))
row := r.raster[rowIdx]
copy(row[colIdx:], runes)
}
Expand Down
14 changes: 14 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package server

import (
"encoding/json"
"log/slog"
"net/http"
"os"

"github.com/brocode/neoweb/components"
"github.com/brocode/neoweb/key"
"github.com/brocode/neoweb/nvimwrapper"
)

Expand Down Expand Up @@ -46,6 +48,18 @@ func Run() {
}
})

mux.HandleFunc("POST /keypress", func(w http.ResponseWriter, r *http.Request) {
var keyPress key.KeyPress
err := json.NewDecoder(r.Body).Decode(&keyPress)
if err != nil {
http.Error(w, "Failed to unmarshall request", 400)
return
}

nvimWrapper.SendKey(keyPress)

})

addr := ":8080"
slog.Info("Start server", "addr", addr)

Expand Down

0 comments on commit 4857e59

Please sign in to comment.