-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
mdr.go
214 lines (176 loc) · 4.13 KB
/
mdr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/MichaelMure/go-term-markdown"
"github.com/awesome-gocui/gocui"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
)
const padding = 4
func main() {
if len(os.Args) >= 2 && (os.Args[1] == "version" || os.Args[1] == "--version") {
printVersion()
return
}
var content []byte
switch len(os.Args) {
case 1:
if isatty.IsTerminal(os.Stdin.Fd()) {
exitError(fmt.Errorf("usage: %s <file.md>", os.Args[0]))
}
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
exitError(errors.Wrap(err, "error while reading STDIN"))
}
content = data
case 2:
data, err := ioutil.ReadFile(os.Args[1])
if err != nil {
exitError(errors.Wrap(err, "error while reading file"))
}
err = os.Chdir(path.Dir(os.Args[1]))
if err != nil {
exitError(err)
}
content = data
default:
exitError(fmt.Errorf("only one file is supported"))
}
g, err := gocui.NewGui(gocui.OutputNormal, false)
if err != nil {
exitError(errors.Wrap(err, "error starting the interactive UI"))
}
defer g.Close()
ui, err := newUi(g)
if err != nil {
exitError(err)
}
ui.setContent(content)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
exitError(err)
}
}
func exitError(err error) {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
const renderView = "render"
type ui struct {
keybindings []keybinding
raw string
// current width of the view
width int
XOffset int
YOffset int
// number of lines in the rendered markdown
lines int
}
func newUi(g *gocui.Gui) (*ui, error) {
result := &ui{
width: -1,
}
g.SetManagerFunc(result.layout)
result.keybindings = []keybinding{
{"", gocui.KeyCtrlC, gocui.ModNone, result.quit},
{renderView, 'q', gocui.ModNone, result.quit},
{renderView, 'k', gocui.ModNone, result.up},
{renderView, gocui.KeyCtrlP, gocui.ModNone, result.up},
{renderView, gocui.KeyArrowUp, gocui.ModNone, result.up},
{renderView, 'j', gocui.ModNone, result.down},
{renderView, gocui.KeyCtrlN, gocui.ModNone, result.down},
{renderView, gocui.KeyArrowDown, gocui.ModNone, result.down},
{renderView, gocui.KeyPgup, gocui.ModNone, result.pageUp},
{renderView, gocui.KeyPgdn, gocui.ModNone, result.pageDown},
{renderView, gocui.KeySpace, gocui.ModNone, result.pageDown},
}
for _, kb := range result.keybindings {
err := kb.Register(g)
if err != nil {
return nil, err
}
}
return result, nil
}
func (ui *ui) setContent(content []byte) {
ui.raw = string(content)
ui.width = -1
}
func (ui *ui) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
v, err := g.SetView(renderView, ui.XOffset, -ui.YOffset, maxX, maxY, 0)
if err != nil {
if !gocui.IsUnknownView(err) {
return err
}
v.Frame = false
v.Wrap = false
}
if len(ui.raw) > 0 && ui.width != maxX {
ui.width = maxX
v.Clear()
_, _ = v.Write(ui.render(g))
}
_, err = g.SetCurrentView(renderView)
if err != nil {
return err
}
return nil
}
func (ui *ui) render(g *gocui.Gui) []byte {
maxX, _ := g.Size()
opts := []markdown.Options{
// needed when going through gocui
markdown.WithImageDithering(markdown.DitheringWithBlocks),
}
rendered := markdown.Render(ui.raw, maxX-1-padding, padding, opts...)
ui.lines = 0
for _, b := range rendered {
if b == '\n' {
ui.lines++
}
}
return rendered
}
func (ui *ui) quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func (ui *ui) up(g *gocui.Gui, v *gocui.View) error {
ui.YOffset -= 1
ui.YOffset = max(ui.YOffset, 0)
return nil
}
func (ui *ui) down(g *gocui.Gui, v *gocui.View) error {
_, maxY := g.Size()
ui.YOffset += 1
ui.YOffset = min(ui.YOffset, ui.lines-maxY+1)
ui.YOffset = max(ui.YOffset, 0)
return nil
}
func (ui *ui) pageUp(g *gocui.Gui, v *gocui.View) error {
_, maxY := g.Size()
ui.YOffset -= maxY / 2
ui.YOffset = max(ui.YOffset, 0)
return nil
}
func (ui *ui) pageDown(g *gocui.Gui, v *gocui.View) error {
_, maxY := g.Size()
ui.YOffset += maxY / 2
ui.YOffset = min(ui.YOffset, ui.lines-maxY+1)
ui.YOffset = max(ui.YOffset, 0)
return nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}