define tree structure

This commit is contained in:
Tyrel Souza 2021-07-09 16:26:12 -04:00
parent 69f271949b
commit a53fc302ee
3 changed files with 138 additions and 60 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitlab.com/tyrelsouza/cp437tree
go 1.16

113
main.go
View File

@ -1,10 +1,5 @@
package cp437tree
import (
"fmt"
"io"
)
const (
VerticalRight = "├"
DownLeft = "┐"
@ -14,6 +9,15 @@ const (
Vertical = "│"
)
type Tree struct {
Text string
Children []Tree
}
func (t *Tree) String() string {
return t.Text
}
/*
// HeaderTop
// HeaderMid
@ -23,55 +27,54 @@ const (
//ItemEndBottom
*/
type Node struct {
Text string
Children *[]Node
}
//
//func (o *Request) ToTreeGroup(w io.Writer, position rune) {
// middle := "├──┐" // default middle branch
// midRow := "│ ├── "
// botRow := "│ └── "
// topBranch := "┌──┐"
// endBranch := "└──┐"
// veryBottomRow := " └── "
//
// rowChars := ""
// if position == 't' {
// branch = topBranch
// }
// if position == 'b' {
// branch = endBranch
// }
//
// // platform name header
// rowChars = midRow
// // preparing the children variable so this whole thing can be extracted later.
// children := o.Versions
// childCount := len(children) - 1
// for idx, v := range children {
// if idx == childCount {
// rowChars = botRow
// if position == 'b' {
// rowChars = veryBottomRow
// }
// }
// _, _ = fmt.Fprintf(w, "\n%s%s", rowChars, v)
// }
//
// // new line to end the row
// _, _ = fmt.Fprintf(w, "\n")
//}
//
//func idToPosition(id, count int) rune {
// // Returns 't' for top, 'b' for bottom, and 'm', for middle by comparing the idx to the count of rows.
// // Does not account for if count = 1
// if id == 0 {
// return 't'
// }
// if id == count-1 {
// return 'b'
// }
// return 'm'
//}
//
func main() {
func (o *Request) ToTreeGroup(w io.Writer, position rune) {
middle := "├──┐" // default middle branch
midRow := "│ ├── "
botRow := "│ └── "
topBranch := "┌──┐"
endBranch := "└──┐"
veryBottomRow := " └── "
rowChars := ""
if position == 't' {
branch = topBranch
}
if position == 'b' {
branch = endBranch
}
// platform name header
rowChars = midRow
// preparing the children variable so this whole thing can be extracted later.
children := o.Versions
childCount := len(children) - 1
for idx, v := range children {
if idx == childCount {
rowChars = botRow
if position == 'b' {
rowChars = veryBottomRow
}
}
_, _ = fmt.Fprintf(w, "\n%s%s", rowChars, v)
}
// new line to end the row
_, _ = fmt.Fprintf(w, "\n")
}
func idToPosition(id, count int) rune {
// Returns 't' for top, 'b' for bottom, and 'm', for middle by comparing the idx to the count of rows.
// Does not account for if count = 1
if id == 0 {
return 't'
}
if id == count-1 {
return 'b'
}
return 'm'
}

72
main_test.go Normal file
View File

@ -0,0 +1,72 @@
package cp437tree
import (
"testing"
)
func Test_Main(t *testing.T) {
tree := Tree{
Text: "1",
Children: []Tree{
{
Text: "2",
Children: []Tree{
{
Text: "3",
},
{
Text: "4",
},
{
Text: "5",
},
{
Text: "6",
},
{
Text: "7",
Children: []Tree{
{
Text: "8",
},
},
},
},
},
{
Text: "9",
},
{
Text: "10",
Children: []Tree{
{
Text: "11",
Children: []Tree{
{
Text: "12",
Children: []Tree{
{
Text: "13",
Children: []Tree{
{
Text: "14",
Children: []Tree{
{
Text: "15",
},
},
},
},
},
},
},
},
},
},
},
},
}
t.Error(tree)
}