Skip to content

Commit

Permalink
Finish as lib
Browse files Browse the repository at this point in the history
  • Loading branch information
lempiy committed Jul 13, 2019
1 parent ad349e1 commit 88f0f7c
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 219 deletions.
157 changes: 157 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
## ↬ dGraph ↫

#### Draw direct graphs with ascii symbols using Golang and more...

##### Usage

```go
package main

import (
"encoding/json"
"log"
"fmt"
"github.com/lempiy/dgraph"
"github.com/lempiy/dgraph/core"
)

const data = `[
{
"id": "Client",
"next": ["Route53"]
},
{
"id": "Route53",
"next": ["ELB", "CloudFront"]
},
{
"id": "CloudFront",
"next": ["S3"]
},
{
"id": "S3",
"next": []
},
{
"id": "ELB",
"next": ["WebServer1", "WebServer2", "WebServer3", "WebServer4"]
},
{
"id": "WebServer1",
"next": ["LB1"]
},
{
"id": "WebServer2",
"next": ["LB1"]
},
{
"id": "WebServer3",
"next": ["LB2"]
},
{
"id": "WebServer4",
"next": ["LB2"]
},
{
"id": "LB1",
"next": ["AppServer1", "AppServer2"]
},
{
"id": "LB2",
"next": ["AppServer3", "AppServer4"]
},
{
"id": "AppServer1",
"next": ["DBMaster", "DBReplica1"]
},
{
"id": "AppServer2",
"next": ["DBMaster", "DBReplica1"]
},
{
"id": "AppServer3",
"next": ["DBMaster", "DBReplica2"]
},
{
"id": "AppServer4",
"next": ["DBMaster", "DBReplica2"]
},
{
"id": "DBReplica1",
"next": ["DBMaster"]
},
{
"id": "DBReplica2",
"next": ["DBMaster"]
},
{
"id": "DBMaster",
"next": []
}
]`

func main() {
var list []core.NodeInput
err := json.Unmarshal([]byte(data), &list)
if err != nil {
log.Fatal(err)
return
}
canvas, err := dgraph.DrawGraph(list)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("%s\n", canvas)
}
```
**Output**:
```
┌─────────┐ ┌─────────┐ ┌─────┐ ┌─────────────┐ ┌─────┐ ┌─────────────┐ ┌───────────┐
│ Client ├───┤ Route53 ├───────┤ ELB ├───────┤ WebServer1 ├───┤ LB1 ├───┤ AppServer1 ├─────────────────────┤ DBMaster │
└─────────┘ └────┬────┘ └──┬──┘ └─────────────┘ └──┬──┘ └──────┬──────┘ └─────┬─────┘
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ ┌─────────────┐ │ │ ┌─────────────┐ │
│ ├──────────┤ WebServer2 ├──────┤ └──────────┤ DBReplica1 ├─────────┤
│ │ └─────────────┘ │ └──────┬──────┘ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ ┌─────────────┐ │ │
│ │ └──────┤ AppServer2 ├──────────┼────────────────┤
│ │ └──────┬──────┘ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ └─────────────────┘ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ ┌─────────────┐ ┌─────┐ ┌─────────────┐ │
│ ├──────────┤ WebServer3 ├───┤ LB2 ├───┤ AppServer3 ├───────────────────────────┤
│ │ └─────────────┘ └──┬──┘ └──────┬──────┘ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ ┌─────────────┐ │ │ ┌─────────────┐ │
│ └──────────┤ WebServer4 ├──────┤ └──────────┤ DBReplica2 ├─────────┤
│ └─────────────┘ │ └──────┬──────┘ │
│ │ │ │
│ │ │ │
│ │ │ │
│ ┌─────────────┐ ┌─────┐ │ ┌─────────────┐ │ │
└────────┤ CloudFront ├───────┤ S3 │ └──────┤ AppServer4 ├──────────┼────────────────┘
└─────────────┘ └─────┘ └──────┬──────┘ │
│ │
│ │
│ │
│ │
└─────────────────┘
```
If you need to render your graph in other format. You can use `core` package to get low level `core.Matrix` struct.
2 changes: 1 addition & 1 deletion ascii/bitmask.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
EndMarker
)

