Skip to content

Commit

Permalink
todo: test
Browse files Browse the repository at this point in the history
  • Loading branch information
szktkfm committed Mar 23, 2024
1 parent 31db3cb commit d684da7
Show file tree
Hide file tree
Showing 5 changed files with 917 additions and 44 deletions.
3 changes: 2 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func (b *ModelBuilder) build() []TableModel {

var cols []Column
for i := range len(t.cols) {
cols = append(cols, Column{Title: NewCell(t.cols[i]), Width: 20})
//todo: widthおかしい
cols = append(cols, Column{Title: NewCell(t.cols[i]), Width: 35})
}

t := NewTable(
Expand Down
62 changes: 32 additions & 30 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ import (
"github.com/charmbracelet/log"
)

func Write(m Model) {
tw := TableWriter{}
tw.write(m)
}

type TableWriter struct {
// segをフィールドに持つ必要ないのでは
segs []TableSegment

Check failure on line 21 in printer.go

View workflow job for this annotation

GitHub Actions / lint

field `segs` is unused (unused)
seg TableSegment
text string
}
type TableSegment struct {
Start int
End int
}

func (t *TableWriter) render(m TableModel) {
var sb strings.Builder
var width int
Expand Down Expand Up @@ -39,13 +55,7 @@ func (t *TableWriter) render(m TableModel) {
t.text = sb.String()
}

func Write(m Model) {
tw := TableWriter{}
tw.write(m)
}

func (t *TableWriter) write(m Model) {

t.render(m.table)

if m.inplace {
Expand All @@ -54,11 +64,12 @@ func (t *TableWriter) write(m Model) {
log.Fatal(err)
}
defer fp.Close()

t.findSegment(fp)
fp.Seek(0, 0)
b, _ := io.ReadAll(fp)
b = append(b[:t.seg.start-1],
append([]byte(t.text), b[t.seg.end:]...)...)
b = append(b[:t.seg.Start-1],
append([]byte(t.text), b[t.seg.End:]...)...)

os.WriteFile(m.fpath, b, 0644)
} else {
Expand All @@ -70,11 +81,11 @@ func (t *TableWriter) write(m Model) {
// ^\s*\|?\s*\-+

// TODO: delimeterの左寄せとか
var tableDelimLeft = regexp.MustCompile(`^\s*\:\-+\s*$`)
var tableDelimRight = regexp.MustCompile(`^\s*\-+\:\s*$`)
var tableDelimCenter = regexp.MustCompile(`^\s*\:\-+\:\s*$`)
var tableDelimNone = regexp.MustCompile(`^\s*\-+\s*$`)
var tableDelim = regexp.MustCompile(`^\s*\|?\s*\-+`)
// var tableDelimLeft = regexp.MustCompile(`^\s*\:\-+\s*$`)
// var tableDelimRight = regexp.MustCompile(`^\s*\-+\:\s*$`)
// var tableDelimCenter = regexp.MustCompile(`^\s*\:\-+\:\s*$`)
// var tableDelimNone = regexp.MustCompile(`^\s*\-+\s*$`)
var tableDelim = regexp.MustCompile(`^\s*\|?\s*\-+\s*\|?\s*`)

func (t *TableWriter) findSegment(fp io.Reader) {
// fmt.Println([]byte(fmt.Sprint(fp)))
Expand All @@ -99,13 +110,12 @@ func (t *TableWriter) findSegment(fp io.Reader) {
}

pos += len(l) + 1

if tableDelimLeft.MatchString(l) ||
tableDelimRight.MatchString(l) ||
tableDelimCenter.MatchString(l) ||
tableDelimNone.MatchString(l) ||
tableDelim.MatchString(l) {
if tableDelim.MatchString(l) {
// header check
log.Debug("line", l)
if prevline == "" {
continue
}
if len(strings.Split(trimPipe(prevline), "|")) <= len(strings.Split(trimPipe(l), "|")) {
inTable = true
start = pos - len(l) - prevlen
Expand All @@ -122,7 +132,9 @@ func (t *TableWriter) findSegment(fp io.Reader) {
}

// TODO: listで返す
t.seg = TableSegment{start: start, end: end}
log.Debug(t)
t.seg = TableSegment{Start: start, End: end}
log.Debugf("start: %d, end: %d", start, end)
}

func trimPipe(l string) string {
Expand All @@ -134,13 +146,3 @@ func trimPipe(l string) string {
}
return l
}

type TableWriter struct {
segs []TableSegment
seg TableSegment
text string
}
type TableSegment struct {
start int
end int
}
4 changes: 2 additions & 2 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestFindSegment(t *testing.T) {
tw.findSegment(b)
got := tw.seg
want := TableSegment{
start: 9,
end: 50,
Start: 9,
End: 50,
}

if diff := cmp.Diff(want, got); diff != "" {
Expand Down
Loading

0 comments on commit d684da7

Please sign in to comment.