Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to "snapshot" output for a single final state rather than incremental updates (eg, when piping the output)
- Loading branch information
Showing
23 changed files
with
167 additions
and
46 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package helpers | ||
|
||
import ( | ||
"rare/pkg/multiterm" | ||
"rare/pkg/multiterm/termstate" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var SnapshotFlag = &cli.BoolFlag{ | ||
Name: "snapshot", | ||
Usage: "In aggregators that support it, only output final results, and not progressive updates. Will enable automatically when piping output", | ||
} | ||
|
||
func BuildVTerm(forceSnapshot bool) multiterm.MultilineTerm { | ||
if forceSnapshot || termstate.IsPipedOutput() { | ||
return multiterm.NewBufferedTerm() | ||
} | ||
return multiterm.New() | ||
} | ||
|
||
func BuildVTermFromArguments(c *cli.Context) multiterm.MultilineTerm { | ||
snapshot := c.Bool(SnapshotFlag.Name) | ||
return BuildVTerm(snapshot) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func TestBuildVTerm(t *testing.T) { | ||
assert.NotNil(t, BuildVTerm(false)) | ||
assert.NotNil(t, BuildVTerm(true)) | ||
} | ||
|
||
func TestBuildVTermFromArgs(t *testing.T) { | ||
app := cli.NewApp() | ||
app.Flags = []cli.Flag{ | ||
&cli.BoolFlag{ | ||
Name: "snapshot", | ||
}, | ||
} | ||
app.Action = func(ctx *cli.Context) error { | ||
BuildVTermFromArguments(ctx) | ||
return nil | ||
} | ||
app.Run([]string{"", "--snapshot"}) | ||
} |
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package multiterm | ||
|
||
import "os" | ||
|
||
type BufferedTerm struct { | ||
*VirtualTerm | ||
} | ||
|
||
// NewBufferedTerm writes on Close | ||
func NewBufferedTerm() *BufferedTerm { | ||
return &BufferedTerm{ | ||
NewVirtualTerm(), | ||
} | ||
} | ||
|
||
func (s *BufferedTerm) Close() { | ||
s.WriteToOutput(os.Stdout) | ||
s.VirtualTerm.Close() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package multiterm | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestBufferedTerm(t *testing.T) { | ||
vt := NewBufferedTerm() | ||
vt.WriteForLine(0, "hello") | ||
vt.Close() | ||
} |
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 |
---|---|---|
|
@@ -36,7 +36,3 @@ func showCursor() { | |
func eraseRemainingLine() { | ||
fmt.Print(escape("[0K")) | ||
} | ||
|
||
func ResetCursor() { | ||
showCursor() | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package termstate | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
// Returns 'true' if output is being piped (Not char device) | ||
func IsPipedOutput() bool { | ||
if fi, err := os.Stdout.Stat(); err == nil { | ||
if (fi.Mode() & os.ModeCharDevice) == 0 { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Gets size of the terminal | ||
func GetTermRowsCols() (rows, cols int, ok bool) { | ||
fd := int(os.Stdout.Fd()) | ||
if !term.IsTerminal(fd) { | ||
return 0, 0, false | ||
} | ||
|
||
cols, rows, err := term.GetSize(fd) | ||
if err != nil { | ||
return 0, 0, false | ||
} | ||
|
||
return rows, cols, true | ||
} |
Oops, something went wrong.