Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ While editing, you can utilize the following vim-like keybindings to navigate an
- Delete the current row or column with `dd`, `vd`.
- Copy the current row or column with `yy`, `vy`.
- Paste a copied row or column with `p`.
- Clear the current cell with `x`.
- Copy the current cell with `y.`.

<img src="assets/02.gif" width=500>

<img src="assets/08.gif" width=500>

For direct editing and saving changes to the same file, use:

```sh
Expand Down Expand Up @@ -89,7 +93,9 @@ Press `I` to open the cell in the text editor set as your `$EDITOR`, allowing yo
| `esc`/`ctrl+c` | Normal mode |
| `o`/`vo` | Add row/column |
| `dd`/`vd` | Delete row/column |
| `x` | Clear cell |
| `yy`/`vy` | Copy row/column |
| `y.` | Copy cell |
| `p` | Paste |
| `q` | Quit |
| `?` | Toggle help |
Expand Down
Binary file modified assets/01.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions assets/01.tape
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ Sleep 1s
Type@100ms "mdtt pokemon.md"
Enter
Sleep 500ms
Type "lllhhyypi"
Backspace@100ms 7
Type "lllhhyypxi"
Type@100ms "Raichu"
Ctrl+C
Type "li"
Backspace@100ms 7
Type "lxi"
Type@100ms "Raichu"
Ctrl+C
Sleep 500ms
Expand Down
Binary file added assets/08.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions assets/08.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Set PlaybackSpeed 1.0
Set TypingSpeed 200ms
Set Width 900
Set Height 500

Set Shell zsh
Sleep 500ms
Type@100ms "mdtt pokemon.md"
Enter
Sleep 500ms
Type "lljjj"
Sleep 500ms
Type "xi"
Sleep 500ms
Type "Fire"
Ctrl+C
Sleep 500ms
Type "y."
Sleep 500ms
Type "k"
Sleep 500ms
Type "p"
Sleep 1s
5 changes: 0 additions & 5 deletions assets/pokemon.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@
| Charmeleon | Lizardo | Fire |
| Charizard | Lizardon | Fire |

## Habitat

| NAME | HABITAT |
| --------- | -------------------------------- |
| Pikachu | Electromagnetically-Active Areas |
10 changes: 10 additions & 0 deletions assets/record.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ echo '| NAME | OFFICIAL ROM. | TYPE 1 |
' > pokemon.md

vhs 07.tape -o 07.gif

echo '| NAME | OFFICIAL ROM. | TYPE 1 |
| ---------- | ------------- | --------- |
| Pikachu | Pikachu | Electric |
| Charmander | Hitokage | Electric |
| Charmeleon | Lizardo | |
| Charizard | Lizardon | Electric |
' > pokemon.md

vhs 08.tape -o 08.gif
99 changes: 82 additions & 17 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ type column struct {
alignment string
}

type rreg struct {
type rowRegister struct {
row
}
type creg struct {
type colRegister struct {
column
x, y int
cells []cell
}

type cellRegister struct {
x, y int
cell cell
}

type quitMsg struct{}

type delPrevKeyMsg struct{}
Expand All @@ -97,6 +102,8 @@ type keyMap struct {
addRowCol key.Binding
delRowCol key.Binding
yank key.Binding
yankCell key.Binding
clearCell key.Binding
paste key.Binding
pageUp key.Binding
pageDown key.Binding
Expand Down Expand Up @@ -139,9 +146,17 @@ func defaultKeyMap() keyMap {
key.WithKeys("d"),
key.WithHelp("dd/v+d", "delete row/column"),
),
clearCell: key.NewBinding(
key.WithKeys("x"),
key.WithHelp("x", "clear cell"),
),
yank: key.NewBinding(
key.WithKeys("y"),
key.WithHelp("y", "copy row"),
key.WithHelp("yy/v+y", "copy row/column"),
),
yankCell: key.NewBinding(
key.WithKeys("."),
key.WithHelp("y.", "copy cell"),
),
paste: key.NewBinding(
key.WithKeys("p"),
Expand Down Expand Up @@ -329,9 +344,8 @@ func (m TableModel) Update(msg tea.Msg) (TableModel, tea.Cmd) {
case widthMsg:
m.updateWidth(msg.width)
case delPrevKeyMsg:
m.prevKey = ""
m.setPrevKey("")
case closeEditorMsg:
log.Debug("Close Editor")
cmd := m.updateFocusedCell(msg)
cmds = append(cmds, cmd)
case tea.KeyMsg:
Expand All @@ -358,8 +372,13 @@ func (m TableModel) Update(msg tea.Msg) (TableModel, tea.Cmd) {
}
cmd := m.delete()
cmds = append(cmds, cmd)
case key.Matches(msg, m.keys.clearCell):
m.clearCell()

case key.Matches(msg, m.keys.yank):
m.copy()
case key.Matches(msg, m.keys.yankCell):
m.copyCell()

case key.Matches(msg, m.keys.paste):
m.paste()
Expand Down Expand Up @@ -390,7 +409,7 @@ func (m TableModel) Update(msg tea.Msg) (TableModel, tea.Cmd) {
case key.Matches(msg, m.keys.editor):
return m, m.writeTmpFile()
}
m.prevKey = msg.String()
m.setPrevKey(msg.String())
case openEditorMsg:
return m, m.openEditor(msg.path)
}
Expand All @@ -399,7 +418,7 @@ func (m TableModel) Update(msg tea.Msg) (TableModel, tea.Cmd) {
case widthMsg:
m.updateWidth(msg.width)
case delPrevKeyMsg:
m.prevKey = ""
m.setPrevKey("")
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.normalMode):
Expand All @@ -412,7 +431,7 @@ func (m TableModel) Update(msg tea.Msg) (TableModel, tea.Cmd) {
cmd := m.updateFocusedCell(msg)
cmds = append(cmds, cmd)
}
m.prevKey = msg.String()
m.setPrevKey(msg.String())
}
case HELP:
switch msg := msg.(type) {
Expand All @@ -429,6 +448,19 @@ func (m TableModel) Update(msg tea.Msg) (TableModel, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m TableModel) clearCell() {
if m.mode == HEADER {
m.cols[m.cursor.x].title.setValue("")
} else if m.mode == NORMAL {
m.rows[m.cursor.y][m.cursor.x].setValue("")
}
m.updateViewport()
}

func (m *TableModel) setPrevKey(s string) {
m.prevKey = s
}

func (m *TableModel) writeTmpFile() tea.Cmd {
tmppath := fmt.Sprintf("%s/mdtt_%s", os.TempDir(), uuid.New())
var focusedCell string
Expand Down Expand Up @@ -558,6 +590,28 @@ func (m *TableModel) copy() {
return
}
m.copyColumn()
} else {
m.setPrevKey("y")
}
}

func (m *TableModel) copyCell() {
if m.prevKey != "y" {
return
}

if m.mode == HEADER {
m.register = cellRegister{
x: m.cursor.x,
y: m.cursor.y,
cell: m.cols[m.cursor.x].title,
}
} else if m.mode == NORMAL {
m.register = cellRegister{
x: m.cursor.x,
y: m.cursor.y,
cell: m.rows[m.cursor.y][m.cursor.x],
}
}
}

Expand All @@ -571,40 +625,51 @@ func (m *TableModel) copyColumn() {
cells = append(cells, NewCell(r[m.cursor.x].value()))
}

m.register = creg{column: col, x: m.cursor.x, y: m.cursor.y, cells: cells}
m.register = colRegister{column: col, x: m.cursor.x, y: m.cursor.y, cells: cells}
}

func (m *TableModel) copyRow() {
var row row
for _, cell := range m.rows[m.cursor.y] {
row = append(row, NewCell(cell.value()))
}
m.register = rreg{row: row}
m.register = rowRegister{row: row}
}

func (m *TableModel) paste() {
switch m.register.(type) {
case rreg:
case rowRegister:
if m.register != nil {
m.insertRow(m.cursor.y+1, m.register.(rreg).row)
m.insertRow(m.cursor.y+1, m.register.(rowRegister).row)

var ro row
for _, cell := range m.register.(rreg).row {
for _, cell := range m.register.(rowRegister).row {
ro = append(ro, NewCell(cell.value()))
}
m.register = rreg{row: ro}
m.register = rowRegister{row: ro}
}
m.SetHeight(len(m.rows))
m.moveDown(1)
case creg:
case colRegister:
if m.register != nil {
m.addColumn()
m.cols[m.cursor.x].title = NewCell(m.register.(creg).title.value())
m.cols[m.cursor.x].title = NewCell(m.register.(colRegister).title.value())
for i := range len(m.rows) {
m.rows[i][m.cursor.x] = NewCell(m.register.(creg).cells[i].value())
m.rows[i][m.cursor.x] = NewCell(m.register.(colRegister).cells[i].value())
}
}
m.updateWidth(0)
case cellRegister:
if m.register != nil {
if m.mode == HEADER {
m.cols[m.cursor.x].title = NewCell(m.register.(cellRegister).cell.value())
} else if m.mode == NORMAL {
m.rows[m.cursor.y][m.cursor.x] = NewCell(m.register.(cellRegister).cell.value())
}
}
m.updateWidth(0)
default:
return
}

m.updateViewport()
Expand Down
Loading