2021-07-30 22:29:20 +00:00
package cmd
import (
"errors"
"fmt"
2021-08-02 19:55:04 +00:00
"image"
2021-07-30 22:29:20 +00:00
"os"
"time"
2023-01-16 02:11:07 +00:00
config2 "tuxpa.in/t/erm/app/darktile/config"
gui2 "tuxpa.in/t/erm/app/darktile/gui"
"tuxpa.in/t/erm/app/darktile/hinters"
termutil2 "tuxpa.in/t/erm/app/darktile/termutil"
"tuxpa.in/t/erm/app/darktile/version"
2021-07-30 22:29:20 +00:00
"github.com/spf13/cobra"
)
var rewriteConfig bool
var debugFile string
var initialCommand string
var shell string
var screenshotAfterMS int
var screenshotFilename string
var themePath string
var showVersion bool
var rootCmd = & cobra . Command {
Use : os . Args [ 0 ] ,
SilenceUsage : true ,
RunE : func ( c * cobra . Command , args [ ] string ) error {
if showVersion {
fmt . Println ( version . Version )
os . Exit ( 0 )
}
var startupErrors [ ] error
2023-01-16 02:11:07 +00:00
var fileNotFound * config2 . ErrorFileNotFound
2021-07-30 22:29:20 +00:00
2023-01-16 02:11:07 +00:00
conf , err := config2 . LoadConfig ( )
2021-07-30 22:29:20 +00:00
if err != nil {
if ! errors . As ( err , & fileNotFound ) {
startupErrors = append ( startupErrors , err )
}
2023-01-16 02:11:07 +00:00
conf = config2 . DefaultConfig ( )
2021-07-30 22:29:20 +00:00
}
if rewriteConfig {
if _ , err := conf . Save ( ) ; err != nil {
return fmt . Errorf ( "failed to write config file: %w" , err )
}
2021-08-02 19:55:04 +00:00
fmt . Println ( "Config written." )
return nil
2021-07-30 22:29:20 +00:00
}
2023-01-16 02:11:07 +00:00
var theme * termutil2 . Theme
2021-07-30 22:29:20 +00:00
if themePath != "" {
2023-01-16 02:11:07 +00:00
theme , err = config2 . LoadThemeFromPath ( conf , themePath )
2021-07-30 22:29:20 +00:00
if err != nil {
return fmt . Errorf ( "failed to load theme: %s" , err )
}
} else {
2023-01-16 02:11:07 +00:00
theme , err = config2 . LoadTheme ( conf )
2021-07-30 22:29:20 +00:00
if err != nil {
if ! errors . As ( err , & fileNotFound ) {
startupErrors = append ( startupErrors , err )
}
2023-01-16 02:11:07 +00:00
theme , err = config2 . DefaultTheme ( conf )
2021-07-30 22:29:20 +00:00
if err != nil {
return fmt . Errorf ( "failed to load default theme: %w" , err )
}
}
}
2023-01-16 02:11:07 +00:00
termOpts := [ ] termutil2 . Option {
termutil2 . WithTheme ( theme ) ,
2021-07-30 22:29:20 +00:00
}
if debugFile != "" {
2023-01-16 02:11:07 +00:00
termOpts = append ( termOpts , termutil2 . WithLogFile ( debugFile ) )
2021-07-30 22:29:20 +00:00
}
if shell != "" {
2023-01-16 02:11:07 +00:00
termOpts = append ( termOpts , termutil2 . WithShell ( shell ) )
2021-07-30 22:29:20 +00:00
}
if initialCommand != "" {
2023-01-16 02:11:07 +00:00
termOpts = append ( termOpts , termutil2 . WithInitialCommand ( initialCommand ) )
2021-07-30 22:29:20 +00:00
}
2023-01-16 02:11:07 +00:00
terminal := termutil2 . New ( termOpts ... )
2021-07-30 22:29:20 +00:00
2023-01-16 02:11:07 +00:00
options := [ ] gui2 . Option {
gui2 . WithFontDPI ( conf . Font . DPI ) ,
gui2 . WithFontSize ( conf . Font . Size ) ,
gui2 . WithFontFamily ( conf . Font . Family ) ,
gui2 . WithOpacity ( conf . Opacity ) ,
gui2 . WithLigatures ( conf . Font . Ligatures ) ,
2021-08-02 19:55:04 +00:00
}
if conf . Cursor . Image != "" {
img , err := getImageFromFilePath ( conf . Cursor . Image )
if err != nil {
startupErrors = append ( startupErrors , err )
} else {
2023-01-16 02:11:07 +00:00
options = append ( options , gui2 . WithCursorImage ( img ) )
2021-08-02 19:55:04 +00:00
}
2021-07-30 22:29:20 +00:00
}
if screenshotAfterMS > 0 {
2023-01-16 02:11:07 +00:00
options = append ( options , gui2 . WithStartupFunc ( func ( g * gui2 . GUI ) {
2021-07-30 22:29:20 +00:00
<- time . After ( time . Duration ( screenshotAfterMS ) * time . Millisecond )
g . RequestScreenshot ( screenshotFilename )
} ) )
}
// load all hinters
for _ , hinter := range hinters . All ( ) {
2023-01-16 02:11:07 +00:00
options = append ( options , gui2 . WithHinter ( hinter ) )
2021-07-30 22:29:20 +00:00
}
2023-01-16 02:11:07 +00:00
g , err := gui2 . New ( terminal , options ... )
2021-07-30 22:29:20 +00:00
if err != nil {
return err
}
for _ , err := range startupErrors {
g . ShowError ( err . Error ( ) )
}
return g . Run ( )
} ,
}
2021-08-02 19:55:04 +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
}
2021-07-30 22:29:20 +00:00
func Execute ( ) error {
rootCmd . Flags ( ) . BoolVar ( & showVersion , "version" , showVersion , "Show darktile version information and exit" )
rootCmd . Flags ( ) . BoolVar ( & rewriteConfig , "rewrite-config" , rewriteConfig , "Write the resultant config after parsing config files and merging with defauls back to the config file" )
rootCmd . Flags ( ) . StringVar ( & debugFile , "log-file" , debugFile , "Debug log file" )
rootCmd . Flags ( ) . StringVarP ( & shell , "shell" , "s" , shell , "Shell to launch terminal with - defaults to configured user shell" )
rootCmd . Flags ( ) . StringVarP ( & initialCommand , "command" , "c" , initialCommand , "Command to run when shell starts - use this with caution" )
rootCmd . Flags ( ) . IntVar ( & screenshotAfterMS , "screenshot-after-ms" , screenshotAfterMS , "Take a screenshot after this many milliseconds" )
rootCmd . Flags ( ) . StringVar ( & screenshotFilename , "screenshot-filename" , screenshotFilename , "Filename to store screenshot taken by --screenshot-after-ms" )
rootCmd . Flags ( ) . StringVar ( & themePath , "theme-path" , themePath , "Path to a theme file to use instead of the default" )
return rootCmd . Execute ( )
}