Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #13 from rcoh/master
Browse files Browse the repository at this point in the history
Delete old curses based renderer
  • Loading branch information
rcoh authored Dec 22, 2016
2 parents b391ed8 + 85709f3 commit 60ec6ab
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 165 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.swp
*.swo
.idea
107 changes: 0 additions & 107 deletions render-basic/Render.go

This file was deleted.

150 changes: 93 additions & 57 deletions render/Render.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,107 @@
package main

import (
"fmt"
"github.com/SumoLogic/sumoshell/render-util"
"github.com/jroimartin/gocui"
"log"
)
import "fmt"
import "github.com/SumoLogic/sumoshell/util"
import "github.com/SumoLogic/sumoshell/render-util"
import "strings"
import "os/exec"
import "os"
import "strconv"

// Need to determine all the columns in the current data, then render based on that

func makeLayout(state *render.RenderState) layoutFunc {
return func(g *gocui.Gui) error {
_, maxY := g.Size()
viewColumns := render.Columns(*state.Messages)
viewNames := render.ColumnNames(viewColumns)
var pos = 0
for _, key := range viewNames {
width := viewColumns[key]
if v, err := g.SetView(key, pos, 0, pos+width, maxY); err != nil {
if err != gocui.ErrorUnkView {
return err
}
fmt.Fprintln(v, key)
}
pos += width + 1
}
update(viewNames, state, g)
return nil
}
type Renderer struct {
showRaw bool
colWidths *map[string]int
cols *[]string
height int64
width int64
rowsPrinted *int64
}

func update(columns []string, state *render.RenderState, g *gocui.Gui) {
for _, column := range columns {
v, _ := g.View(column)
v.Clear()
fmt.Fprintln(v, column)
for _, row := range *state.Messages {
fmt.Fprintln(v, row[column])
}
}

}
func main() {
m := make(map[string]int)
cols := []string{}
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdout
out, _ := cmd.Output()
wh := strings.Split(string(out), " ")
hstr := strings.Trim(wh[0], "\n")
wstr := strings.Trim(wh[1], "\n")
height, _ := strconv.ParseInt(hstr, 10, 64)
width, _ := strconv.ParseInt(wstr, 10, 64)

type layoutFunc func(*gocui.Gui) error
rows := int64(0)

func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.Quit
if len(os.Args) == 2 && os.Args[1] == "noraw" {
util.ConnectToStdIn(Renderer{false, &m, &cols, height, width, &rows})
} else {
util.ConnectToStdIn(Renderer{true, &m, &cols, height, width, &rows})
}
}

func main() {
var err error
g := gocui.NewGui()
if err := g.Init(); err != nil {
log.Panicln(err)
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
*r.colWidths = colsWidth
for _, col := range colNames {
v, _ := inp[col]
vStr := fmt.Sprint(v)
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(";")
}
if r.showRaw {
fmt.Printf(util.ExtractRaw(inp))
}
fmt.Print("\n")
}
defer g.Close()

renderState := render.NewConnectedRenderState(g.Flush)
g.SetLayout(makeLayout(renderState))
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
if util.IsStartRelation(inp) {
fmt.Println("======")
for _, col := range *r.cols {
width := (*r.colWidths)[col]
spaces := strings.Repeat(" ", width-len(col))
fmt.Printf("%v%s", col, spaces)
}
*r.rowsPrinted += 2
fmt.Printf("\n")
}

g.MainLoop()

if err != nil && err != gocui.Quit {
log.Panicln(err)
if util.IsRelation(inp) {
colsWidth := render.Columns([]map[string]interface{}{inp})
colNames := render.ColumnNames(colsWidth)
*r.cols = colNames
*r.colWidths = colsWidth
for _, col := range colNames {
v, _ := inp[col]
vStr := fmt.Sprint(v)
spaces := strings.Repeat(" ", colsWidth[col]-len(vStr))
fmt.Printf("%v%s", vStr, spaces)
}
*r.rowsPrinted += 1
fmt.Printf("\n")
}

if util.IsEndRelation(inp) {
if *r.rowsPrinted < r.height-1 {
for i := *r.rowsPrinted; i < r.height; i++ {
fmt.Printf("\n")
}
}
*r.rowsPrinted = 0
}
}
3 changes: 2 additions & 1 deletion util/Raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func ConnectToReader(operator SumoOperator, reader io.Reader) {
err := json.Unmarshal(buf, &rawMsg)
buf = []byte{}
if err != nil {
log.Println("Error parsing json")
log.Println("Error parsing json", err)
log.Println(string(line))
} else {
mapMessage, ok := rawMsg.(map[string]interface{})
if ok {
Expand Down

0 comments on commit 60ec6ab

Please sign in to comment.