Skip to content

Commit 596fff2

Browse files
committed
feat: table scheme implemented on proposal at issue #2
1 parent b1e86ac commit 596fff2

File tree

5 files changed

+150
-1
lines changed

5 files changed

+150
-1
lines changed

changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ v1.0.2:
1212
example 'ExampleField_Hide()' fixed
1313
a trailing new line '\n' added to table -> string core function
1414

15+
v1.0.3:
16+
new scheme feature added
17+

example_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,97 @@ func ExampleSTable_FieldCount() {
578578
fmt.Println(table.FieldCount())
579579
// output: 3
580580
}
581+
582+
func ExampleInjectScheme() {
583+
// example struct
584+
type Person struct {
585+
Age int `table:"age"`
586+
Height float64 `table:"height"`
587+
Name string `table:"name"`
588+
Male bool `table:"male"`
589+
}
590+
591+
// lets create a person named 'ruby'
592+
ruby := &Person{
593+
Name: "Ruby Cohen",
594+
Age: 31,
595+
Height: 1.853,
596+
Male: true,
597+
}
598+
599+
sc := &Scheme{
600+
Caption: "Ruby Cohen Personal Info",
601+
GeneralPadding: 2,
602+
FieldOptions: map[string]*Options{
603+
"male": {Hide: true},
604+
"height": {Format: "%0.2f m"},
605+
},
606+
}
607+
608+
t, err := InjectScheme(sc, ruby)
609+
if err != nil {
610+
fmt.Println(err)
611+
return
612+
}
613+
fmt.Println(t)
614+
// output:
615+
// +---------------------------------+
616+
// | Ruby Cohen Personal Info |
617+
// |---------------------------------|
618+
// | age | height | name |
619+
// |-------+----------+--------------|
620+
// | 31 | 1.85 m | Ruby Cohen |
621+
// +-------+----------+--------------+
622+
}
623+
624+
func ExamplePrintWithScheme() {
625+
// example struct
626+
type Person struct {
627+
Age int `table:"age"`
628+
Height float64 `table:"height"`
629+
Name string `table:"name"`
630+
Male bool `table:"male"`
631+
}
632+
633+
// lets create a person named 'ruby'
634+
ruby := &Person{
635+
Name: "Ruby Cohen",
636+
Age: 31,
637+
Height: 1.853,
638+
Male: true,
639+
}
640+
641+
sc := &Scheme{
642+
Caption: "Ruby Cohen Personal Info",
643+
GeneralPadding: 2,
644+
FieldOptions: map[string]*Options{
645+
"male": {Hide: true},
646+
"height": {Format: "%0.2f m"},
647+
},
648+
}
649+
// print the ruby's info
650+
PrintWithScheme(sc, ruby)
651+
652+
// if anything change
653+
ruby.Age = 32
654+
655+
// we can print with same scheme
656+
PrintWithScheme(sc, ruby)
657+
658+
// output:
659+
// +---------------------------------+
660+
// | Ruby Cohen Personal Info |
661+
// |---------------------------------|
662+
// | age | height | name |
663+
// |-------+----------+--------------|
664+
// | 31 | 1.85 m | Ruby Cohen |
665+
// +-------+----------+--------------+
666+
//
667+
// +---------------------------------+
668+
// | Ruby Cohen Personal Info |
669+
// |---------------------------------|
670+
// | age | height | name |
671+
// |-------+----------+--------------|
672+
// | 32 | 1.85 m | Ruby Cohen |
673+
// +-------+----------+--------------+
674+
}

table_scheme.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package stable
2+
3+
import "fmt"
4+
5+
type Scheme struct {
6+
Caption string
7+
BorderStyleName borderStyleName
8+
GeneralPadding int
9+
FieldOptions map[string]*Options
10+
}
11+
12+
func InjectScheme(sc *Scheme, i interface{}) (*STable, error) {
13+
t, err := ToTable(i)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
t.caption = processCaption(sc.Caption)
19+
style, err := processBorderStyle(sc.BorderStyleName)
20+
if err != nil {
21+
return nil, err
22+
}
23+
t.borderStyle = style
24+
t.generalPadding = processPadding(sc.GeneralPadding)
25+
for name, opts := range sc.FieldOptions {
26+
t.GetFieldByName(name).SetOptions(opts)
27+
}
28+
return t, nil
29+
}
30+
31+
func PrintWithScheme(sc *Scheme, i interface{}) error {
32+
t, err := InjectScheme(sc, i)
33+
if err != nil {
34+
return err
35+
}
36+
fmt.Println(t)
37+
return nil
38+
}

table_utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,21 @@ func processCaption(caption string) string {
3232
return caption
3333
}
3434

35+
func processBorderStyle(bsn borderStyleName) (*BorderStyle, error) {
36+
if bsn == 0 {
37+
return DefaultLineStyle, nil
38+
}
39+
bs, err := getStyle(bsn)
40+
if err != nil {
41+
return nil, err
42+
}
43+
return bs, nil
44+
}
45+
3546
func processPadding(padding int) int {
47+
if padding == 0 {
48+
return DefaultGeneralPadding
49+
}
3650
if padding > MaxGeneralPadding {
3751
return MaxGeneralPadding
3852
}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package stable
22

33
const (
44
// Version current version
5-
Version string = "v1.0.2"
5+
Version string = "v1.0.3"
66
)

0 commit comments

Comments
 (0)