use crate::tui::command::CommandOption;
use crate::{
github,
tui::{command, event::AppEvent},
workflow::Workflow,
};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::collections::HashMap;
use std::sync::Arc;
use std::{
collections::HashSet,
fs,
path::{Path, PathBuf},
sync::mpsc::Sender,
};
use tokio_util::sync::CancellationToken;
const SPINNER_FRAMES: &[&str] = &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
const BITRATE_STEP: i32 = 64;
const BITRATE_MIN: i32 = 64;
const BITRATE_MAX: i32 = 640;
const COVER_SIZE_STEP: i32 = 100;
const COVER_SIZE_MIN: i32 = 100;
const COVER_SIZE_MAX: i32 = 2000;
const MAX_LOG_LINES: usize = 500;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Screen {
Home,
FilePicker,
Options,
Running,
}
#[derive(Debug, Clone)]
pub struct DirEntry {
pub path: PathBuf,
pub name: String,
pub is_dir: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusState {
Pending,
Running,
Done,
Failed,
}
#[derive(Debug, Clone)]
pub struct FileStatus {
pub path: PathBuf,
pub state: StatusState,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OptionsRow {
ForceOverwrite,
AudioBitrate,
CoverSize,
OutputDirectory,
ExecutionMode,
Start,
}
impl OptionsRow {
fn all() -> [Self; 6] {
[
Self::ForceOverwrite,
Self::AudioBitrate,
Self::CoverSize,
Self::OutputDirectory,
Self::ExecutionMode,
Self::Start,
]
}
fn from_index(index: usize) -> Self {
Self::all().get(index).copied().unwrap_or(Self::Start)
}
fn count() -> usize {
Self::all().len()
}
}
#[derive(Debug, Clone)]
pub struct RunOptions {
pub output_dir: PathBuf,
pub force: bool,
pub bitrate: u32,
pub cover_size: u32,
pub parallel: bool,
pub custom: HashMap<String, String>,
}
impl Default for RunOptions {
fn default() -> Self {
Self {
output_dir: PathBuf::from("out"),
force: false,
bitrate: 320,
cover_size: 600,
parallel: true,
custom: HashMap::new(),
}
}
}
#[derive(Debug)]
pub struct App {
pub screen: Screen,
pub commands: Vec<Arc<dyn command::TuiCommand>>,
pub home_index: usize,
pub selected_command: Option<usize>,
pub cwd: PathBuf,
pub entries: Vec<DirEntry>,
pub picker_index: usize,
pub selected_files: HashSet<PathBuf>,
pub options: RunOptions,
pub options_index: usize,
pub log: Vec<String>,
pub file_statuses: Vec<FileStatus>,
pub running: bool,
pub finished: bool,
pub succeeded: usize,
pub failed: usize,
pub should_quit: bool,
pub spinner_frame: usize,
pub ffmpeg_path: PathBuf,
pub parallel: bool,
pub cancel_token: Option<CancellationToken>,
pub residual_files: Vec<PathBuf>,
pub show_cleanup_prompt: bool,
pub editing_text: Option<(String, String, String)>,
}
impl App {
pub fn new(ffmpeg_path: Option<PathBuf>) -> Self {
let commands: Vec<Arc<dyn command::TuiCommand>> = vec![
Arc::new(Workflow::Ts2Mp4),
Arc::new(Workflow::Mkv2Mp3),
Arc::new(Workflow::Mp32Mp4),
Arc::new(Workflow::Vidwrap),
Arc::new(github::tui::GhContribCommand),
];
Self {
screen: Screen::Home,
commands,
home_index: 0,
selected_command: None,
cwd: std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
entries: Vec::new(),
picker_index: 0,
selected_files: HashSet::new(),
options: RunOptions::default(),
options_index: 0,
log: Vec::new(),
file_statuses: Vec::new(),
running: false,
finished: false,
succeeded: 0,
failed: 0,
should_quit: false,
spinner_frame: 0,
ffmpeg_path: ffmpeg_path.unwrap_or_else(|| PathBuf::from("ffmpeg")),
parallel: true,
cancel_token: None,
residual_files: Vec::new(),
show_cleanup_prompt: false,
editing_text: None,
}
}
pub fn tick(&mut self) {
self.spinner_frame = (self.spinner_frame + 1) % SPINNER_FRAMES.len();
}
pub fn spinner(&self) -> &'static str {
SPINNER_FRAMES[self.spinner_frame]
}
pub fn push_log(&mut self, line: String) {
self.log.push(line);
if self.log.len() > MAX_LOG_LINES {
let overflow = self.log.len() - MAX_LOG_LINES;
self.log.drain(..overflow);
}
}
pub fn file_started(&mut self, index: usize) {
if let Some(status) = self.file_statuses.get_mut(index) {
status.state = StatusState::Running;
}
}
pub fn file_done(&mut self, index: usize, ok: bool) {
if let Some(status) = self.file_statuses.get_mut(index) {
status.state = if ok {
StatusState::Done
} else {
StatusState::Failed
};
}
}
pub fn all_done(&mut self, succeeded: usize, failed: usize) {
self.running = false;
self.finished = true;
self.succeeded = succeeded;
self.failed = failed;
self.cancel_token = None;
self.push_log(format!("✔ Done: {succeeded} succeeded, {failed} failed"));
}
pub fn cancelled_with_residual(&mut self, files: Vec<PathBuf>) {
self.running = false;
self.finished = true;
self.residual_files = files;
self.show_cleanup_prompt = true;
self.cancel_token = None;
}
pub fn command_title(&self) -> &'static str {
self.selected_command
.map(|i| self.commands[i].title())
.unwrap_or("")
}
pub fn option_rows(&self) -> Vec<String> {
let mut rows = Vec::new();
let Some(cmd_idx) = self.selected_command else {
return vec!["Start".to_string()];
};
let cmd = &self.commands[cmd_idx];
for opt in cmd.options() {
match opt {
CommandOption::Toggle { label, .. } => {
let val = match label {
"Force overwrite" => self.options.force,
_ => self
.options
.custom
.get(label)
.map(|v| v == "true")
.unwrap_or(false),
};
rows.push(format!("{:<20}: {}", label, if val { "ON" } else { "OFF" }));
}
CommandOption::Numeric { label, unit, .. } => {
let val = match label {
"Audio bitrate" => self.options.bitrate.to_string(),
"Cover size" => self.options.cover_size.to_string(),
_ => "N/A".to_string(),
};
rows.push(format!("{:<20}: {} {}", label, val, unit));
}
CommandOption::Text {
label,
default,
placeholder: _,
} => {
let val = self
.options
.custom
.get(label)
.map(|s| s.as_str())
.unwrap_or(default);
let display = if val.is_empty() { "<empty>" } else { val };
rows.push(format!("{:<20}: {}", label, display));
}
}
}
if cmd.file_picker_mode() != command::FilePickerMode::NotNeeded {
rows.push(format!(
"{:<20}: {}",
"Output directory",
self.options.output_dir.display()
));
rows.push(format!(
"{:<20}: {}",
"Parallelism",
if self.options.parallel {
"Parallel"
} else {
"Sequential"
}
));
}
rows.push("Start".to_string());
rows
}
pub fn handle_key(&mut self, key: KeyEvent, tx: &Sender<AppEvent>) {
if self.is_global_quit(key) {
self.should_quit = true;
return;
}
if self.try_cancel_running_batch(key) {
return;
}
if let Some((label, mut buffer, placeholder)) = self.editing_text.take() {
match key.code {
KeyCode::Enter => {
let value = if buffer.trim().is_empty() {
placeholder
} else {
buffer
};
self.options.custom.insert(label, value);
}
KeyCode::Esc => {
}
KeyCode::Char(c) => {
buffer.push(c);
self.editing_text = Some((label, buffer, placeholder));
}
KeyCode::Backspace => {
buffer.pop();
self.editing_text = Some((label, buffer, placeholder));
}
_ => {
self.editing_text = Some((label, buffer, placeholder));
}
}
return;
}
match self.screen {
Screen::Home => self.handle_home(key),
Screen::FilePicker => self.handle_picker(key),
Screen::Options => self.handle_options(key, tx),
Screen::Running => self.handle_running(key),
}
}
}
impl App {
fn input_ext(&self) -> &'static str {
self.selected_command
.and_then(|i| match self.commands[i].file_picker_mode() {
command::FilePickerMode::Required { extension, .. } => Some(extension),
_ => None,
})
.unwrap_or("")
}
fn handle_home(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
self.home_index = self.home_index.saturating_sub(1);
}
KeyCode::Down | KeyCode::Char('j') => {
if self.home_index + 1 < self.commands.len() {
self.home_index += 1;
}
}
KeyCode::Enter => self.select_current_command(),
KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
_ => {}
}
}
fn handle_picker(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
self.picker_index = self.picker_index.saturating_sub(1);
}
KeyCode::Down | KeyCode::Char('j') => {
if self.picker_index + 1 < self.entries.len() {
self.picker_index += 1;
}
}
KeyCode::Char(' ') => self.toggle_current(),
KeyCode::Char('a') if key.modifiers.contains(KeyModifiers::CONTROL) => {
self.select_all_files()
}
KeyCode::Backspace | KeyCode::Char('h') => self.go_parent(),
KeyCode::Enter | KeyCode::Char('l') => self.enter_current_or_confirm(),
KeyCode::Char('b') | KeyCode::Esc => self.screen = Screen::Home,
KeyCode::Char('q') => self.should_quit = true,
_ => {}
}
}
fn handle_options(&mut self, key: KeyEvent, tx: &Sender<AppEvent>) {
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
self.options_index = self.options_index.saturating_sub(1);
}
KeyCode::Down | KeyCode::Char('j') => {
self.options_index =
(self.options_index + 1).min(self.option_rows().len().saturating_sub(1));
}
KeyCode::Left => self.adjust_current(-1),
KeyCode::Right => self.adjust_current(1),
KeyCode::Enter | KeyCode::Char(' ') => self.activate_current(tx),
KeyCode::Char('b') | KeyCode::Esc => {
if self
.selected_command
.map(|i| self.commands[i].file_picker_mode())
== Some(command::FilePickerMode::NotNeeded)
{
self.screen = Screen::Home;
} else {
self.screen = Screen::FilePicker;
}
}
KeyCode::Char('q') => self.should_quit = true,
_ => {}
}
}
fn handle_running(&mut self, key: KeyEvent) {
if self.show_cleanup_prompt {
match key.code {
KeyCode::Char('y') | KeyCode::Enter => self.handle_cleanup_choice(true),
KeyCode::Char('n') => self.handle_cleanup_choice(false),
_ => {}
}
return;
}
if self.running {
return;
}
match key.code {
KeyCode::Enter | KeyCode::Esc => self.reset_to_home(),
KeyCode::Char('q') => self.should_quit = true,
_ => {}
}
}
fn load_directory(&mut self, path: &Path) {
let mut entries = Vec::new();
if let Ok(read_dir) = fs::read_dir(path) {
for entry in read_dir.flatten() {
let p = entry.path();
if self.should_show_entry(&p) {
entries.push(DirEntry {
name: entry.file_name().to_string_lossy().into_owned(),
path: p,
is_dir: entry.file_type().map(|t| t.is_dir()).unwrap_or(false),
});
}
}
}
entries.sort_by(|a, b| match (a.is_dir, b.is_dir) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.name.cmp(&b.name),
});
self.cwd = path.to_path_buf();
self.entries = entries;
self.picker_index = 0;
}
fn go_parent(&mut self) {
if let Some(parent) = self.cwd.parent().map(Path::to_path_buf) {
self.load_directory(&parent);
}
}
fn toggle_current(&mut self) {
let Some(entry) = self.entries.get(self.picker_index) else {
return;
};
if entry.is_dir {
return;
}
let path = entry.path.clone();
if !self.selected_files.remove(&path) {
self.selected_files.insert(path);
}
}
fn select_all_files(&mut self) {
for entry in &self.entries {
if !entry.is_dir {
self.selected_files.insert(entry.path.clone());
}
}
}
fn confirm_selection(&mut self) {
self.select_current_file_if_needed();
if !self.selected_files.is_empty()
|| self
.selected_command
.map(|i| self.commands[i].file_picker_mode())
== Some(command::FilePickerMode::NotNeeded)
{
self.options_index = 0;
self.screen = Screen::Options;
}
}
fn adjust_current(&mut self, delta: i32) {
let rows = self.option_rows();
let Some(row_text) = rows.get(self.options_index) else {
return;
};
if row_text.starts_with("Audio bitrate") {
self.options.bitrate = adjust_u32(
self.options.bitrate,
delta,
BITRATE_STEP,
BITRATE_MIN,
BITRATE_MAX,
);
} else if row_text.starts_with("Cover size") {
self.options.cover_size = adjust_u32(
self.options.cover_size,
delta,
COVER_SIZE_STEP,
COVER_SIZE_MIN,
COVER_SIZE_MAX,
);
} else if row_text.starts_with("Parallelism") {
self.options.parallel = !self.options.parallel;
}
}
fn activate_current(&mut self, tx: &Sender<AppEvent>) {
let rows = self.option_rows();
let Some(row_text) = rows.get(self.options_index) else {
return;
};
if row_text.starts_with("Force overwrite") || row_text.starts_with("Skip README") {
let label = row_text.split(':').next().unwrap().trim();
let is_on = row_text.contains("ON");
self.options
.custom
.insert(label.to_string(), (!is_on).to_string());
if label == "Force overwrite" {
self.options.force = !is_on;
}
} else if row_text == "Start" {
self.start_run(tx);
} else if row_text.starts_with("Output directory") {
} else if row_text.starts_with("Parallelism") {
self.options.parallel = !self.options.parallel;
} else {
let label = row_text.split(':').next().unwrap().trim();
let current = self.options.custom.get(label).cloned().unwrap_or_default();
let placeholder = self
.selected_command
.and_then(|i| {
self.commands[i].options().into_iter()
.find(|opt| matches!(opt, CommandOption::Text { label: l, .. } if *l == label))
.and_then(|opt| match opt {
CommandOption::Text { placeholder, .. } => Some(placeholder),
_ => None,
})
})
.unwrap_or("");
self.editing_text = Some((label.to_string(), current, placeholder.to_string()));
}
}
pub fn handle_cleanup_choice(&mut self, remove: bool) {
if remove {
for path in &self.residual_files {
let _ = fs::remove_file(path);
}
self.push_log(format!(
"🗑️ Removed {} residual files.",
self.residual_files.len()
));
} else {
self.push_log(format!(
"📁 Kept {} residual files.",
self.residual_files.len()
));
}
self.residual_files.clear();
self.show_cleanup_prompt = false;
}
fn start_run(&mut self, tx: &Sender<AppEvent>) {
let Some(cmd_idx) = self.selected_command else {
return;
};
let command = self.commands[cmd_idx].clone();
let files = self.selected_files_sorted();
let cancel = CancellationToken::new();
self.prepare_run(&files, cancel.clone());
crate::tui::runner::spawn_worker(
tx.clone(),
self.ffmpeg_path.clone(),
command,
files,
self.options.clone(),
cancel,
);
}
fn is_global_quit(&self, key: KeyEvent) -> bool {
key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c')
}
fn try_cancel_running_batch(&mut self, key: KeyEvent) -> bool {
if self.screen != Screen::Running || !self.running || key.code != KeyCode::Char('c') {
return false;
}
if let Some(token) = &self.cancel_token {
token.cancel();
self.push_log("⚠️ Cancellation requested...".into());
}
true
}
fn select_current_command(&mut self) {
let cmd_idx = self.home_index;
let current_dir = self.cwd.clone();
self.selected_command = Some(cmd_idx);
self.selected_files.clear();
self.log.clear();
self.finished = false;
self.reset_options_for_command();
if self.commands[cmd_idx].file_picker_mode() == command::FilePickerMode::NotNeeded {
self.screen = Screen::Options;
} else {
self.load_directory(¤t_dir);
self.screen = Screen::FilePicker;
}
}
fn reset_options_for_command(&mut self) {
self.options.custom.clear();
let Some(cmd_idx) = self.selected_command else {
return;
};
for option in self.commands[cmd_idx].options() {
if let CommandOption::Text { label, default, .. } = option {
self.options
.custom
.insert(label.to_string(), default.to_string());
}
}
}
fn enter_current_or_confirm(&mut self) {
let Some(entry) = self.entries.get(self.picker_index).cloned() else {
return;
};
if entry.is_dir {
self.load_directory(&entry.path);
} else {
self.confirm_selection();
}
}
fn should_show_entry(&self, path: &Path) -> bool {
if path.is_dir() {
return true;
}
let ext = self.input_ext();
if ext.is_empty() {
return false;
}
path.extension()
.and_then(|e| e.to_str())
.map(|e| e.eq_ignore_ascii_case(ext))
.unwrap_or(false)
}
fn select_current_file_if_needed(&mut self) {
if !self.selected_files.is_empty() {
return;
}
let Some(entry) = self.entries.get(self.picker_index) else {
return;
};
if !entry.is_dir {
self.selected_files.insert(entry.path.clone());
}
}
fn current_options_row(&self) -> OptionsRow {
OptionsRow::from_index(self.options_index)
}
fn selected_files_sorted(&self) -> Vec<PathBuf> {
let mut files: Vec<PathBuf> = self.selected_files.iter().cloned().collect();
files.sort();
files
}
fn prepare_run(&mut self, files: &[PathBuf], cancel: CancellationToken) {
self.cancel_token = Some(cancel);
self.file_statuses = files
.iter()
.map(|path| FileStatus {
path: path.clone(),
state: StatusState::Pending,
})
.collect();
self.log.clear();
self.running = true;
self.finished = false;
self.succeeded = 0;
self.failed = 0;
self.residual_files.clear();
self.show_cleanup_prompt = false;
self.screen = Screen::Running;
}
fn reset_to_home(&mut self) {
self.screen = Screen::Home;
self.finished = false;
self.show_cleanup_prompt = false;
self.residual_files.clear();
}
}
fn adjust_u32(current: u32, delta: i32, step: i32, min: i32, max: i32) -> u32 {
let next = current as i32 + delta * step;
next.clamp(min, max) as u32
}