src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

main.go (2232B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"flag"
      6 	"fmt"
      7 	"os"
      8 	"path/filepath"
      9 	"time"
     10 
     11 	"code.dwrz.net/src/pkg/log"
     12 	"code.dwrz.net/src/pkg/statusbar"
     13 	"code.dwrz.net/src/pkg/statusbar/cpu"
     14 	"code.dwrz.net/src/pkg/statusbar/datetime"
     15 	"code.dwrz.net/src/pkg/statusbar/disk"
     16 	"code.dwrz.net/src/pkg/statusbar/eth"
     17 	"code.dwrz.net/src/pkg/statusbar/light"
     18 	"code.dwrz.net/src/pkg/statusbar/memory"
     19 	"code.dwrz.net/src/pkg/statusbar/mic"
     20 	"code.dwrz.net/src/pkg/statusbar/power"
     21 	"code.dwrz.net/src/pkg/statusbar/temp"
     22 	"code.dwrz.net/src/pkg/statusbar/volume"
     23 	"code.dwrz.net/src/pkg/statusbar/wlan"
     24 	"code.dwrz.net/src/pkg/statusbar/wwan"
     25 )
     26 
     27 var once = flag.Bool("once", false, "print output once")
     28 
     29 // TODO: block labels; rendered in case of error.
     30 func main() {
     31 	flag.Parse()
     32 
     33 	var (
     34 		ctx = context.Background()
     35 		l   = log.New(os.Stderr)
     36 	)
     37 
     38 	// Setup workspace and log file.
     39 	cdir, err := os.UserCacheDir()
     40 	if err != nil {
     41 		l.Error.Fatalf(
     42 			"failed to determine user cache directory: %v", err,
     43 		)
     44 	}
     45 	wdir := filepath.Join(cdir, "statusbar")
     46 
     47 	if err := os.MkdirAll(wdir, os.ModeDir|0700); err != nil {
     48 		l.Error.Fatalf("failed to create tmp dir: %v", err)
     49 	}
     50 
     51 	flog, err := os.Create(wdir + "/log")
     52 	if err != nil {
     53 		l.Error.Fatalf("failed to create log file: %v", err)
     54 	}
     55 	defer flog.Close()
     56 
     57 	// Prepare the datetime format.
     58 	now := time.Now()
     59 	yearEnd := time.Date(
     60 		now.Year()+1, 1, 1, 0, 0, 0, 0, time.UTC,
     61 	).AddDate(0, 0, -1)
     62 
     63 	dfmt := fmt.Sprintf(
     64 		"+%%Y-%%m-%%d %%u/7 %%W/52 %%j/%d %%H:%%M %%Z",
     65 		yearEnd.YearDay(),
     66 	)
     67 
     68 	var bar = statusbar.New(statusbar.Parameters{
     69 		Blocks: []statusbar.Block{
     70 			cpu.New(),
     71 			temp.New(),
     72 			memory.New(),
     73 			disk.New("/", "/home"),
     74 			eth.New("eth0"),
     75 			wlan.New(),
     76 			wwan.New("wwan0"),
     77 			power.New(power.Path),
     78 			light.New(),
     79 			volume.New(),
     80 			mic.New(),
     81 			datetime.New(datetime.Parameters{
     82 				Format:   dfmt,
     83 				Timezone: "UTC",
     84 			}),
     85 		},
     86 		Log:       log.New(flog),
     87 		Separator: "┃",
     88 	})
     89 
     90 	// Initial print.
     91 	fmt.Println(bar.Render(ctx))
     92 	if *once {
     93 		os.Exit(0)
     94 	}
     95 
     96 	// Main loop.
     97 	ticker := time.NewTicker(1 * time.Second)
     98 	for {
     99 		select {
    100 		case <-ticker.C:
    101 			// now := time.Now()
    102 			fmt.Println(bar.Render(ctx))
    103 			// fmt.Println(time.Since(now))
    104 		}
    105 	}
    106 }