-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
119 lines (101 loc) · 2.42 KB
/
table.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
109
110
111
112
113
114
115
116
117
118
119
package go_ping_sweep
import (
"errors"
"fmt"
"strings"
)
// struct to create the table
type Table struct {
Title string //table title
Header []string // table header
Data [][]string // table row * column
tail int // hold the index for the end data row
}
// function to set the table title
func (t *Table) SetTitle(title string) {
t.Title = title
}
// function to set the table header.
func (t *Table) SetHeader(header ...string) {
t.Header = header
}
// function to add the data to the table.
func (t *Table) AddData(data ...string) error {
// if the header is not set return err
if t.Header == nil {
return errors.New("Header is not set")
}
if t.Data == nil {
t.Data = make([][]string, 10, 100)
}
// add the data to the table data
if data != nil {
t.Data[t.tail] = make([]string, len(t.Header))
for i := 0; i < len(data) && i < len(t.Header); i++ {
t.Data[t.tail][i] = data[i]
}
t.tail++
}
return nil
}
//funtion print the data in table format
func (t *Table) CreateTable() {
// print title
if t.Title != "" {
fmt.Println("=========" + t.Title + "==============")
}
// print the header first
fmt.Println("")
for i := 0; i < len(t.Header); i++ {
fmt.Print(t.Header[i] + " | ")
}
fmt.Println("")
for i := 0; i < t.tail; i++ {
for j := 0; j < len(t.Header); j++ {
fmt.Print(t.Data[i][j] + " | ")
}
fmt.Println("")
}
fmt.Println("")
}
// function print the data in table
func (t *Table) PrintTable() {
for i := 0; i < t.tail; i++ {
for j := 0; j < len(t.Header); j++ {
fmt.Print(t.Data[i][j])
}
}
}
// function print the header data
func (t *Table) PrintHeader() {
for i := 0; i < len(t.Header); i++ {
fmt.Println(t.Header[i])
}
}
// return the header indes if exist
// ex if table is:
// TimePing | DataSize | PacketSize | status |
// then getColumnIndes("Status") will be 4 otherwise return -1.
func (t *Table) getCoulumnIndex(header string) int {
if t != nil {
for i := 0; i < t.tail; i++ {
if strings.Compare(t.Header[i], header) == 0 {
return i
}
}
}
return -1
}
// function returns the the column.
func (t *Table) GetColumn(columHeader string) []string {
headerIndex := t.getCoulumnIndex(columHeader)
columnData := make([]string, 10, 100)
if headerIndex != -1 {
// retrieve the all the element from the column.
for i:=0; i < t.tail; i++ {
columnData[i] = t.Data[i][headerIndex]
//fmt.Print(t.Data[i][headerIndex])
}
}
return columnData
}