From 6b41f2a89c99742a216a892d5388e537c9c7e60d Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Mon, 21 Mar 2022 01:46:36 +0200 Subject: [PATCH] feat: add progress bars and show which file is encoding --- TODO.md | 3 +-- app.go | 72 ++++++++++++++++++++++++++++++++++++++--------------- go.mod | 8 +++--- go.sum | 3 +-- progress.go | 2 +- 5 files changed, 58 insertions(+), 30 deletions(-) diff --git a/TODO.md b/TODO.md index f2d89bd..4a0ee67 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,6 @@ ## TODO: use os.Exec. its much better cuz we can actually kill processes. ## TODO: kill the ffmpeg process on ctrl+c. (if terminal window terminates it'll probably leave a zombie process) -## TODO: fancy progress bars for single file and directory encoding. -## TODO: print the name of the current file being encoded e.g `Encoding "Video Example Test 1"...`. +## TODO: calculate total progress based on file sizes instead of counts of files for a more accurate progress percentage. ## TODO: customizable encoding settings, using tui elements to control and select them. (not gonna work on this in the near future) ## TODO: ask user if they want the old videos deleted. (probably not gonna work on this in the near future too) ## FIXME: tui is stuck forever if file is already encoded. diff --git a/app.go b/app.go index 5c10a4e..1d0e108 100644 --- a/app.go +++ b/app.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strings" + "github.com/charmbracelet/bubbles/progress" "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -16,15 +17,18 @@ import ( var checkmark = lipgloss.NewStyle().Foreground(lipgloss.Color("#22FF33")).Render("✔") type model struct { - IsDirectory bool - Path string - CurrentFileName string - FileCount int - Files []string - Spinner spinner.Model - Progress float64 - Program *tea.Program - Quitting bool + IsDirectory bool + Path string + CurrentFileName string + FileCount int + Files []string + Spinner spinner.Model + SingleFileProgressBar progress.Model + SingleFileProgress float64 + TotalProgressBar progress.Model + TotalProgress float64 + Program *tea.Program + Quitting bool } func initialModel(fileInfo os.FileInfo, absolutePath string) *model { @@ -33,14 +37,17 @@ func initialModel(fileInfo os.FileInfo, absolutePath string) *model { s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) return &model{ - IsDirectory: fileInfo.IsDir(), - CurrentFileName: fileInfo.Name(), - Path: absolutePath, - FileCount: 0, - Files: make([]string, 0), - Spinner: s, - Progress: 0.0, - Quitting: false, + IsDirectory: fileInfo.IsDir(), + CurrentFileName: fileInfo.Name(), + Path: absolutePath, + FileCount: 0, + Files: make([]string, 0), + Spinner: s, + SingleFileProgressBar: progress.New(progress.WithGradient("#1010ff", "#00ff00")), + SingleFileProgress: 0.0, + TotalProgressBar: progress.New(progress.WithDefaultGradient()), + TotalProgress: 0.0, + Quitting: false, } } @@ -63,6 +70,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, encodeVideo case encodeVideoMsg: + m.CurrentFileName = filepath.Base(m.Files[len(m.Files)-1]) + go func() { fullFilePath := m.Files[len(m.Files)-1] encode(fullFilePath, filepath.Base(fullFilePath), m.Program) @@ -78,11 +87,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, encodeVideo case updateProgress: - m.Progress = msg.progress - return m, nil + m.SingleFileProgress = msg.progress + m.TotalProgress = (1.0/float64(m.FileCount))*msg.progress + (1.0 / float64(m.FileCount) * float64(m.FileCount-len(m.Files))) + + singleProgressCmd := m.SingleFileProgressBar.SetPercent(msg.progress) + totalProgressCmd := m.TotalProgressBar.SetPercent(m.TotalProgress) + + return m, tea.Batch(singleProgressCmd, totalProgressCmd) case quitMsg: m.Quitting = true return m, tea.Quit + case progress.FrameMsg: + singleProgressModel, singleProgressCmd := m.SingleFileProgressBar.Update(msg) + m.SingleFileProgressBar = singleProgressModel.(progress.Model) + + totalProgressModel, totalProgressCmd := m.TotalProgressBar.Update(msg) + m.TotalProgressBar = totalProgressModel.(progress.Model) + + return m, tea.Batch(singleProgressCmd, totalProgressCmd) } m.Spinner, cmd = m.Spinner.Update(msg) @@ -95,7 +117,17 @@ func (m model) View() string { 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%.0f%% done", m.Spinner.View(), m.FileCount-len(m.Files), m.FileCount, m.Progress) + progress := "" + + if m.IsDirectory { + progress = fmt.Sprintf("%s %d/%d files encoded\nFile Progress: %s\n\nTotal Progress: %s", m.Spinner.View(), m.FileCount-len(m.Files), m.FileCount, m.SingleFileProgressBar.View(), m.TotalProgressBar.View()) + } else { + progress = fmt.Sprintf("%s %d/%d files encoded\n%s", m.Spinner.View(), m.FileCount-len(m.Files), m.FileCount, m.SingleFileProgressBar.View()) + } + + view := fmt.Sprintf("\nEncoding \"%s\"...\n%s", m.CurrentFileName, progress) + + return view } type initUi struct { diff --git a/go.mod b/go.mod index 50f431e..55b7b39 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,9 @@ module git.illusionman1212.tech/illusion/ffui go 1.16 require ( - github.com/charmbracelet/bubbles v0.10.3 // indirect - github.com/charmbracelet/bubbletea v0.20.0 // indirect - github.com/charmbracelet/lipgloss v0.5.0 // indirect + github.com/charmbracelet/bubbles v0.10.3 + github.com/charmbracelet/bubbletea v0.20.0 + github.com/charmbracelet/lipgloss v0.5.0 github.com/gabriel-vasile/mimetype v1.4.0 - github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f // indirect - github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 // indirect github.com/u2takey/ffmpeg-go v0.4.1 ) diff --git a/go.sum b/go.sum index 5b5b328..a9d09a7 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,7 @@ github.com/charmbracelet/bubbles v0.10.3/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPAL github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA= github.com/charmbracelet/bubbletea v0.20.0 h1:/b8LEPgCbNr7WWZ2LuE/BV1/r4t5PyYJtDb+J3vpwxc= github.com/charmbracelet/bubbletea v0.20.0/go.mod h1:zpkze1Rioo4rJELjRyGlm9T2YNou1Fm4LIJQSa5QMEM= +github.com/charmbracelet/harmonica v0.1.0 h1:lFKeSd6OAckQ/CEzPVd2mqj+YMEubQ/3FM2IYY3xNm0= github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao= github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM= github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8= @@ -23,7 +24,6 @@ github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmx github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= @@ -59,7 +59,6 @@ github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ github.com/panjf2000/ants/v2 v2.4.2/go.mod h1:f6F0NZVFsGCp5A7QW/Zj/m92atWwOkY0OIhFxRNFr4A= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= diff --git a/progress.go b/progress.go index e68e5e9..119d74c 100644 --- a/progress.go +++ b/progress.go @@ -74,7 +74,7 @@ func TempSock(totalDuration float64, teaP *tea.Program) string { progressFloat, err := strconv.ParseFloat(progress, 64) if err == nil { teaP.Send(updateProgress{ - progress: progressFloat * 100, + progress: progressFloat, }) } }