func main() { window := geometry.Flag("window.geometry", image.Rect(0, 0, 117, 20), "window geometry in pixels") battRect := geometry.Flag("battery.geometry", image.Rect(0, 0, 21, 18).Add(image.Pt(1, 2)), "battery icon geometry in pixels") borderThickness := flag.Int("border", 1, "battery border thickness in pixels") textRect := geometry.Flag("text.geometry", image.Rect(0, 0, 95, 20).Add(image.Pt(22, 0)), "text box geometry in pixels") textFont := flag.String("text.font", "DejaVuSans-Bold", "application text font") textFontSize := flag.Float64("text.fontsize", 14, "application text font size") textInterval := flag.Duration("text.interval", 7*time.Second+500*time.Millisecond, "interval to display each formatted text metric") flag.Parse() // remaining arguments are text formatters to rotate between var formatters []battery.MetricFormatter for _, tsrc := range flag.Args() { t, err := battery.FormatMetricTemplate(tsrc) if err != nil { log.Fatalf("template: %v %q", err, tsrc) } formatters = append(formatters, t) } if len(formatters) == 0 { formatters = append(formatters, defaultFormatters...) } // Open the specified font. ttfpath, err := LocateFont(*textFont) if err != nil { log.Fatalf("font: %v", err) } font, err := ReadFontFile(ttfpath) if err != nil { log.Fatalf("font: %v", err) } // configure the application window layout layout := &AppLayout{ rect: *window, battRect: *battRect, textRect: *textRect, thickness: *borderThickness, DPI: 72, font: font, fontSize: *textFontSize, } app := NewApp(layout) app.BatteryColor = defaultGrey // Connect to the x server and create a dockapp window for the process. X, err := xgbutil.NewConn() if err != nil { log.Fatal(err) } dockapp, err := dockapp.New(X, *window) if err != nil { log.Fatal(err) } defer dockapp.Destroy() // begin profiling the battery. prime the profile by immediately calling // the Metrics method. metricsc := make(chan *battery.Metrics, 1) guage, err := creeperguage.NewCreeperBatteryGuage() if err != nil { log.Fatal(err) } batt := battery.NewProfiler(guage) go batt.Start(time.Minute, metricsc) defer batt.Stop() // rotate through all provided formatters (or the default set), sending // them to the draw loop at the specified interval. formatterc := make(chan battery.MetricFormatter, 1) go battery.RotateMetricsFormat(*textInterval, formatterc, formatters...) // begin the main draw loop. the draw loop receives updates in the form of // new battery metrics and formatters. The event loop will exit if the // draw loop ever terminates. go RunApp(dockapp, app, metricsc, formatterc) // finally map the window and start the main event loop dockapp.Main() }
func main() { defer func() { if e := recover(); e != nil { panic(e) } panic("show me the stacks") }() window := geometry.Flag("window.geometry", image.Rect(0, 0, 100, 20), "window geometry in pixels") ignore := flag.String("ignore", "", "comma separated list of cpus to ignore") flag.Parse() poll, err := Poll(time.Second) if err != nil { log.Fatal(err) } delta := Delta(poll.C) deltaCPU := TimeToCPU(delta) if *ignore != "" { ignores := strings.Split(*ignore, ",") deltaCPU = FilterCPU(deltaCPU, ignores) } app := NewApp() // Connect to the x server and create a dockapp window for the process. X, err := xgbutil.NewConn() if err != nil { log.Fatal(err) } sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt, syscall.SIGTERM) dockapp, err := dockapp.New(X, *window) if err != nil { log.Fatal(err) } defer dockapp.Destroy() defer dockapp.Quit() // map the window and start the main event loop go dockapp.Main() // begin the main draw loop. the draw loop receives updates in the form of // new battery metrics and formatters. The event loop will exit if the // draw loop ever terminates. go RunApp(dockapp, app, deltaCPU) var timeout <-chan time.Time for { select { case s := <-sig: signal.Stop(sig) log.Printf("signal received: %s", s) poll.Stop() timeout = time.After(time.Second) case <-timeout: panic("timeout") case <-app.Done(): return } } }