Skip to content

Commit

Permalink
Add additional display configuration (#49)
Browse files Browse the repository at this point in the history
* Use a display config struct

* Clean-up

* Move main to cmd

* Fix tests

* Add padding options

* Make status widgets optional

* Upgrade termdash, use new splitting options

* Address workflow testing issues with cmd
  • Loading branch information
spacez320 authored Mar 19, 2024
1 parent ae9593a commit ce09eed
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 150 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jobs:
- uses: actions/checkout@v4
- name: Tests
run: |
pushd cmd/cryptarch
go mod tidy # FIXME I don't know why this is necessary.
go test
popd
go test internal/*
go test pkg/*
Expand Down
3 changes: 2 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ before:
# - go generate ./... # Leaving for future use.

builds:
- env:
- dir: cmd/cryptarch
env:
- CGO_ENABLED=0
goos:
- darwin
Expand Down
43 changes: 38 additions & 5 deletions main.go → cmd/cryptarch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ const (
MODE_READ // For running in 'read' mode.
)

// Misc. constants.
const (
CONFIG_FILE_DIR = "cryptarch"
CONFIG_FILE_NAME = "cryptarch.yaml"
)

var (
count int // Number of attempts to execute the query.
delay int // Delay between queries.
Expand All @@ -57,12 +63,17 @@ var (
labels string // Result value labels.
logLevel string // Log level.
mode int // Mode to execute in.
outerPaddingBottom int // Bottom padding settings.
outerPaddingLeft int // Left padding settings.
outerPaddingRight int // Right padding settings.
outerPaddingTop int // Top padding settings.
port string // Port for RPC.
promExporterAddr string // Address for Prometheus metrics page.
promPushgatewayAddr string // Address for Prometheus Pushgateway.
queries queriesArg // Queries to execute.
showHelp bool // Whether or not to show help.
showHelp bool // Whether or not to show helpt
showLogs bool // Whether or not to show logs.
showStatus bool // Whether or not to show statuses.
silent bool // Whether or not to be quiet.

ctx = context.Background() // Initialize context.
Expand Down Expand Up @@ -90,18 +101,24 @@ func main() {
doneQueriesChan chan bool // Channel for tracking query completion.
pauseQueryChans map[string]chan bool // Channels for pausing queries.

resultsReadyChan = make(chan bool) // Channel for signaling results readiness.
displayConfig = lib.NewDisplayConfig() // Configuration for display modes.
resultsReadyChan = make(chan bool) // Channel for signaling results readiness.
)

// Define arguments.
flag.BoolVar(&history, "history", true, "Whether or not to use or preserve history.")
flag.BoolVar(&showHelp, "show-help", true, "Whether or not to show help displays.")
flag.BoolVar(&showLogs, "show-logs", false, "Whether or not to show log displays.")
flag.BoolVar(&showStatus, "show-status", true, "Whether or not to show status displays.")
flag.BoolVar(&silent, "silent", false, "Don't output anything to a console.")
flag.IntVar(&count, "count", 1, "Number of query executions. -1 for continuous.")
flag.IntVar(&delay, "delay", 3, "Delay between queries (seconds).")
flag.IntVar(&displayMode, "display", int(lib.DISPLAY_MODE_RAW), "Result mode to display.")
flag.IntVar(&mode, "mode", int(MODE_QUERY), "Mode to execute in.")
flag.IntVar(&outerPaddingBottom, "outer-padding-bottom", -1, "Bottom display padding.")
flag.IntVar(&outerPaddingLeft, "outer-padding-left", -1, "Left display padding.")
flag.IntVar(&outerPaddingRight, "outer-padding-right", -1, "Right display padding.")
flag.IntVar(&outerPaddingTop, "outer-padding-top", -1, "Top display padding.")
flag.StringVar(&filters, "filters", "", "Results filters.")
flag.StringVar(&labels, "labels", "", "Labels to apply to query values, separated by commas.")
flag.StringVar(&logLevel, "log-level", "error", "Log level.")
Expand Down Expand Up @@ -172,16 +189,32 @@ func main() {
ctx = context.WithValue(ctx, "filters", parseCommaDelimitedArg(filters))
ctx = context.WithValue(ctx, "queries", queries.ToStrings())

// Set-up display configuration.
displayConfig.ShowHelp = showHelp
displayConfig.ShowLogs = showLogs
displayConfig.ShowStatus = showStatus
if outerPaddingBottom >= 0 {
displayConfig.OuterPaddingBottom = outerPaddingBottom
}
if outerPaddingLeft >= 0 {
displayConfig.OuterPaddingLeft = outerPaddingLeft
}
if outerPaddingRight >= 0 {
displayConfig.OuterPaddingRight = outerPaddingRight
}
if outerPaddingTop >= 0 {
displayConfig.OuterPaddingTop = outerPaddingTop
}

// Execute result viewing.
if !silent {
lib.Results(
ctx,
lib.DisplayMode(displayMode),
ctx.Value("queries").([]string)[0], // Always start with the first query.
history,
showHelp,
showLogs,
lib.Config{
displayConfig,
&lib.Config{
LogLevel: logLevel,
PrometheusExporterAddr: promExporterAddr,
PushgatewayAddr: promPushgatewayAddr,
Expand Down
File renamed without changes.
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ require pkg/storage v0.0.0 // indirect
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.6.1-0.20231203215052-2917c3801e73 // indirect
github.com/gdamore/tcell/v2 v2.7.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mum4k/termdash v0.18.0 // indirect
github.com/mum4k/termdash v0.20.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/tview v0.0.0-20231206124440-5f078138442e // indirect
github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.9.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

Expand Down
29 changes: 14 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.1-0.20231203215052-2917c3801e73 h1:SeDV6ZUSVlTAUUPdMzPXgMyj96z+whQJRRUff8dIeic=
github.com/gdamore/tcell/v2 v2.6.1-0.20231203215052-2917c3801e73/go.mod h1:pwzJMyH4Hd0AZMJkWQ+/g01dDvYWEvmJuaiRU71Xl8k=
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU=
github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
github.com/mum4k/termdash v0.18.0 h1:wpy3FKcVV5s2TOoMTKzqQXwL5VClZIlNrRqZDpeIzBA=
github.com/mum4k/termdash v0.18.0/go.mod h1:VWL18wLZDKVKF/f4TkMRiKZb9Eg8Ax99PtNuGuRAguw=
github.com/mum4k/termdash v0.20.0 h1:g6yZvE7VJmuefJmDrSrv5Az8IFTTSCqG0x8xiOMPbyM=
github.com/mum4k/termdash v0.20.0/go.mod h1:/kPwGKcOhLawc2OmWJPLQ5nzR5PmcbiKMcVv9/413b4=
github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
Expand Down Expand Up @@ -51,22 +53,19 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
70 changes: 50 additions & 20 deletions internal/lib/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
"github.com/rivo/tview"
)

// General configuration for display modes.
type DisplayConfig struct {
HelpSize, LogsSize, ResultsSize int // Proportional size of widgets.
OuterPaddingBottom, OuterPaddingLeft, OuterPaddingRight, OuterPaddingTop int // Padding for the full display.
ShowHelp, ShowLogs, ShowStatus bool // Whether or not to show widgets.
TablePadding int // Padding for table cells in table displays.
}

// Represents the display driver.
type DisplayDriver int

Expand All @@ -34,16 +42,21 @@ const (
DISPLAY_MODE_GRAPH // For running in 'graph' display mode.
)

// Defaults for display configs.
const (
DEFAULT_HELP_SIZE = 10
DEFAULT_LOGS_SIZE = 15
DEFAULT_OUTER_PADDING_BOTTOM = 5
DEFAULT_OUTER_PADDING_LEFT = 10
DEFAULT_OUTER_PADDING_RIGHT = 10
DEFAULT_OUTER_PADDING_TOP = 5
DEFAULT_RESULTS_SIZE = 75
DEFAULT_TABLE_PADDING = 2
)

// Misc. constants.
const (
HELP_TEXT = "(ESC) Quit | (Space) Pause | (Tab) Next Display | (n) Next Query"
HELP_SIZE = 10 // Proportional size of the logs widget.
LOGS_SIZE = 15 // Proportional size of the logs widget.
OUTER_PADDING_LEFT = 10 // Left padding for the full display.
OUTER_PADDING_RIGHT = 10 // Right padding for the full display.
OUTER_PADDING_TOP = 5 // Top padding for the full display.
OUTER_PADDING_BOTTOM = 5 // Bottom padding for the full display.
RESULTS_SIZE = 75 // Proportional size of the results widget.
TABLE_PADDING = 2 // Padding for table cell entries.
HELP_TEXT = "(ESC) Quit | (Space) Pause | (Tab) Next Display | (n) Next Query"
)

var (
Expand Down Expand Up @@ -78,6 +91,23 @@ func displayQuit() {
close(interruptChan)
}

// Creates a default display config.
func NewDisplayConfig() *DisplayConfig {
return &DisplayConfig{
HelpSize: DEFAULT_HELP_SIZE,
LogsSize: DEFAULT_LOGS_SIZE,
OuterPaddingBottom: DEFAULT_OUTER_PADDING_BOTTOM,
OuterPaddingLeft: DEFAULT_OUTER_PADDING_LEFT,
OuterPaddingRight: DEFAULT_OUTER_PADDING_RIGHT,
OuterPaddingTop: DEFAULT_OUTER_PADDING_TOP,
ResultsSize: DEFAULT_RESULTS_SIZE,
ShowHelp: true,
ShowLogs: false,
ShowStatus: true,
TablePadding: DEFAULT_TABLE_PADDING,
}
}

// Presents raw output.
func RawDisplay(query string) {
var (
Expand All @@ -100,7 +130,7 @@ func RawDisplay(query string) {
}

// Update the results pane with new results as they are generated.
func StreamDisplay(query string, filters, labels []string, showHelp, showLogs bool) {
func StreamDisplay(query string, filters, labels []string, displayConfig *DisplayConfig) {
var (
reader = readerIndexes[query] // Reader index for the query.
)
Expand All @@ -110,7 +140,7 @@ func StreamDisplay(query string, filters, labels []string, showHelp, showLogs bo
reader.Dec()

// Initialize the display.
widgets := initDisplayTviewText(query, filters, labels, showHelp, showLogs)
widgets := initDisplayTviewText(query, filters, labels, displayConfig)

// Start the display.
display(
Expand Down Expand Up @@ -146,21 +176,21 @@ func StreamDisplay(query string, filters, labels []string, showHelp, showLogs bo
}

// Creates a table of results for the results pane.
func TableDisplay(query string, filters, labels []string, showHelp, showLogs bool) {
func TableDisplay(query string, filters, labels []string, displayConfig *DisplayConfig) {
var (
widgets tviewWidgets // Widgets produced by tview.

reader = readerIndexes[query] // Reader index for the query.
tableCellPadding = strings.Repeat(" ", TABLE_PADDING) // Padding to add to table cell content.
valueIndexes = []int{} // Indexes of the result values to add to the table.
reader = readerIndexes[query] // Reader index for the query.
tableCellPadding = strings.Repeat(" ", displayConfig.TablePadding) // Padding to add to table cell content.
valueIndexes = []int{} // Indexes of the result values to add to the table.
)

// Wait for the first result to appear to synchronize storage.
GetResultWait(query)
reader.Dec()

// Initialize the display.
widgets = initDisplayTviewTable(query, filters, labels, showHelp, showLogs)
widgets = initDisplayTviewTable(query, filters, labels, displayConfig)

// Start the display.
display(
Expand Down Expand Up @@ -251,7 +281,7 @@ func TableDisplay(query string, filters, labels []string, showHelp, showLogs boo
}

// Creates a graph of results for the results pane.
func GraphDisplay(query string, filters, labels []string, showHelp, showLogs bool) {
func GraphDisplay(query string, filters, labels []string, displayConfig *DisplayConfig) {
var (
err error // General error holder.

Expand Down Expand Up @@ -286,7 +316,7 @@ func GraphDisplay(query string, filters, labels []string, showHelp, showLogs boo
// Print all previous results.
for _, result := range store.GetToIndex(query, reader) {
// We can display the next result.
value := result.Values[valueIndex]
value := result.Values.Get(valueIndex)

switch value.(type) {
case int64:
Expand All @@ -307,7 +337,7 @@ func GraphDisplay(query string, filters, labels []string, showHelp, showLogs boo
<-pauseDisplayChan
default:
// We can display the next result.
value := (GetResult(query)).Values[valueIndex]
value := (GetResult(query)).Values.Get(valueIndex)

switch value.(type) {
case int64:
Expand All @@ -322,5 +352,5 @@ func GraphDisplay(query string, filters, labels []string, showHelp, showLogs boo

// Initialize the display. This must happen after the display function is invoked, otherwise data
// will never appear.
initDisplayTermdash(widgets, query, filters, labels, showHelp, showLogs)
initDisplayTermdash(widgets, query, filters, labels, displayConfig)
}
Loading

0 comments on commit ce09eed

Please sign in to comment.