Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aca committed Apr 28, 2020
1 parent bf98883 commit dac2df1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 79 deletions.
19 changes: 19 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
archives:
- replacements:
darwin: Darwin
linux: Linux
# windows: Windows
386: i386
amd64: x86_64
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
47 changes: 8 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,26 @@ tdraw
Inspired by [Explaining Code using ASCII Art](https://blog.regehr.org/archives/1653).<br/>
Supports box / line drawing, text input and eraser.

![tdraw](./tdraw.gif)

#### Install
```
go get -u github.com/aca/tdraw
```
or download from https://github.com/aca/tdraw/releases

#### Usage
```
tdraw > output
tdraw > draw.txt
```

```
tdraw has 3 mode.
ESC: Box Mode(default)
L: Draw Line
I: Text
ESC: Box(+Undirected line, default)
l: Directed line
t: Text
---
MouseR: Eraser
CTRL-C/CTRL-D: exit
```

```
┌──────────────┐
│ │ BOX
└──────────────┘
<--------------- LINES
--------------->
TEXT
---- --- ----> ERASE WITH MouseR
```

```
+----------+
v |
┌─────────────────┐ |
│ STATE A │ |
└─────────────────┘ |
| |
v |
┌─────────────────┐ |
│ │ | ┌───────────────┐
│ STATE B │ ---------> │ FAIL │
│ │ | └───────────────┘
└─────────────────┘ |
| |
v |
┌─────────────────┐ |
│ │ |
│ STATE C │ |
│ │ ---+
└─────────────────┘
```
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFDnH08=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
Expand Down
Binary file added tdraw.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 35 additions & 40 deletions tdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const (
MODE_BOX = "BOX"
MODE_LINE = "LINE"
MODE_INSERT = "INSERT"
MODE_TEXT = "TEXT"
MODE_ERASOR = "ERASE"
)

Expand All @@ -36,13 +36,6 @@ func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
}