var AsciiBitmask = map[uint16]rune{
var Bitmask = map[uint16]rune{
RightVector: '─',
LeftVector: '─',
HorizontalBorder: '─',
Expand Down
9 changes: 4 additions & 5 deletions ascii/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,20 @@ func (c *Canvas) drawPixel(x, y int, flag uint16) (err error) {
var newPixel Pixel
switch {
case pixel == nil:
newPixel.Rune = AsciiBitmask[flag]
newPixel.Rune = Bitmask[flag]
newPixel.Flag = flag
newPixel.InitialFlag = flag
case pixel.Flag == 0:
err = fmt.Errorf("Found colision for pixel `%s` with letter `%s` on x: `%d` y: `%d`\n%s\n",
resolve(flag), string([]rune{pixel.Rune}), x, y, c)
return
default:
newPixel.Rune = AsciiBitmask[flag|pixel.InitialFlag]
newPixel.Rune = Bitmask[flag|pixel.InitialFlag]
newPixel.Flag = flag | pixel.InitialFlag
newPixel.InitialFlag = pixel.InitialFlag
if AsciiBitmask[flag|pixel.InitialFlag] == 0 {
if Bitmask[flag|pixel.InitialFlag] == 0 {
err = fmt.Errorf("unexpected symbol intersection new `%s` and old `%s`\n%s\n",
resolve(flag), resolve(pixel.Flag), c)
fmt.Println(flag, pixel.Flag, x, y, "---", flag|pixel.Flag)
return
}
}
Expand All @@ -74,5 +73,5 @@ func (c *Canvas) String() (acc string) {
}

func resolve(flag uint16) string {
return string([]rune{AsciiBitmask[flag]})
return string([]rune{Bitmask[flag]})
}
2 changes: 1 addition & 1 deletion ascii/corner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewCorner(x, y int, orientation core.AnchorOrientation) *Corner {
x: x,
y: y,
orientation: orientation,
rune: AsciiBitmask[getFlagFromOrientation(orientation)],
rune: Bitmask[getFlagFromOrientation(orientation)],
flag: getFlagFromOrientation(orientation),
}
}
Expand Down
46 changes: 46 additions & 0 deletions example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
┌─────────┐ ┌─────────┐ ┌─────┐ ┌─────────────┐ ┌─────┐ ┌─────────────┐ ┌───────────┐
│ Client ├───┤ Route53 ├───────┤ ELB ├───────┤ WebServer1 ├───┤ LB1 ├───┤ AppServer1 ├─────────────────────┤ DBMaster │
└─────────┘ └────┬────┘ └──┬──┘ └─────────────┘ └──┬──┘ └──────┬──────┘ └─────┬─────┘
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ ┌─────────────┐ │ │ ┌─────────────┐ │
│ ├──────────┤ WebServer2 ├──────┤ └──────────┤ DBReplica1 ├─────────┤
│ │ └─────────────┘ │ └──────┬──────┘ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ ┌─────────────┐ │ │
│ │ └──────┤ AppServer2 ├──────────┼────────────────┤
│ │ └──────┬──────┘ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ └─────────────────┘ │
│ │ │
│ │ │
│ │ │
│ │ │
│ │ ┌─────────────┐ ┌─────┐ ┌─────────────┐ │
│ ├──────────┤ WebServer3 ├───┤ LB2 ├───┤ AppServer3 ├───────────────────────────┤
│ │ └─────────────┘ └──┬──┘ └──────┬──────┘ │
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ ┌─────────────┐ │ │ ┌─────────────┐ │
│ └──────────┤ WebServer4 ├──────┤ └──────────┤ DBReplica2 ├─────────┤
│ └─────────────┘ │ └──────┬──────┘ │
│ │ │ │
│ │ │ │
│ │ │ │
│ ┌─────────────┐ ┌─────┐ │ ┌─────────────┐ │ │
└────────┤ CloudFront ├───────┤ S3 │ └──────┤ AppServer4 ├──────────┼────────────────┘
└─────────────┘ └─────┘ └──────┬──────┘ │
│ │
│ │
│ │
│ │
└─────────────────┘


Loading

0 comments on commit 88f0f7c

Please sign in to comment.