erm/app/darktile/cmd/root.go

75 lines
1.5 KiB
Go
Raw Permalink Normal View History

2021-07-30 22:29:20 +00:00
package cmd
import (
2023-01-16 02:55:18 +00:00
"context"
2021-07-30 22:29:20 +00:00
"fmt"
"image"
2024-02-07 23:43:18 +00:00
"log/slog"
2021-07-30 22:29:20 +00:00
"os"
2023-01-16 02:55:18 +00:00
2024-10-21 08:15:19 +00:00
"anime.bike/slogutil/slogenv"
2024-02-07 23:43:18 +00:00
"gfx.cafe/util/go/fxplus"
"github.com/lmittmann/tint"
"go.uber.org/fx"
"tuxpa.in/t/erm/app/darktile/config"
"tuxpa.in/t/erm/app/darktile/gui"
"tuxpa.in/t/erm/app/darktile/termutil"
2023-01-16 02:11:07 +00:00
"tuxpa.in/t/erm/app/darktile/version"
2021-07-30 22:29:20 +00:00
)
2023-01-16 02:55:18 +00:00
type Context struct {
context.Context
}
2021-07-30 22:29:20 +00:00
2023-01-16 02:55:18 +00:00
type Root struct {
2023-01-16 06:03:38 +00:00
Term Term `cmd:"" aliases:"t" default:"1" help:"launch term"`
ListFonts ListFonts `cmd:"" aliases:"lf" name:"list-fonts" help:"list fonts"`
2023-01-16 02:55:18 +00:00
}
2021-07-30 22:29:20 +00:00
2023-01-16 02:55:18 +00:00
type Term struct {
2024-02-07 23:43:18 +00:00
ShowVersion bool `name:"version" help:"Show erm version information and exit"`
2023-01-16 02:55:18 +00:00
}
2021-07-30 22:29:20 +00:00
2023-01-16 02:55:18 +00:00
func (r *Term) Run(ctx *Context) error {
if r.ShowVersion {
fmt.Println(version.Version)
return nil
}
2021-07-30 22:29:20 +00:00
2024-02-07 23:43:18 +00:00
fxApp := fx.New(
fxplus.WithLogger,
fx.Provide(
func() *slog.Logger {
return slog.New(tint.NewHandler(os.Stdout, &tint.Options{
AddSource: true,
2024-10-21 08:15:19 +00:00
Level: slogenv.EnvLevel(),
2024-02-07 23:43:18 +00:00
}))
},
fxplus.Context,
//
config.NewLark,
termutil.New,
gui.New,
),
fx.Invoke(func(g *gui.GUI, s fx.Shutdowner, l *slog.Logger) {
go func() {
if err := g.Run(s); err != nil && !fxplus.IsShutdownOrCancel(err) {
l.Error("fatal error running gui", "err", err)
s.Shutdown(fx.ExitCode(1))
}
s.Shutdown(fx.ExitCode(0))
}()
2023-01-16 02:55:18 +00:00
}))
2024-02-07 23:43:18 +00:00
fxApp.Run()
return nil
2021-07-30 22:29:20 +00:00
}
2023-01-16 06:03:38 +00:00
func getImageFromFilePath(filePath string) (image.Image, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
image, _, err := image.Decode(f)
return image, err
}