misc: small clean ups and fixes

fix: dry run command truncation by printing the command after bubbletea quits
Remove unnecessary startEncoding tea.Cmd
Capitalize model struct to Model
This commit is contained in:
2024-12-26 18:34:33 +02:00
parent fb390bbcbe
commit 2e1d175e98
3 changed files with 44 additions and 47 deletions
+19 -34
View File
@@ -31,7 +31,7 @@ type File struct {
Encode bool Encode bool
} }
type model struct { type Model struct {
IsDirectory bool IsDirectory bool
Path string Path string
CurrentFileName string CurrentFileName string
@@ -57,7 +57,9 @@ type model struct {
ErrQuitMessage string ErrQuitMessage string
} }
func initialModel(fileInfo os.FileInfo, absolutePath string) *model { // We're returning a pointer here so we can embed the tea.Program on the original model
// instead of a copy.
func initialModel(fileInfo os.FileInfo, absolutePath string) *Model {
s := spinner.New() s := spinner.New()
s.Spinner = spinner.Dot s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
@@ -83,7 +85,7 @@ func initialModel(fileInfo os.FileInfo, absolutePath string) *model {
} }
} }
return &model{ return &Model{
IsDirectory: fileInfo.IsDir(), IsDirectory: fileInfo.IsDir(),
CurrentFileName: fileInfo.Name(), CurrentFileName: fileInfo.Name(),
Path: absolutePath, Path: absolutePath,
@@ -102,7 +104,7 @@ func initialModel(fileInfo os.FileInfo, absolutePath string) *model {
} }
} }
func (m model) updateConfigFocusedOptions() { func (m Model) updateConfigFocusedOptions() {
for _, visCfg := range m.VisibleConfig { for _, visCfg := range m.VisibleConfig {
for i := range m.Config { for i := range m.Config {
if m.Config[i].Name == visCfg.Name { if m.Config[i].Name == visCfg.Name {
@@ -112,11 +114,11 @@ func (m model) updateConfigFocusedOptions() {
} }
} }
func (m model) Init() tea.Cmd { func (m Model) Init() tea.Cmd {
return tea.Batch(m.Spinner.Tick, m.statFiles) return tea.Batch(m.Spinner.Tick, m.statFiles)
} }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
switch msg := msg.(type) { switch msg := msg.(type) {
@@ -226,7 +228,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case parsedCfgMsg: case parsedCfgMsg:
m.Screen = Main m.Screen = Main
m.ParsedConfig = msg.parsedConfig m.ParsedConfig = msg.parsedConfig
return m, tea.Batch(m.Spinner.Tick, startEncoding) if m.DryRun {
return m, tea.Batch(tea.ExitAltScreen, tea.Quit)
}
return m, tea.Batch(m.Spinner.Tick, encodeVideo)
} }
case Main: case Main:
switch msg := msg.(type) { switch msg := msg.(type) {
@@ -242,12 +248,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
return m, tea.Sequence(tea.ExitAltScreen, tea.Quit) return m, tea.Sequence(tea.ExitAltScreen, tea.Quit)
} }
case encodingStartedMsg:
if m.DryRun {
return m, tea.Sequence(tea.ExitAltScreen, gracefullyQuit)
}
return m, encodeVideo
case encodeVideoMsg: case encodeVideoMsg:
m.CurrentFileName = filepath.Base(m.Files[len(m.Files)-1]) m.CurrentFileName = filepath.Base(m.Files[len(m.Files)-1])
@@ -319,7 +319,7 @@ func parseConfig(cfg []Config) ParsedConfig {
} }
} }
func FilesScreenView(m model) string { func FilesScreenView(m Model) string {
view := "" view := ""
// TODO: Create a nice view where we can select different videos and then select the encoding options and start // TODO: Create a nice view where we can select different videos and then select the encoding options and start
@@ -337,7 +337,7 @@ func FilesScreenView(m model) string {
return view return view
} }
func CfgScreenView(m model) string { func CfgScreenView(m Model) string {
view := "" view := ""
for i, cfg := range m.VisibleConfig { for i, cfg := range m.VisibleConfig {
@@ -379,7 +379,7 @@ func CfgScreenView(m model) string {
return view return view
} }
func MainScreenView(m model) string { func MainScreenView(m Model) string {
progress := "" progress := ""
if m.IsDirectory { if m.IsDirectory {
@@ -425,28 +425,13 @@ func formatEstimate(estimate int) string {
return fmt.Sprintf("%ds", estimate) return fmt.Sprintf("%ds", estimate)
} }
func (m model) View() string { func (m Model) View() string {
if m.Quitting {
if m.DryRun { if m.DryRun {
sb := strings.Builder{} return ""
for _, file := range m.Files {
parentDir := filepath.Dir(file)
fileName := filepath.Base(file)
extensionIndex := strings.LastIndex(fileName, ".")
newFileName := fileName[:extensionIndex]
extension := fileName[extensionIndex:]
outFileFullPath := filepath.Join(parentDir, newFileName+fmt.Sprintf("_[%s]_[%s]", m.ParsedConfig.VideoEncoder, m.ParsedConfig.AudioEncoder)+extension)
cmd := exec.Command("ffmpeg", buildFFmpegCmdArgs(file, outFileFullPath, m.ParsedConfig)...)
sb.WriteString(cmd.String())
sb.WriteByte('\n')
} }
return fmt.Sprintf("%s %s\n", Checkmark, sb.String()) if m.Quitting {
} else {
return fmt.Sprintf("%s %d/%d files encoded\n", Checkmark, m.FileCount-len(m.Files), m.FileCount) return fmt.Sprintf("%s %d/%d files encoded\n", Checkmark, m.FileCount-len(m.Files), m.FileCount)
}
} else if m.Cancelled { } else if m.Cancelled {
// TODO: should we clean up the file ourselves? // TODO: should we clean up the file ourselves?
return fmt.Sprintf("%s Encoding cancelled. Stopped ffmpeg process.\n Make sure to clean up the created file\n", X) return fmt.Sprintf("%s Encoding cancelled. Stopped ffmpeg process.\n Make sure to clean up the created file\n", X)
+3 -9
View File
@@ -30,7 +30,7 @@ type filesStatMsg struct {
files []string files []string
} }
func (m *model) statFiles() tea.Msg { func (m *Model) statFiles() tea.Msg {
if m.IsDirectory { if m.IsDirectory {
entries, _ := os.ReadDir(m.Path) entries, _ := os.ReadDir(m.Path)
@@ -70,12 +70,6 @@ func (m *model) statFiles() tea.Msg {
} }
} }
type encodingStartedMsg struct{}
func startEncoding() tea.Msg {
return encodingStartedMsg{}
}
type encodeVideoMsg struct{} type encodeVideoMsg struct{}
func encodeVideo() tea.Msg { func encodeVideo() tea.Msg {
@@ -93,7 +87,7 @@ type parsedCfgMsg struct {
dryRun bool dryRun bool
} }
func (m *model) parseConfig(dryRun bool) tea.Cmd { func (m *Model) parseConfig(dryRun bool) tea.Cmd {
m.DryRun = dryRun m.DryRun = dryRun
return func() tea.Msg { return func() tea.Msg {
@@ -120,7 +114,7 @@ type errQuitMsg struct {
msg string msg string
} }
func (m *model) cleanUp() tea.Msg { func (m *Model) cleanUp() tea.Msg {
if len(m.Files) == 0 { if len(m.Files) == 0 {
return tea.Quit() return tea.Quit()
} }
+19 -1
View File
@@ -46,11 +46,29 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
_, err = p.Run() final, err := p.Run()
if err != nil { if err != nil {
fmt.Println(err)
log.Fatal(err) log.Fatal(err)
} }
finalModel, _ := final.(Model)
if finalModel.DryRun {
for _, file := range finalModel.Files {
parentDir := filepath.Dir(file)
fileName := filepath.Base(file)
extensionIndex := strings.LastIndex(fileName, ".")
newFileName := fileName[:extensionIndex]
extension := fileName[extensionIndex:]
outFileFullPath := filepath.Join(parentDir, newFileName+fmt.Sprintf("_[%s]_[%s]", finalModel.ParsedConfig.VideoEncoder, finalModel.ParsedConfig.AudioEncoder)+extension)
cmd := exec.Command("ffmpeg", buildFFmpegCmdArgs(file, outFileFullPath, finalModel.ParsedConfig)...)
fmt.Println(fmt.Sprintf("%s %s", Checkmark, cmd.String()))
}
}
} }
func buildFFmpegCmdArgs(fullFilePath string, outFullFilePath string, cfg ParsedConfig, additionalArgs ...string) []string { func buildFFmpegCmdArgs(fullFilePath string, outFullFilePath string, cfg ParsedConfig, additionalArgs ...string) []string {