-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
54 lines (47 loc) · 1.15 KB
/
format.go
File metadata and controls
54 lines (47 loc) · 1.15 KB
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
package kmap
import "fmt"
// Strings used to format output
var formatStrings = map[int]string{
2: ` y
---------
| %c | %c |
---------
x | %c | %c |
---------`,
3: ` y
-----------------
| %c | %c | %c | %c |
-----------------
x | %c | %c | %c | %c |
-----------------
z`,
4: ` y
-----------------
| %c | %c | %c | %c |
-----------------
| %c | %c | %c | %c |
----------------- x
| %c | %c | %c | %c |
w -----------------
| %c | %c | %c | %c |
-----------------
z`,
}
// Format creates a formatted string that can be outputted containing the data of the Kmap.
func (kmap *Kmap) Format() string {
// Create a flattened version of the k-map that uses '1', 'X' and '0' values instead of &true, &false and nil
var flat []interface{} // interface is used to allow tuple argument in fmt.Sprintf call
for _, v := range kmap.Values {
for _, v := range v {
if v == nil {
flat = append(flat, '0')
} else if *v {
flat = append(flat, '1')
} else {
flat = append(flat, 'X')
}
}
}
// Format and return the output
return fmt.Sprintf(formatStrings[kmap.Size], flat...)
}