ソースを参照

Get latest revision "github.com/bndr/gotabulate"

xormplus 8 年 前
コミット
9412f3ace5
1 ファイル変更38 行追加1 行削除
  1. 38 1
      vendor/github.com/bndr/gotabulate/tabulate.go

+ 38 - 1
vendor/github.com/bndr/gotabulate/tabulate.go

@@ -14,6 +14,7 @@ type TableFormat struct {
 	LineBottom      Line
 	LineBottom      Line
 	HeaderRow       Row
 	HeaderRow       Row
 	DataRow         Row
 	DataRow         Row
+	TitleRow        Row
 	Padding         int
 	Padding         int
 	HeaderHide      bool
 	HeaderHide      bool
 	FitScreen       bool
 	FitScreen       bool
@@ -44,11 +45,13 @@ var TableFormats = map[string]TableFormat{
 		LineBottom:      Line{"", "-", "  ", ""},
 		LineBottom:      Line{"", "-", "  ", ""},
 		HeaderRow:       Row{"", "  ", ""},
 		HeaderRow:       Row{"", "  ", ""},
 		DataRow:         Row{"", "  ", ""},
 		DataRow:         Row{"", "  ", ""},
+		TitleRow:        Row{"", "  ", ""},
 		Padding:         1,
 		Padding:         1,
 	},
 	},
 	"plain": TableFormat{
 	"plain": TableFormat{
 		HeaderRow: Row{"", "  ", ""},
 		HeaderRow: Row{"", "  ", ""},
 		DataRow:   Row{"", "  ", ""},
 		DataRow:   Row{"", "  ", ""},
+		TitleRow:  Row{"", "  ", ""},
 		Padding:   1,
 		Padding:   1,
 	},
 	},
 	"grid": TableFormat{
 	"grid": TableFormat{
@@ -58,6 +61,7 @@ var TableFormats = map[string]TableFormat{
 		LineBottom:      Line{"+", "-", "+", "+"},
 		LineBottom:      Line{"+", "-", "+", "+"},
 		HeaderRow:       Row{"|", "|", "|"},
 		HeaderRow:       Row{"|", "|", "|"},
 		DataRow:         Row{"|", "|", "|"},
 		DataRow:         Row{"|", "|", "|"},
+		TitleRow:        Row{"|", " ", "|"},
 		Padding:         1,
 		Padding:         1,
 	},
 	},
 }
 }
@@ -68,6 +72,8 @@ var MIN_PADDING = 5
 // Main Tabulate structure
 // Main Tabulate structure
 type Tabulate struct {
 type Tabulate struct {
 	Data          []*TabulateRow
 	Data          []*TabulateRow
+	Title         string
+	TitleAlign    string
 	Headers       []string
 	Headers       []string
 	FloatFormat   byte
 	FloatFormat   byte
 	TableFormat   TableFormat
 	TableFormat   TableFormat
@@ -251,7 +257,24 @@ func (t *Tabulate) Render(format ...interface{}) string {
 		padded_widths[i] = cols[i] + MIN_PADDING*t.TableFormat.Padding
 		padded_widths[i] = cols[i] + MIN_PADDING*t.TableFormat.Padding
 	}
 	}
 
 
+	// Calculate total width of the table
+	totalWidth := len(t.TableFormat.DataRow.sep) * (len(cols) - 1) // Include all but the final separator
+	for _, w := range padded_widths {
+		totalWidth += w
+	}
+
 	// Start appending lines
 	// Start appending lines
+	if len(t.Title) > 0 {
+		if !inSlice("aboveTitle", t.HideLines) {
+			lines = append(lines, t.buildLine(padded_widths, cols, t.TableFormat.LineTop))
+		}
+		savedAlign := t.Align
+		if len(t.TitleAlign) > 0 {
+			t.SetAlign(t.TitleAlign) // Temporary replace alignment with the title alignment
+		}
+		lines = append(lines, t.buildRow([]string{t.Title}, []int{totalWidth}, nil, t.TableFormat.TitleRow))
+		t.SetAlign(savedAlign)
+	}
 
 
 	// Append top line if not hidden
 	// Append top line if not hidden
 	if !inSlice("top", t.HideLines) {
 	if !inSlice("top", t.HideLines) {
@@ -270,7 +293,7 @@ func (t *Tabulate) Render(format ...interface{}) string {
 	for index, element := range t.Data {
 	for index, element := range t.Data {
 		lines = append(lines, t.buildRow(t.padRow(element.Elements, t.TableFormat.Padding), padded_widths, cols, t.TableFormat.DataRow))
 		lines = append(lines, t.buildRow(t.padRow(element.Elements, t.TableFormat.Padding), padded_widths, cols, t.TableFormat.DataRow))
 		if index < len(t.Data)-1 {
 		if index < len(t.Data)-1 {
-			if element.Continuos != true {
+			if element.Continuos != true && !inSlice("betweenLine", t.HideLines) {
 				lines = append(lines, t.buildLine(padded_widths, cols, t.TableFormat.LineBetweenRows))
 				lines = append(lines, t.buildLine(padded_widths, cols, t.TableFormat.LineBetweenRows))
 			}
 			}
 		}
 		}
@@ -312,6 +335,17 @@ func (t *Tabulate) getWidths(headers []string, data []*TabulateRow) []int {
 	return widths
 	return widths
 }
 }
 
 
+// SetTitle sets the title of the table can also accept a second string to define an alignment for the title
+func (t *Tabulate) SetTitle(title ...string) *Tabulate {
+
+	t.Title = title[0]
+	if len(title) > 1 {
+		t.TitleAlign = title[1]
+	}
+
+	return t
+}
+
 // Set Headers of the table
 // Set Headers of the table
 // If Headers count is less than the data row count, the headers will be padded to the right
 // If Headers count is less than the data row count, the headers will be padded to the right
 func (t *Tabulate) SetHeaders(headers []string) *Tabulate {
 func (t *Tabulate) SetHeaders(headers []string) *Tabulate {
@@ -407,6 +441,9 @@ func (t *Tabulate) wrapCellData() []*TabulateRow {
 	var arr []*TabulateRow
 	var arr []*TabulateRow
 	var cleanSplit bool
 	var cleanSplit bool
 	var addr int
 	var addr int
+	if len(t.Data) == 0 {
+		return arr
+	}
 	next := t.Data[0]
 	next := t.Data[0]
 	for index := 0; index <= len(t.Data); index++ {
 	for index := 0; index <= len(t.Data); index++ {
 		elements := next.Elements
 		elements := next.Elements