diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..450da70 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitlab.com/tyrelsouza/cp437tree + +go 1.16 diff --git a/main.go b/main.go index 72718b5..56b0071 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,23 @@ package cp437tree -import ( - "fmt" - "io" -) - const ( VerticalRight = "├" - DownLeft = "┐" - UpRight = "└" - DownRight = "┌" - Horizontal ="─" - Vertical = "│" + DownLeft = "┐" + UpRight = "└" + DownRight = "┌" + Horizontal = "─" + 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' } \ No newline at end of file diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..375d03e --- /dev/null +++ b/main_test.go @@ -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) +} \ No newline at end of file