2022-03-19 02:35:23 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2022-03-21 01:46:36 +02:00
|
|
|
"github.com/charmbracelet/bubbles/progress"
|
2022-03-19 02:35:23 +02:00
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
type Screen int
|
|
|
|
|
type FfuiBool int
|
|
|
|
|
|
2022-03-20 15:51:34 +02:00
|
|
|
var checkmark = lipgloss.NewStyle().Foreground(lipgloss.Color("#22FF33")).Render("✔")
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
var Configs = []Config{
|
|
|
|
|
{Name: "Delete unencoded video(s)?", Opts: []string{"No", "Yes"}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
Cfg Screen = iota
|
|
|
|
|
Main
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
False FfuiBool = iota
|
|
|
|
|
True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
|
Name string
|
|
|
|
|
Opts []string
|
|
|
|
|
FocusedOption int
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 02:35:23 +02:00
|
|
|
type model struct {
|
2022-03-21 01:46:36 +02:00
|
|
|
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
|
2022-10-07 06:07:44 +03:00
|
|
|
Screen Screen
|
|
|
|
|
Config map[int]Config
|
|
|
|
|
FocusIndex int
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initialModel(fileInfo os.FileInfo, absolutePath string) *model {
|
|
|
|
|
s := spinner.New()
|
|
|
|
|
s.Spinner = spinner.Dot
|
|
|
|
|
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
cfg := make(map[int]Config)
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(Configs); i++ {
|
|
|
|
|
cfg[i] = Configs[i]
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 02:35:23 +02:00
|
|
|
return &model{
|
2022-03-21 01:46:36 +02:00
|
|
|
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,
|
2022-10-07 06:07:44 +03:00
|
|
|
Config: cfg,
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
2022-10-07 06:07:44 +03:00
|
|
|
return tea.Batch(m.Spinner.Tick, initCfg)
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
|
switch msg.String() {
|
|
|
|
|
case "ctrl+c":
|
|
|
|
|
return m, tea.Quit
|
|
|
|
|
}
|
2022-10-07 06:07:44 +03:00
|
|
|
}
|
2022-03-19 02:35:23 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
switch m.Screen {
|
|
|
|
|
case Cfg:
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
|
key := msg.String()
|
|
|
|
|
switch key {
|
|
|
|
|
case "enter", " ":
|
|
|
|
|
// switch to main screen if we're focused on the start button
|
|
|
|
|
if m.FocusIndex == len(Configs) {
|
|
|
|
|
m.Screen = Main
|
|
|
|
|
return m, tea.Batch(m.Spinner.Tick, m.statFile)
|
|
|
|
|
}
|
|
|
|
|
case "tab", "shift+tab", "up", "down":
|
|
|
|
|
if key == "up" || key == "shift+tab" {
|
|
|
|
|
m.FocusIndex--
|
|
|
|
|
} else {
|
|
|
|
|
m.FocusIndex++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: make this len(Configs) + 1 after start button is added and change the else statement accordingly
|
|
|
|
|
if m.FocusIndex >= len(Configs) {
|
|
|
|
|
m.FocusIndex = 0
|
|
|
|
|
} else if m.FocusIndex < 0 {
|
|
|
|
|
m.FocusIndex = len(Configs) - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: move to start button
|
|
|
|
|
case "left", "right":
|
|
|
|
|
if cfg, ok := m.Config[m.FocusIndex]; ok {
|
|
|
|
|
if key == "right" {
|
|
|
|
|
cfg.FocusedOption++
|
|
|
|
|
} else {
|
|
|
|
|
cfg.FocusedOption--
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cfg.FocusedOption >= len(cfg.Opts) {
|
|
|
|
|
cfg.FocusedOption = 0
|
|
|
|
|
} else if cfg.FocusedOption < 0 {
|
|
|
|
|
cfg.FocusedOption = len(cfg.Opts) - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.Config[m.FocusIndex] = cfg
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case Main:
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case initUi:
|
|
|
|
|
m.FileCount = msg.fileCount
|
|
|
|
|
m.Files = msg.files
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
|
case finishedEncodingVideo:
|
|
|
|
|
m.Files = m.Files[:len(m.Files)-1]
|
|
|
|
|
|
|
|
|
|
if len(m.Files) == 0 {
|
|
|
|
|
return m, m.gracefullyQuit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m, encodeVideo
|
|
|
|
|
case updateProgress:
|
|
|
|
|
m.SingleFileProgress = msg.progress
|
|
|
|
|
m.TotalProgress = (1.0/float64(m.FileCount))*msg.progress + (1.0 / float64(m.FileCount) * float64(m.FileCount-len(m.Files)))
|
2022-03-21 01:46:36 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
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)
|
2022-03-19 02:35:23 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
totalProgressModel, totalProgressCmd := m.TotalProgressBar.Update(msg)
|
|
|
|
|
m.TotalProgressBar = totalProgressModel.(progress.Model)
|
2022-03-19 02:35:23 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
return m, tea.Batch(singleProgressCmd, totalProgressCmd)
|
2022-03-20 15:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
m.Spinner, cmd = m.Spinner.Update(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m, cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CfgScreenView(m model) string {
|
|
|
|
|
view := ""
|
2022-03-21 01:46:36 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
for i, cfg := range Configs {
|
|
|
|
|
opts := ""
|
2022-03-21 01:46:36 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
for j, opt := range cfg.Opts {
|
|
|
|
|
if m.Config[i].FocusedOption == j {
|
|
|
|
|
opts += FocusedOption.Render(opt)
|
|
|
|
|
} else {
|
|
|
|
|
opts += BlurredOption.Render(opt)
|
|
|
|
|
}
|
2022-03-21 01:46:36 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
if j != len(cfg.Opts)-1 {
|
|
|
|
|
opts += ", "
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-21 01:46:36 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
if i == m.FocusIndex {
|
|
|
|
|
view += fmt.Sprintf("%s { %s }\n", FocusedConfig.Render(cfg.Name), opts)
|
|
|
|
|
} else {
|
|
|
|
|
view += fmt.Sprintf("%s { %s }\n", BlurredConfig.Render(cfg.Name), opts)
|
|
|
|
|
}
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
// TODO: add start button
|
2022-03-19 02:35:23 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
return view
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
func MainScreenView(m model) string {
|
2022-03-21 01:46:36 +02:00
|
|
|
progress := ""
|
|
|
|
|
|
|
|
|
|
if m.IsDirectory {
|
2022-10-07 02:45:32 +03:00
|
|
|
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())
|
2022-03-21 01:46:36 +02:00
|
|
|
} else {
|
2022-10-07 02:45:32 +03:00
|
|
|
progress = fmt.Sprintf("%s %d/%d files encoded\n%s",
|
|
|
|
|
m.Spinner.View(),
|
|
|
|
|
m.FileCount-len(m.Files),
|
|
|
|
|
m.FileCount,
|
|
|
|
|
m.SingleFileProgressBar.View())
|
2022-03-21 01:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view := fmt.Sprintf("\nEncoding \"%s\"...\n%s", m.CurrentFileName, progress)
|
|
|
|
|
|
|
|
|
|
return view
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
func (m model) View() string {
|
|
|
|
|
if m.Quitting {
|
|
|
|
|
return fmt.Sprintf("%s %d/%d files encoded\n", checkmark, m.FileCount-len(m.Files), m.FileCount)
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
switch m.Screen {
|
|
|
|
|
case Cfg:
|
|
|
|
|
return CfgScreenView(m)
|
|
|
|
|
case Main:
|
|
|
|
|
return MainScreenView(m)
|
2022-03-19 02:35:23 +02:00
|
|
|
}
|
2022-03-20 15:51:34 +02:00
|
|
|
|
2022-10-07 06:07:44 +03:00
|
|
|
return "Error: Invalid Screen"
|
2022-03-20 15:51:34 +02:00
|
|
|
}
|