mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Merge pull request #683 from noxer/master
Expose the widget dimensions in cmdrunner
This commit is contained in:
commit
895b33ccdd
@ -19,6 +19,10 @@ type Settings struct {
|
||||
cmd string `help:"The terminal command to be run, withouth the arguments. Ie: ping, whoami, curl."`
|
||||
tail bool `help:"Automatically scroll to the end of the command output."`
|
||||
maxLines int `help:"Maximum number of lines kept in the buffer."`
|
||||
|
||||
// The dimensions of the module
|
||||
width int
|
||||
height int
|
||||
}
|
||||
|
||||
// NewSettingsFromYAML loads the cmdrunner portion of the WTF config
|
||||
@ -33,5 +37,7 @@ func NewSettingsFromYAML(name string, moduleConfig *config.Config, globalConfig
|
||||
maxLines: moduleConfig.UInt("maxLines", 256),
|
||||
}
|
||||
|
||||
settings.width, settings.height = utils.CalculateDimensions(moduleConfig, globalConfig)
|
||||
|
||||
return &settings
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package cmdrunner
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -108,6 +109,7 @@ func (widget *Widget) execute() {
|
||||
// Setup the command to run
|
||||
cmd := exec.Command(widget.settings.cmd, widget.settings.args...)
|
||||
cmd.Stdout = widget
|
||||
cmd.Env = widget.environment()
|
||||
|
||||
// Run the command and wait for it to exit in another Go-routine
|
||||
go func() {
|
||||
@ -134,3 +136,13 @@ func (widget *Widget) drainLines(n int) {
|
||||
widget.buffer.ReadBytes('\n')
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) environment() []string {
|
||||
envs := os.Environ()
|
||||
envs = append(
|
||||
envs,
|
||||
fmt.Sprintf("WTF_WIDGET_WIDTH=%d", widget.settings.width),
|
||||
fmt.Sprintf("WTF_WIDGET_HEIGHT=%d", widget.settings.height),
|
||||
)
|
||||
return envs
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ import (
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -144,3 +146,53 @@ func ParseJson(obj interface{}, text io.Reader) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CalculateDimensions reads the module dimensions from the module and global config. The border is already substracted.
|
||||
func CalculateDimensions(moduleConfig, globalConfig *config.Config) (int, int) {
|
||||
// Read the source data from the config
|
||||
left := moduleConfig.UInt("position.left", 0)
|
||||
top := moduleConfig.UInt("position.top", 0)
|
||||
width := moduleConfig.UInt("position.width", 0)
|
||||
height := moduleConfig.UInt("position.height", 0)
|
||||
|
||||
cols := ToInts(globalConfig.UList("wtf.grid.columns"))
|
||||
rows := ToInts(globalConfig.UList("wtf.grid.rows"))
|
||||
|
||||
// Make sure the values are in bounds
|
||||
left = clamp(left, 0, len(cols)-1)
|
||||
top = clamp(top, 0, len(rows)-1)
|
||||
width = clamp(width, 0, len(cols)-left)
|
||||
height = clamp(height, 0, len(rows)-top)
|
||||
|
||||
// Start with the border subtracted and add all the spanned rows and cols
|
||||
w, h := -2, -2
|
||||
for _, x := range cols[left : left+width] {
|
||||
w += x
|
||||
}
|
||||
for _, y := range rows[top : top+height] {
|
||||
h += y
|
||||
}
|
||||
|
||||
// The usable space may be empty
|
||||
w = max(w, 0)
|
||||
h = max(h, 0)
|
||||
|
||||
return w, h
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func clamp(x, a, b int) int {
|
||||
if a > x {
|
||||
return a
|
||||
}
|
||||
if b < x {
|
||||
return b
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user