Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleanup event handling logic #9

Merged
merged 6 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
TODO
timeline.md
help.log
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.18-alpine

WORKDIR /app

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY ./ ./

RUN apk add --no-cache bash

RUN go build -o /rowix-server ./server/main.go

EXPOSE 8080

CMD [ "/rowix-server -addr rowix-server.fly.dev" ]
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# rowix

A collaborative text editor written in Go.

## Usage

To start the server:

```
go run server/main.go
```

To start the client:

```
go run client/*.go
```

(spin up at least 2 clients - it's a collaborative editor! Also works with a single client.)

## How does it work?

Here's a basic explanation:

- Each client has a CRDT-backed local state.
- The CRDT has a `Document` which can be represented by a sequence of characters with some attributes.
- The server is responsible for:
- establishing connections with the client
- maintaining a list of active connections
- broadcasting operations sent from a client to other clients
- Clients connect to the server and send operations to the server.
- The TUI is responsible for:
- Rendering document contents
- Handling key events
- Generating payload on key presses
- Dispatch generated payload to the server
173 changes: 173 additions & 0 deletions client/editor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package main

import (
"fmt"

"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)

type Editor struct {
text []rune
x int
y int
width int
height int
}

func NewEditor() *Editor {
return &Editor{
x: 1,
y: 1,
}
}

func (e *Editor) GetText() []rune {
return e.text
}

func (e *Editor) SetText(text string) {
e.text = []rune(text)
}

func (e *Editor) GetX() int {
return e.x
}

func (e *Editor) GetY() int {
return e.y
}

func (e *Editor) GetWidth() int {
return e.width
}

func (e *Editor) GetHeight() int {
return e.height
}

func (e *Editor) SetSize(w, h int) {
e.width = w
e.height = h
}

// AddRune adds a rune to the editor's state and updates position.
func (e *Editor) AddRune(r rune) {
cursor := e.calcCursor()
if cursor == 0 {
e.text = append([]rune{r}, e.text...)
} else if cursor < len(e.text) {
e.text = append(e.text[:cursor], e.text[cursor-1:]...)
e.text[cursor] = r
} else {
e.text = append(e.text[:cursor], r)
}
if r == rune('\n') {
e.x = 1
e.y += 1
} else {
e.x += runewidth.RuneWidth(r)
}
}

// Draw updates the UI by setting cells with the editor's content.
func (e *Editor) Draw() {
_ = termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
termbox.SetCursor(e.x-1, e.y-1)
x := 0
y := 0

for i := 0; i < len(e.text); i++ {
if e.text[i] == rune('\n') {
x = 0
y++
} else {
if x < e.width {
// Set cell content.
termbox.SetCell(x, y, e.text[i], termbox.ColorDefault, termbox.ColorDefault)
}

// Update x by rune's width.
x = x + runewidth.RuneWidth(e.text[i])
}
}

// Show position details (for debugging).
e.showPositions()

// Flush back buffer!
termbox.Flush()
}

// showPositions shows the positions with other details.
func (e *Editor) showPositions() {
x, y := e.calcCursorXY(e.calcCursor())

// Construct message for debugging.
str := fmt.Sprintf("x=%d, y=%d, cursor=%d, len(text)=%d, x,y=%d,%d", e.x, e.y, e.calcCursor(), len(e.text), x, y)

for i, r := range []rune(str) {
termbox.SetCell(i, e.height-1, r, termbox.ColorDefault, termbox.ColorDefault)
}
}

// MoveCursor updates the cursor position.
func (e *Editor) MoveCursor(x, y int) {
c := e.calcCursor()

if x > 0 {
if c+x <= len(e.text) {
e.x, e.y = e.calcCursorXY(c + x)
}
} else {
if 0 <= c+x {
if e.text[c+x] == rune('\n') {
e.x, e.y = e.calcCursorXY(c + x - 1)
} else {
e.x, e.y = e.calcCursorXY(c + x)
}
}
}
}

// CalcCursor calculates the cursor position.
func (e *Editor) calcCursor() int {
ri := 0
y := 1
x := 0

for y < e.y {
for _, r := range e.text {
ri++
if r == '\n' {
y++
break
}
}
}

for _, r := range e.text[ri:] {
if x >= e.x-runewidth.RuneWidth(r) {
break
}
x += runewidth.RuneWidth(r)
ri++
}

return ri
}

// calcCursorXY calculates cursor position from the index obtained from the content.
func (e *Editor) calcCursorXY(index int) (int, int) {
x := 1
y := 1
for i := 0; i < index; i++ {
if e.text[i] == rune('\n') {
x = 1
y++
} else {
x = x + runewidth.RuneWidth(e.text[i])
}
}
return x, y
}
Loading