forked from kfultz07/go-dataframe
-
Notifications
You must be signed in to change notification settings - Fork 4
/
console.go
108 lines (87 loc) · 1.86 KB
/
console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package dataframe
import (
"fmt"
"strconv"
"time"
)
func calculateSpaces(val string, maxColumnWidth int) string {
valLength := len(val)
if len(val)%2 != 0 {
val += " "
}
if len(val) == maxColumnWidth {
return "|" + val + "| ---> " + strconv.Itoa(valLength)
}
for len(val) < maxColumnWidth {
val = " " + val + " "
}
return "|" + val + "| ---> " + strconv.Itoa(valLength)
}
func calculateMaxColumnWidth(headers []string) int {
maxWidth := len(headers[0])
for _, each := range headers {
if len(each) > maxWidth {
maxWidth = len(each)
}
}
return maxWidth
}
// Dynamically generate the column headers for the table.
func generateTableColumns(headers []string, maxColumnWidth int) string {
var head string
var columnCount int
for _, h := range headers {
val := calculateSpaces(h, maxColumnWidth)
head += val + "\n"
columnCount++
}
head = "\n" + head
border := " "
for i := 0; i < maxColumnWidth; i++ {
border += "-"
}
head = "Column Count: " + strconv.Itoa(columnCount) + "\n" + border + head + border
return head
}
// Method to print all columns in a viewable table within the terminal.
func (frame DataFrame) ViewColumns() {
var columns []string
// Add columns in order from map.
for i := 0; i < len(frame.Headers); i++ {
for k, v := range frame.Headers {
if v == i {
columns = append(columns, k)
}
}
}
maxColumnWidth := calculateMaxColumnWidth(columns)
head := generateTableColumns(columns, maxColumnWidth)
fmt.Println(head)
}
func loading(quit <-chan bool) {
char := []string{
"| L",
"/ LO",
"- LOA",
"\\ LOAD",
"| LOADI",
"/ LOADIN",
"- LOADING",
"\\ LOADING.",
"| LOADING..",
"/ LOADING...",
"- ",
}
for {
select {
case <-quit:
fmt.Printf("\r")
return
default:
for _, c := range char {
fmt.Printf("\r%s", c)
time.Sleep(time.Millisecond * 75)
}
}
}
}