1 Star 0 Fork 4

中华田园人/Sampler

forked from Gitee 极速下载/Sampler 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 4.80 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"fmt"
ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/asset"
"github.com/sqshq/sampler/client"
"github.com/sqshq/sampler/component"
"github.com/sqshq/sampler/component/asciibox"
"github.com/sqshq/sampler/component/barchart"
"github.com/sqshq/sampler/component/gauge"
"github.com/sqshq/sampler/component/layout"
"github.com/sqshq/sampler/component/runchart"
"github.com/sqshq/sampler/component/sparkline"
"github.com/sqshq/sampler/component/textbox"
"github.com/sqshq/sampler/config"
"github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data"
"github.com/sqshq/sampler/event"
"github.com/sqshq/sampler/metadata"
"runtime/debug"
"time"
)
type Starter struct {
player *asset.AudioPlayer
lout *layout.Layout
palette console.Palette
opt config.Options
cfg config.Config
}
func (s *Starter) startAll() []*data.Sampler {
samplers := make([]*data.Sampler, 0)
for _, c := range s.cfg.RunCharts {
cpt := runchart.NewRunChart(c, s.palette)
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers))
}
for _, c := range s.cfg.SparkLines {
cpt := sparkline.NewSparkLine(c, s.palette)
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
}
for _, c := range s.cfg.BarCharts {
cpt := barchart.NewBarChart(c, s.palette)
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers))
}
for _, c := range s.cfg.Gauges {
cpt := gauge.NewGauge(c, s.palette)
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Cur, c.Min, c.Max}, c.Triggers))
}
for _, c := range s.cfg.AsciiBoxes {
cpt := asciibox.NewAsciiBox(c, s.palette)
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
}
for _, c := range s.cfg.TextBoxes {
cpt := textbox.NewTextBox(c, s.palette)
samplers = append(samplers, s.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers))
}
return samplers
}
func (s *Starter) start(drawable ui.Drawable, consumer *data.Consumer, componentConfig config.ComponentConfig, itemsConfig []config.Item, triggersConfig []config.TriggerConfig) *data.Sampler {
cpt := component.NewComponent(drawable, consumer, componentConfig)
triggers := data.NewTriggers(triggersConfig, consumer, s.opt, s.player)
items := data.NewItems(itemsConfig, *componentConfig.RateMs)
s.lout.AddComponent(cpt)
time.Sleep(10 * time.Millisecond) // desync coroutines
return data.NewSampler(consumer, items, triggers, s.opt, s.cfg.Variables, *componentConfig.RateMs)
}
func main() {
cfg, opt := config.LoadConfig()
bc := client.NewBackendClient()
statistics := metadata.GetStatistics(cfg)
license := metadata.GetLicense()
if opt.LicenseKey != nil {
registerLicense(statistics, opt, bc)
}
console.Init()
defer console.Close()
player := asset.NewAudioPlayer()
if player != nil {
defer player.Close()
}
defer handleCrash(statistics, opt, bc)
defer updateStatistics(cfg, time.Now())
palette := console.GetPalette(*cfg.Theme)
lout := layout.NewLayout(component.NewStatusBar(*opt.ConfigFile, palette, license),
component.NewMenu(palette), component.NewIntro(palette), component.NewNagWindow(palette))
if statistics.LaunchCount == 0 {
if !opt.DisableTelemetry {
go bc.ReportInstallation(statistics)
}
lout.StartWithIntro()
} else if statistics.LaunchCount%20 == 0 { // once in a while
if license == nil || !license.Valid {
lout.StartWithNagWindow()
} else {
go verifyLicense(license, bc)
}
if !opt.DisableTelemetry {
go bc.ReportStatistics(statistics)
}
}
starter := &Starter{player, lout, palette, opt, *cfg}
samplers := starter.startAll()
handler := event.NewHandler(samplers, opt, lout)
handler.HandleEvents()
}
func handleCrash(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) {
if rec := recover(); rec != nil {
err := rec.(error)
if !opt.DisableTelemetry {
bc.ReportCrash(fmt.Sprintf("%s\n%s", err.Error(), string(debug.Stack())), statistics)
}
panic(err)
}
}
func updateStatistics(cfg *config.Config, startTime time.Time) {
metadata.PersistStatistics(cfg, time.Since(startTime))
}
func registerLicense(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) {
lc, err := bc.RegisterLicenseKey(*opt.LicenseKey, statistics)
if err != nil {
console.Exit("License registration failed: " + err.Error())
} else {
metadata.SaveLicense(*lc)
console.Exit("License successfully verified, Sampler can be restarted without --license flag now. Thank you.")
}
}
func verifyLicense(license *metadata.License, bc *client.BackendClient) {
verifiedLicense, _ := bc.VerifyLicenseKey(*license.Key)
if verifiedLicense != nil {
metadata.SaveLicense(*verifiedLicense)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Chinese_pastoral_person/Sampler.git
git@gitee.com:Chinese_pastoral_person/Sampler.git
Chinese_pastoral_person
Sampler
Sampler
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385