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

117
main.go
View File

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