From 72dc4b17808dff6cccb23f89ad65e8f1c879851b Mon Sep 17 00:00:00 2001 From: Russell Cohen Date: Mon, 24 Aug 2015 18:41:17 -0700 Subject: [PATCH] render-basic won't print off the end of lines --- render-basic/Render.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/render-basic/Render.go b/render-basic/Render.go index 294f567..351662a 100644 --- a/render-basic/Render.go +++ b/render-basic/Render.go @@ -13,6 +13,7 @@ type Renderer struct { colWidths *map[string]int cols *[]string height int64 + width int64 rowsPrinted *int64 } @@ -24,19 +25,24 @@ func main() { out, _ := cmd.Output() wh := strings.Split(string(out), " ") hstr := strings.Trim(wh[0], "\n") - rows := int64(0) + wstr := strings.Trim(wh[1], "\n") height, _ := strconv.ParseInt(hstr, 10, 64) + width, _ := strconv.ParseInt(wstr, 10, 64) + + rows := int64(0) + fmt.Println("%v", width) if len(os.Args) == 2 && os.Args[1] == "noraw" { - util.ConnectToStdIn(Renderer{false, &m, &cols, height, &rows}) + util.ConnectToStdIn(Renderer{false, &m, &cols, height, width, &rows}) } else { - util.ConnectToStdIn(Renderer{true, &m, &cols, height, &rows}) + util.ConnectToStdIn(Renderer{true, &m, &cols, height, width, &rows}) } } func (r Renderer) Process(inp map[string]interface{}) { if util.IsPlus(inp) { var printed = false + charsPrinted := int64(0) colsWidth := render.Columns([]map[string]interface{}{inp}) colNames := render.ColumnNames(colsWidth) *r.cols = colNames @@ -44,8 +50,19 @@ func (r Renderer) Process(inp map[string]interface{}) { for _, col := range colNames { v, _ := inp[col] vStr := fmt.Sprint(v) - spaces := strings.Repeat(" ", (len(col)+3+colsWidth[col])-len(vStr)) - fmt.Printf("[%s=%v]%s", col, vStr, spaces) + spaces := strings.Repeat(" ", colsWidth[col]-len(vStr)) + finalStr := fmt.Sprintf("[%s=%v]%s", col, vStr, spaces) + if charsPrinted+int64(len(finalStr)) > r.width { + availableChars := r.width - charsPrinted + if availableChars > 3 { + fmt.Printf(finalStr[:availableChars-3]) + fmt.Printf("...") + charsPrinted = r.width + } + } else { + charsPrinted += int64(len(finalStr)) + fmt.Printf(finalStr) + } } if printed { fmt.Printf(";")