This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from rcoh/master
Delete old curses based renderer
- Loading branch information
Showing
4 changed files
with
96 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.swp | ||
*.swo | ||
.idea |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters