Skip to content

Commit

Permalink
read from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
szktkfm committed Apr 6, 2024
1 parent 1c3fd0f commit af64377
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 116 deletions.
14 changes: 1 addition & 13 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mdtt
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-runewidth"
)

Expand All @@ -23,14 +22,10 @@ type Cell struct {
func NewCell(value string) Cell {
ta := textinput.New()
ta.Placeholder = ""
// ta.ShowLineNumbers = false
// ta.SetHeight(2)
ta.Focus()
// ta.CharLimit = 156
ta.SetValue(value)
ta.Prompt = ""
ta.Cursor.Style = lipgloss.NewStyle().
Foreground(lipgloss.Color("205"))
ta.Cursor.Style = cellCursorStyle
return Cell{textInput: ta, err: nil}
}

Expand All @@ -41,20 +36,13 @@ func (m Cell) Init() tea.Cmd {
func (m Cell) Update(msg tea.Msg) (Cell, tea.Cmd) {
var cmd tea.Cmd

// h := m.textInput.LineInfo().Height
// fmt.Println(h)

switch msg := msg.(type) {
// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}

m.textInput, cmd = m.textInput.Update(msg)
// h := strings.Count(m.textInput.Value(), "\n") + 2
// h := m.textInput.LineCount() + 1
// m.textInput.SetHeight(h)
width := runewidth.StringWidth(m.textInput.Value()) + 2
return m, tea.Batch(cmd, updateWidthCmd(width))
}
Expand Down
47 changes: 34 additions & 13 deletions cmd/mdtt/mdtt.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"io"
"os"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/mattn/go-isatty"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
"github.com/szktkfm/mdtt"
Expand All @@ -18,8 +20,8 @@ var (
Date = ""
BuiltBy = ""
rootCmd = &cobra.Command{
Use: "gh dash",
Short: "A gh extension that shows a configurable dashboard of pull requests and issues.",
Use: "mdtt [file]",
Short: "Markdown Table Editor with TUI",
Version: "",
Run: func(cmd *cobra.Command, args []string) {
debug, err := cmd.Flags().GetBool("debug")
Expand All @@ -36,17 +38,7 @@ var (
if inplace && len(args) == 0 {
log.Fatal("no input files")
}

var model mdtt.Model
if len(args) == 0 {
model = mdtt.NewRoot()

} else {
model = mdtt.NewRoot(
mdtt.WithMDFile(args[0]),
mdtt.WithInplace(inplace),
)
}
var model = createModel(args, inplace)

p := tea.NewProgram(
model,
Expand All @@ -62,6 +54,35 @@ var (
}
)

func createModel(args []string, inplace bool) mdtt.Model {

var model mdtt.Model

if !isatty.IsTerminal(os.Stdin.Fd()) {

content, _ := io.ReadAll(os.Stdin)
model = mdtt.NewRoot(
mdtt.WithMarkdown(content),
)

} else if len(args) == 0 {

model = mdtt.NewRoot()

} else {
f, _ := os.Open(args[0])
defer f.Close()
content, _ := io.ReadAll(f)
model = mdtt.NewRoot(
mdtt.WithMarkdown(content),
mdtt.WithInplace(inplace),
mdtt.WithFilePath(args[0]),
)
}

return model
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
Expand Down
13 changes: 0 additions & 13 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ type Element struct {
func (tr *ModelBuilder) NewElement(node ast.Node, source []byte) Element {

switch node.Kind() {
// case ast.KindEmphasis:
// return Element{}

// case ast.KindLink:
// return Element{}
Expand All @@ -40,7 +38,6 @@ func (tr *ModelBuilder) NewElement(node ast.Node, source []byte) Element {
return Element{}

case ast.KindCodeSpan:
// n := node.(*ast.CodeSpan)
return Element{
Renderer: func(b *bytes.Buffer) {
b.WriteString("`")
Expand All @@ -51,7 +48,6 @@ func (tr *ModelBuilder) NewElement(node ast.Node, source []byte) Element {
}

case ast.KindEmphasis:
// n := node.(*ast.CodeSpan)
n := node.(*ast.Emphasis)
return Element{
Renderer: func(b *bytes.Buffer) {
Expand Down Expand Up @@ -112,14 +108,6 @@ func (tr *ModelBuilder) NewElement(node ast.Node, source []byte) Element {
},
}

// return Element{
// Renderer: &BaseElement{
// Token: string(n.Value.Unicode),
// },
// }
// return Element{}

// Unknown case
default:
return Element{
Renderer: func(b *bytes.Buffer) {
Expand All @@ -128,5 +116,4 @@ func (tr *ModelBuilder) NewElement(node ast.Node, source []byte) Element {
},
}
}
// return Element{}
}
27 changes: 6 additions & 21 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ import (
"github.com/charmbracelet/lipgloss"
)

var (
// titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(2)
selectedItemStyle = lipgloss.NewStyle().
PaddingLeft(0).
Foreground(lipgloss.Color("170"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
)

type item string

type SelectMsg struct {
Expand Down Expand Up @@ -47,10 +37,10 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list

str := string(i)

fn := itemStyle.Render
fn := listItemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render(" " +
return listSelectedItemStyle.Render(" " +
strings.Replace(
strings.Replace(strings.Join(s, " "), "\n", "\n ", -1),
"\n ", "\n>", 1))
Expand Down Expand Up @@ -101,8 +91,8 @@ func NewList(opts ...func(*ListModel)) ListModel {
l.SetShowFilter(false)
l.SetShowHelp(false)
l.SetShowTitle(false)
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
l.Styles.PaginationStyle = listPaginationStyle
l.Styles.HelpStyle = listHelpStyle

m := ListModel{list: l}

Expand All @@ -122,22 +112,17 @@ func WithItems(items []list.Item) func(*ListModel) {
}
}

var headerStyle = lipgloss.NewStyle().Bold(true).Padding(0, 2).
Border(lipgloss.NormalBorder(), true, false, true, false).
BorderForeground(lipgloss.Color("240")).
Foreground(lipgloss.Color("249"))

func WithTables(tables []TableModel) func(*ListModel) {
return func(m *ListModel) {
var items []list.Item
for _, t := range tables {
var header []string
for _, c := range t.cols {
header = append(header,
headerStyle.Render(c.Title.Value()))
listHeaderStyle.Render(c.Title.Value()))
}
header = append(header,
headerStyle.Render("…"))
listHeaderStyle.Render("…"))

items = append(items, item(lipgloss.JoinHorizontal(lipgloss.Left, header...)))
}
Expand Down
33 changes: 6 additions & 27 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package mdtt

import (
"bytes"
"io"
"os"

"github.com/charmbracelet/log"
"github.com/muesli/termenv"
"github.com/yuin/goldmark"
east "github.com/yuin/goldmark-emoji/ast"
"github.com/yuin/goldmark/ast"
Expand All @@ -32,17 +29,13 @@ type Table struct {
cols []string
}

func parse(file string) []TableModel {
f, _ := os.Open(file)
defer f.Close()
s, _ := io.ReadAll(f)

func parse(s []byte) []TableModel {
md := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithExtensions(extension.Table),
)

builder := NewModelBuilder(Options{})
builder := NewModelBuilder()

md.SetRenderer(
renderer.NewRenderer(
Expand All @@ -52,17 +45,11 @@ func parse(file string) []TableModel {
),
)

// Convert markdown to HTML
var buf bytes.Buffer
md.Convert(s, &buf)

log.Debug("rows", "rows", builder.rows)
log.Debug("cols", "cols", builder.cols)
var _buf bytes.Buffer
md.Convert(s, &_buf)

// table.WithHeight(7),
m := builder.build()
return builder.build()

return m
}

func (b *ModelBuilder) build() []TableModel {
Expand Down Expand Up @@ -100,16 +87,8 @@ func (b *ModelBuilder) build() []TableModel {
return models
}

// Options is used to configure an ANSIRenderer.
type Options struct {
BaseURL string
WordWrap int
PreserveNewLines bool
ColorProfile termenv.Profile
}

// NewModelBuilder returns a new ANSIRenderer with style and options set.
func NewModelBuilder(options Options) *ModelBuilder {
func NewModelBuilder() *ModelBuilder {
var buf []byte
return &ModelBuilder{
buf: bytes.NewBuffer(buf),
Expand Down
7 changes: 6 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mdtt

import (
"io"
"os"
"testing"
)

Expand Down Expand Up @@ -30,7 +32,10 @@ func TestParse(t *testing.T) {
WithNaiveRows(rows2),
)

got := parse("testdata/01.md")
f, _ := os.Open("testdata/01.md")
defer f.Close()
s, _ := io.ReadAll(f)
got := parse(s)

if !isEqualTables(want1, got[0]) {
t.Error("Table value is mismatch")
Expand Down
18 changes: 11 additions & 7 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,24 @@ func TestReplaceTable(t *testing.T) {

func testUtilReplaceTable(src, wnt string, idx int) ([]byte, []byte) {
tw := TableWriter{}
fp, _ := os.Open(src)
defer fp.Close()
fpSrc, _ := os.Open(src)
defer fpSrc.Close()

fp_, _ := os.Open(wnt)
defer fp_.Close()
md, _ := io.ReadAll(fp_)
m := NewRoot(
WithMDFile(wnt),
WithMarkdown(md),
WithFilePath(wnt),
)
m.table = m.tables[idx]
m.choose = idx

tw.render(m.table)
got := tw.replaceTable(fp, idx)
got := tw.replaceTable(fpSrc, idx)

fp2, _ := os.Open(wnt)
defer fp2.Close()
want, _ := io.ReadAll(fp2)
fpWnt, _ := os.Open(wnt)
defer fpWnt.Close()
want, _ := io.ReadAll(fpWnt)
return got, want
}
Loading

0 comments on commit af64377

Please sign in to comment.