~jcc/swaybar-commander

0660f95bf89493ebf39fd038455d76a8247680d8 — Jason Cox 1 year, 8 months ago 8b042d7
Allow an update requester command for each block
4 files changed, 41 insertions(+), 10 deletions(-)

M README.md
M block/block.go
M config/config.go
M swaybar-commander.1.scd
M README.md => README.md +1 -1
@@ 1,6 1,6 @@
# swaybar-commander

[swaybar-commander](https://sr.ht/~jcc/swaybar-commander) is a swaybar command that allows each section of the bar to update independently. Each section updates at a regular frequency, and additional updates can be requested on-demand via a helper command. The sections are configured in a simple TOML file and can take advantage of the [swaybar-protocol](https://man.archlinux.org/man/swaybar-protocol.7.en).
[swaybar-commander](https://sr.ht/~jcc/swaybar-commander) is a swaybar command that allows each section of the bar to update independently. Each section updates at a regular frequency, and additional updates can be requested on-demand via a helper command and/or a background script. The sections are configured in a simple TOML file and can take advantage of the [swaybar-protocol](https://man.archlinux.org/man/swaybar-protocol.7.en).

## Documentation


M block/block.go => block/block.go +25 -6
@@ 7,6 7,7 @@ import (
	"os"
	"os/exec"
	"os/signal"
	"strconv"
	"strings"
	"syscall"
	"time"


@@ 20,17 21,19 @@ var execCommand = exec.Command // allow mocking in tests
// Block represents a single block in the swaybar.
type Block struct {
	proto.Block
	cmd      []string
	pollFreq time.Duration
	ticker   *time.Ticker
	cmd                []string
	updateRequesterCmd []string
	pollFreq           time.Duration
	ticker             *time.Ticker
}

// New creates a Block from its configuration.
func New(conf config.Block) Block {
	return Block{
		Block:    conf.Block,
		cmd:      conf.Cmd,
		pollFreq: time.Duration(conf.PollSecs) * time.Second,
		Block:              conf.Block,
		cmd:                conf.Cmd,
		updateRequesterCmd: conf.UpdateRequesterCmd,
		pollFreq:           time.Duration(conf.PollSecs) * time.Second,
	}
}



@@ 40,6 43,22 @@ func (b *Block) Run(updateChan chan<- proto.Block, updateRequestSig syscall.Sign
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, updateRequestSig)

	if len(b.updateRequesterCmd) > 0 {
		name := b.updateRequesterCmd[0]
		args := append(
			b.updateRequesterCmd[1:],
			strconv.Itoa(os.Getpid()),
			strconv.Itoa(int(updateRequestSig)),
		)

		updateRequesterCmd := execCommand(name, args...)
		err := updateRequesterCmd.Start()
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to start update requester for block %v: %v\n", *b, err)
			os.Exit(1)
		}
	}

	b.ticker = time.NewTicker(b.pollFreq)
	defer b.ticker.Stop()


M config/config.go => config/config.go +3 -2
@@ 20,8 20,9 @@ type Main struct {
// Block represents the configuration for a single block.
type Block struct {
	proto.Block
	Cmd      []string
	PollSecs int
	Cmd                []string
	UpdateRequesterCmd []string
	PollSecs           int
}

var readFile = ioutil.ReadFile // allow mocking in tests

M swaybar-commander.1.scd => swaybar-commander.1.scd +12 -1
@@ 15,7 15,8 @@ swaybar-commander - swaybar command for *sway*(1) using the

swaybar-commander allows defining multiple sections of the swaybar, each of
which updates at its own frequency. Updates can also be requested using
*swaybar-commander update ...*, as described below.
*swaybar-commander update ...* or via an *UpdateRequesterCmd* that runs in the
background, as described below.

To use it, you'll need to create a config file, as described below, and then set
*swaybar-commander* as your *swaybar_command* (see *sway-bar*(5)).


@@ 33,6 34,9 @@ Note that "updating" a block means that it will re-run its *Cmd*, and if the
output has changed, swaybar-commander will output a new element to be rendered
by the bar.

The *UpdateRequesterCmd* config value can also be used to request updates -- see
below.

# CONFIGURATION

swaybar-commander expects a TOML config file at


@@ 71,6 75,13 @@ required.
	*Cmd* outputs, any unspecified properties will fall back to their default
	values if defined.

*UpdateRequesterCmd*
	An array of strings specifying a command and arguments to be run in the
	background in order to request updates to the block. The command will be
	passed a process ID and a signal number as the first and second arguments
	following the specified arguments. Sending the specified signal to the
	specified process will request an update to the block, as described above.

*PollSecs*
	An integer specifying how often, in seconds, *Cmd* should be run to check
	for new content. If unspecified, *PollSecs* defaults to _60_.