~jcc/swaybar-commander

2c009c59f759ae172c795eca67ae5576f194bc1d — Jason Cox 1 year, 9 months ago 70b5d20
Add doc comments
4 files changed, 20 insertions(+), 0 deletions(-)

M block/block.go
M commander.go
M config/config.go
M proto/proto.go
M block/block.go => block/block.go +5 -0
@@ 1,3 1,4 @@
// Package block provides the implementation of each block in the swaybar.
package block

import (


@@ 14,6 15,7 @@ import (

var execCommand = exec.Command // allow mocking in tests

// Block represents a single block in the swaybar.
type Block struct {
	proto.Block
	cmd      []string


@@ 21,6 23,7 @@ type Block struct {
	ticker   *time.Ticker
}

// New creates a Block from its configuration.
func New(conf config.Block) Block {
	return Block{
		Block:    conf.Block,


@@ 29,6 32,8 @@ func New(conf config.Block) Block {
	}
}

// Run runs a Block indefinitely, sending updated swaybar protocol blocks on the
// provided channel whenever there is a change.
func (b *Block) Run(updateChan chan<- proto.Block) {
	b.ticker = time.NewTicker(b.pollFreq)
	defer b.ticker.Stop()

M commander.go => commander.go +3 -0
@@ 1,3 1,6 @@
// swaybar-commander is a swaybar command that allows each section to be updated
// independently of the others. See the man page, swaybar-commander(1), for more
// info.
package main

import (

M config/config.go => config/config.go +5 -0
@@ 1,3 1,4 @@
// Package config handles the configuration file.
package config

import (


@@ 10,11 11,13 @@ import (
	"github.com/BurntSushi/toml"
)

// Main represents the full configuration.
type Main struct {
	Defaults proto.Block
	Blocks   []Block
}

// Block represents the configuration for a single block.
type Block struct {
	proto.Block
	Cmd      []string


@@ 23,6 26,8 @@ type Block struct {

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

// Load reads the config file, validates it, populates defaults as needed, and
// returns the parsed configuration.
func Load() (Main, error) {
	configHome, _ := os.LookupEnv("XDG_CONFIG_HOME")
	if configHome == "" {

M proto/proto.go => proto/proto.go +7 -0
@@ 1,3 1,4 @@
// Package proto provides types and functions implementing the swaybar protocol.
package proto

import (


@@ 7,6 8,7 @@ import (
	"os"
)

// Block represents the protocol fields of a block in the swaybar.
type Block struct {
	FullText            string       `json:"full_text"`
	ShortText           *string      `json:"short_text,omitempty"`


@@ 27,11 29,13 @@ type Block struct {
	Markup              *string      `json:"markup,omitempty"`
}

// Init initializes the swaybar protocol.
func Init() {
	fmt.Println(`{"version": 1}`)
	fmt.Println("[")
}

// Send tells the swaybar to render a new set of Blocks.
func Send(blocks []Block) error {
	jsonBytes, err := json.Marshal(blocks)
	if err != nil {


@@ 43,6 47,9 @@ func Send(blocks []Block) error {
	return nil
}

// MergeBlocks merges two Blocks. If a field is nil (or empty string in the case
// of FullText) on b1, it will be set to the value from b2. b1 is modified in
// place.
func MergeBlocks(b1 *Block, b2 Block) error {
	if b1 == nil {
		return errors.New("b1 cannot be nil")