Skip to content
Snippets Groups Projects
Select Git revision
  • 12bc71ba07b0e8a3b78a90a7422119e469cafe73
  • main default protected
2 results

run.go

Blame
  • run.go 1.44 KiB
    // Package bar handles the main program for i3going-on.
    // Copyright (C) 2022 Kevin Lyda <kevin@lyda.ie>
    package bar
    
    import (
    	"bufio"
    	"fmt"
    	"os"
    	"strings"
    	"time"
    
    	"github.com/spf13/cobra"
    	"github.com/spf13/viper"
    	"gitlab.ie.suberic.net/kevin/i3going-on/config"
    	"golang.org/x/sys/unix"
    )
    
    func handleCommands() {
    	logger, err := os.Create("/tmp/i3going-on.log")
    	if err != nil {
    		panic(err)
    	}
    	scanner := bufio.NewScanner(os.Stdin)
    	for scanner.Scan() {
    		logger.WriteString(scanner.Text())
    	}
    }
    
    // Run is essentially the main program.
    func Run(cmd *cobra.Command, args []string) {
    	viper.BindPFlags(cmd.Flags())
    	cfg, err := config.ReadConfig(viper.GetString("config"))
    	if err != nil {
    		// TODO Make a better default
    		cfg, err = config.ReadConfigStr(`---
    modules:
      - module: text
        name: post
        params:
          text: "post"
          color: "#11ff11"
        on-click: xdg-open https://mastodon.ie/
      - module: date
        params:
          format: 06-05-04 15:02
        on-click: xdg-open https://calendar.google.com/
    `)
    		cobra.CheckErr(err)
    	}
    	go handleCommands()
    
    	fmt.Printf(`{ "version": 1, "stop_signal": %d, "cont_signal": %d, "click_events": true }`+"\n",
    		unix.SignalNum("SIGSTOP"), unix.SignalNum("SIGCONT"))
    	separator := "["
    	for {
    		line := make([]string, 0, 5)
    		for _, module := range cfg.Modules {
    			line = append(line, module.Render())
    		}
    		fmt.Printf("%s\n[%s]", separator, strings.Join(line, ","))
    		separator = ","
    		time.Sleep(5 * time.Second)
    	}
    }