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

release 0.1.1 #2

Merged
merged 5 commits into from
Apr 20, 2024
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ dist/
debug.log
.vscode/
test.md
mdtt
/mdtt
*bkup*
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.
Binary file modified assets/02.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/03.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/04.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/05.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/06.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions assets/record.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/zsh


echo '## Pokemon

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

vhs 01.tape -o 01.gif

vhs 02.tape -o 02.gif

vhs 03.tape -o 03.gif

echo '## Pokemon

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

vhs 04.tape -o 04.gif

vhs 05.tape -o 05.gif

echo '## Pokemon

| NAME | OFFICIAL ROM. | TYPE 1 |
| ---------- | ------------- | -------- |
| Pikachu | Pikachu | Electric |
| Charmander | Hitokage | Fire |
| Charmeleon | Lizardo | Fire |
| Charizard | Lizardon | Fire |

## Habitat

| NAME | HABITAT |
| --------- | -------------------------------- |
| Pikachu | Electromagnetically-Active Areas |
' > pokemon.md

vhs 06.tape -o 06.gif
6 changes: 5 additions & 1 deletion cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func NewCell(value string) cell {
return cell{textInput: ta, err: nil}
}

var (
widthPadding = 2
)

func (m cell) update(msg tea.Msg) (cell, tea.Cmd) {
var cmd tea.Cmd

Expand All @@ -39,7 +43,7 @@ func (m cell) update(msg tea.Msg) (cell, tea.Cmd) {
}

m.textInput, cmd = m.textInput.Update(msg)
width := runewidth.StringWidth(m.textInput.Value()) + 2
width := runewidth.StringWidth(m.textInput.Value()) + widthPadding
return m, tea.Batch(cmd, updateWidthCmd(width))
}

Expand Down
Binary file removed mdtt
Binary file not shown.
11 changes: 4 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ var highPriority = 100
type tableModelBuilder struct {
inTable bool
buf *bytes.Buffer
// temprary storage of table rows
// temporary storage of table rows
rows []string
// temprary storage of table columns
// temporary storage of table columns
cols []string
// temprary storage of table alignments
// temporary storage of table alignments
alignment []string
tables []table
}
Expand Down Expand Up @@ -95,10 +95,7 @@ func (b *tableModelBuilder) build() []TableModel {
}

func NewTableModelBuilder() *tableModelBuilder {
var buf []byte
return &tableModelBuilder{
buf: bytes.NewBuffer(buf),
}
return &tableModelBuilder{buf: bytes.NewBuffer(nil)}
}

// RegisterFuncs implements NodeRenderer.RegisterFuncs.
Expand Down
26 changes: 15 additions & 11 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type tableWriter struct {
}