func drawLine(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) {
// if y2 < y1 {
// y1, y2 = y2, y1
// }
// if x2 < x1 {
// x1, x2 = x2, x1
// }

// do not draw point
if x1 == x2 && y1 == y2 {
return
Expand Down Expand Up @@ -117,16 +110,29 @@ func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) {
return
}

for col := x1; col <= x2; col++ {
for col := x1 + 1; col < x2; col++ {
// RuneHLine = '─'
s.SetContent(col, y1, tcell.RuneHLine, nil, tcell.StyleDefault)
s.SetContent(col, y2, tcell.RuneHLine, nil, tcell.StyleDefault)
}
for row := y1; row <= y2; row++ {
for row := y1 + 1; row < y2; row++ {
// RuneVLine = '│'
s.SetContent(x1, row, tcell.RuneVLine, nil, tcell.StyleDefault)
s.SetContent(x2, row, tcell.RuneVLine, nil, tcell.StyleDefault)
}

if y1 == y2 {
s.SetContent(x1, y1, tcell.RuneHLine, nil, tcell.StyleDefault)
s.SetContent(x2, y2, tcell.RuneHLine, nil, tcell.StyleDefault)
return
}

if x1 == x2 {
s.SetContent(x1, y1, tcell.RuneVLine, nil, tcell.StyleDefault)
s.SetContent(x2, y2, tcell.RuneVLine, nil, tcell.StyleDefault)
return
}

if y1 != y2 && x1 != x2 {
// Only add corners if we need to
s.SetContent(x1, y1, tcell.RuneULCorner, nil, tcell.StyleDefault)
Expand Down Expand Up @@ -177,8 +183,6 @@ func main() {
s.EnableMouse()
s.Clear()

white := tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorBlack)

// mouse
mx, my := -1, -1
ox, oy := -1, -1
Expand All @@ -192,8 +196,8 @@ func main() {
// imodeLastC := ' '

for {
r, _, _, _ := s.GetContent(mx, my)
emitStr(s, 1, 1, white, fmt.Sprintf("[%s][%v,%v][%d]"+" ", mode, mx, my, r))
// r, _, _, _ := s.GetContent(mx, my)
emitStr(s, 1, 1, defStyle, fmt.Sprintf("[%3v,%3v] %-7s | t:text / l:line / esc:box / MouseR: erase", mx, my, mode))

s.Show()
ev := s.PollEvent()
Expand All @@ -210,35 +214,22 @@ func main() {

switch ev := ev.(type) {
case *tcell.EventResize:
// s.Sync()
// s.SetContent(w-1, h-1, 'R', nil, st)
s.Sync()
case *tcell.EventKey:

if ev.Key() == tcell.KeyCtrlC || ev.Key() == tcell.KeyCtrlD {
s.Sync()
// s.Fini()
sizeX, sizeY := s.Size()

// arr := make([][]rune, sizeX)
// for i := range arr {
// arr[i] = make([]rune, sizeY)
// }
arr := make([]string, sizeY)

for y := 2; y < sizeY; y++ {
for x := 0; x < sizeX; x++ {
c, _, _, _ := s.GetContent(x, y)
// log.Println(x, y, c)
arr[y] = arr[y] + string(c)
}
}
s.Clear()
s.Sync()

{
n := 0
for y := 0; y < len(arr); y++ {
// if strings.TrimSpace(arr[y]) == "" || strings.TrimSpace(arr[y]) == "\n" {
if strings.TrimSpace(arr[y]) == "" {
n++
} else {
Expand All @@ -252,7 +243,6 @@ func main() {
n := 0
for y := len(arr) - 1; y >= 0; y-- {
if strings.TrimSpace(arr[y]) == "" {
// if strings.TrimSpace(arr[y]) == "" || strings.TrimSpace(arr[y]) == "\n" {
n++
} else {
break
Expand All @@ -261,6 +251,7 @@ func main() {
arr = arr[0 : len(arr)-n]
}

s.Clear()
for y := 0; y < len(arr); y++ {
fmt.Println(arr[y])
}
Expand All @@ -272,11 +263,11 @@ func main() {
switch ev.Key() {
default:
switch ev.Rune() {
case 'i':
mode = MODE_INSERT
case 't':
mode = MODE_TEXT
imodeCurX, imodeCurY = mx, my
imodeStartX, _ = mx, my
s.SetContent(imodeCurX, imodeCurY, '_', nil, st)
s.SetContent(imodeCurX, imodeCurY, '_', nil, st.Blink(true))
case 'l':
mode = MODE_LINE
}
Expand All @@ -285,33 +276,37 @@ func main() {
switch ev.Key() {
case tcell.KeyEscape:
mode = MODE_BOX
default:
switch ev.Rune() {
case 't', 'i':
mode = MODE_TEXT
imodeCurX, imodeCurY = mx, my
imodeStartX, _ = mx, my
s.SetContent(imodeCurX, imodeCurY, '_', nil, st.Blink(true))
}
}

case MODE_INSERT:
case MODE_TEXT:
switch ev.Key() {
case tcell.KeyEscape:
mode = MODE_BOX
s.SetContent(imodeCurX, imodeCurY, ' ', nil, st)
// s.Fini()
// os.Exit(0)
case tcell.KeyEnter:
// mode = MODE_NORMAL
s.SetContent(imodeCurX, imodeCurY, ' ', nil, st)
imodeCurX = imodeStartX
imodeCurY = imodeCurY + 1
case tcell.KeyDEL:
if imodeCurX > imodeStartX {
s.SetContent(imodeCurX, imodeCurY, ' ', nil, st)
s.SetContent(imodeCurX-1, imodeCurY, '_', nil, st)
s.SetContent(imodeCurX-1, imodeCurY, '_', nil, st.Blink(true))
imodeCurX -= 1
} else {
s.SetContent(imodeCurX, imodeCurY, '_', nil, st)
}
default:
s.SetContent(imodeCurX, imodeCurY, ev.Rune(), nil, st)
// imodeLastC, _, _, _ = s.GetContent(imodeCurX, imodeCurY)
s.SetContent(imodeCurX, imodeCurY, ev.Rune(), nil, st.Blink(true))
imodeCurX += 1
s.SetContent(imodeCurX, imodeCurY, '_', nil, st)
s.SetContent(imodeCurX, imodeCurY, '_', nil, st.Blink(true))
}
}
case *tcell.EventMouse:
Expand Down

0 comments on commit dac2df1

Please sign in to comment.