type tableLocator struct {
rang []tableRange
ranges []tableRange
inTable bool
codeFence string
inCodeBlock bool
Expand All @@ -32,22 +32,26 @@ func Write(m Model) {
tw := tableWriter{}
tw.render(m.table)
if m.inplace {
tw.writeFile(m)

if err := tw.writeFile(m); err != nil {
log.Error("Error writing file: ", err)
return
}
} else {
fmt.Print(tw.text)
}
}

func (tw *tableWriter) writeFile(m Model) {
func (tw *tableWriter) writeFile(m Model) error {
fp, err := os.Open(m.fpath)
if err != nil {
log.Fatal(err)
return fmt.Errorf("failed to open file: %w", err)
}
defer fp.Close()
b := tw.replaceTable(fp, m.choose)

os.WriteFile(m.fpath, b, 0644)
if err := os.WriteFile(m.fpath, b, 0644); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}

func (t *tableWriter) render(m TableModel) {
Expand Down Expand Up @@ -107,8 +111,8 @@ func (t *tableWriter) replaceTable(fp *os.File, idx int) []byte {

fp.Seek(0, 0)
b, _ := io.ReadAll(fp)
b = append(b[:tl.rang[idx].Start-1],
append([]byte(t.text), b[min(len(b), tl.rang[idx].End):]...)...)
b = append(b[:tl.ranges[idx].Start-1],
append([]byte(t.text), b[min(len(b), tl.ranges[idx].End):]...)...)
return b
}

Expand Down Expand Up @@ -144,7 +148,7 @@ func (tl *tableLocator) findLocations(fp io.Reader) {
if tl.inTable {
if isBlankLine(line) || isThematicBreak(line) {
tl.inTable = false
tl.rang = append(tl.rang, tableRange{Start: start, End: pos})
tl.ranges = append(tl.ranges, tableRange{Start: start, End: pos})
}
}

Expand All @@ -165,7 +169,7 @@ func (tl *tableLocator) findLocations(fp io.Reader) {
prevlen = len(line) + 1
}
if tl.inTable {
tl.rang = append(tl.rang, tableRange{Start: start, End: pos})
tl.ranges = append(tl.ranges, tableRange{Start: start, End: pos})
}
}

Expand Down
2 changes: 1 addition & 1 deletion printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestFindSegment(t *testing.T) {
`
b := bytes.NewBuffer([]byte(md))
tl.findLocations(b)
got := tl.rang[0]
got := tl.ranges[0]
want := tableRange{
Start: 9,
End: 50,
Expand Down
53 changes: 9 additions & 44 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func NewTableModel(opts ...TableOption) TableModel {
}

m.updateViewport()
m.viewport.Height = m.viewport.Height - 1

return m
}
Expand Down Expand Up @@ -493,7 +494,7 @@ func (m *TableModel) paste() {
}
m.register = rreg{row: ro}
}
m.SetHeight(len(m.rows) + 1)
m.SetHeight(len(m.rows))
m.moveDown(1)
case creg:
if m.register != nil {
Expand Down Expand Up @@ -780,33 +781,17 @@ func (m *TableModel) renderRow(rowID int) string {
}

func (m *TableModel) insertRow(idx int, ro row) {
var rows []row
if len(m.rows) == idx {
rows = append(m.rows, ro)
} else {
rows = append(m.rows[:idx+1], m.rows[idx:]...)
}
rows[idx] = ro
rows := append(m.rows[:idx], append([]row{ro}, m.rows[idx:]...)...)
m.SetRows(rows)
}

func (m *TableModel) deleteRow(idx int) {
var rows []row
if len(m.rows) == idx {
rows = m.rows[:idx-1]
} else {
rows = append(m.rows[:idx], m.rows[idx+1:]...)
}
rows := append(m.rows[:idx], m.rows[idx+1:]...)
m.SetRows(rows)
}

func (m *TableModel) deleteColumn(idx int) {
var cols []column
if len(m.cols) == idx {
cols = m.cols[:idx-1]
} else {
cols = append(m.cols[:idx], m.cols[idx+1:]...)
}
cols := append(m.cols[:idx], m.cols[idx+1:]...)
m.SetColumns(cols)

var rows []row
Expand All @@ -816,36 +801,16 @@ func (m *TableModel) deleteColumn(idx int) {
m.SetRows(rows)
}

func insertCell(r row, idx int, cell cell) row {
var ro row
if len(r) == idx {
ro = append(r, cell)
} else {
ro = append(r[:idx+1], r[idx:]...)
}
ro[idx] = cell
return ro
func insertCell(r row, idx int, cel cell) row {
return append(r[:idx], append([]cell{cel}, r[idx:]...)...)
}

func insertCol(c []column, idx int, col column) []column {
var newCol []column
if len(c) == idx {
newCol = append(c, col)
} else {
newCol = append(c[:idx+1], c[idx:]...)
}
newCol[idx] = col
return newCol
return append(c[:idx], append([]column{col}, c[idx:]...)...)
}

func deleteCell(r row, idx int) row {
var row row
if len(r) == idx {
row = r[:idx-1]
} else {
row = append(r[:idx], r[idx+1:]...)
}
return row
return append(r[:idx], r[idx+1:]...)
}

type delPrevKeyMsg struct{}
Expand Down
Loading