440 lines
12 KiB
Rust
440 lines
12 KiB
Rust
mod core;
|
|
|
|
use std::{
|
|
io,
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
use crossterm::{
|
|
event::{self, Event, KeyCode, KeyEventKind},
|
|
execute,
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
};
|
|
use ratatui::{
|
|
layout::{Constraint, Layout, Rect, Spacing},
|
|
symbols::merge::MergeStrategy,
|
|
widgets::{Block, List, ListItem, ListState, Paragraph},
|
|
style::{Modifier, Style},
|
|
Frame, Terminal,
|
|
};
|
|
use ratatui::backend::CrosstermBackend;
|
|
|
|
#[derive(Clone)]
|
|
enum View {
|
|
Categories,
|
|
Placeholder(String), // temporary for testing navigation
|
|
Login,
|
|
}
|
|
|
|
#[derive(Clone, Copy)]
|
|
enum LoginFocus {
|
|
User,
|
|
Pass,
|
|
}
|
|
|
|
struct App {
|
|
should_exit: bool,
|
|
status: String,
|
|
config_status: String,
|
|
|
|
view_stack: Vec<View>,
|
|
|
|
items: Vec<String>,
|
|
selected: usize,
|
|
|
|
config: Option<core::config::Config>,
|
|
login_user: String,
|
|
login_pass: String,
|
|
login_focus: LoginFocus,
|
|
login_error: Option<String>,
|
|
}
|
|
|
|
impl Default for App {
|
|
fn default() -> Self {
|
|
let loaded = core::config::load_config().ok();
|
|
|
|
let (view_stack, config_status) = match &loaded {
|
|
Some(cfg) if cfg.access_token.is_some() => (
|
|
vec![View::Categories],
|
|
format!("auth: token present ({})", cfg.base_url),
|
|
),
|
|
Some(cfg) => (
|
|
vec![View::Login],
|
|
format!("auth: needs login ({})", cfg.base_url),
|
|
),
|
|
None => (
|
|
vec![View::Login],
|
|
"config: missing/invalid (need base_url)".to_string(),
|
|
),
|
|
};
|
|
|
|
Self {
|
|
should_exit: false,
|
|
status: "Press q to quit".to_string(),
|
|
config_status,
|
|
config: loaded,
|
|
|
|
view_stack,
|
|
|
|
items: vec![
|
|
"Shows".into(),
|
|
"Movies".into(),
|
|
"Music".into(),
|
|
"Playlists".into(),
|
|
"Collections".into(),
|
|
"Continue Watching".into(),
|
|
"Recently Added".into(),
|
|
"Downloads".into(),
|
|
"Settings".into(),
|
|
],
|
|
selected: 0,
|
|
|
|
login_user: String::new(),
|
|
login_pass: String::new(),
|
|
login_focus: LoginFocus::User,
|
|
login_error: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
impl App {
|
|
fn current_view(&self) -> &View {
|
|
self.view_stack.last().unwrap()
|
|
}
|
|
}
|
|
|
|
fn main() -> io::Result<()> {
|
|
// Terminal setup
|
|
enable_raw_mode()?;
|
|
let mut stdout = io::stdout();
|
|
execute!(stdout, EnterAlternateScreen)?;
|
|
let backend = CrosstermBackend::new(stdout);
|
|
let mut terminal = Terminal::new(backend)?;
|
|
terminal.clear()?;
|
|
|
|
let res = run_app(&mut terminal);
|
|
|
|
// Terminal restore (always)
|
|
disable_raw_mode()?;
|
|
execute!(io::stdout(), LeaveAlternateScreen)?;
|
|
terminal.show_cursor()?;
|
|
|
|
res
|
|
}
|
|
|
|
fn run_app(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<()> {
|
|
let tick_rate = Duration::from_millis(16); // ~60fps
|
|
let mut last_tick = Instant::now();
|
|
let mut app = App::default();
|
|
|
|
while !app.should_exit {
|
|
terminal.draw(|f| ui(f, &app))?;
|
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
|
if event::poll(timeout)? {
|
|
if let Event::Key(key) = event::read()? {
|
|
if key.kind == KeyEventKind::Press {
|
|
handle_key(&mut app, key.code);
|
|
}
|
|
}
|
|
}
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
// tick-based updates go here later (smooth nav, download polling, etc.)
|
|
last_tick = Instant::now();
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn handle_key(app: &mut App, code: KeyCode) {
|
|
// Special input handling in Login view
|
|
if matches!(app.current_view(), View::Login) {
|
|
match code {
|
|
KeyCode::Char('q') => { app.should_exit = true; return; }
|
|
KeyCode::Esc => { app.should_exit = true; return; }
|
|
|
|
KeyCode::Tab => {
|
|
app.login_focus = match app.login_focus {
|
|
LoginFocus::User => LoginFocus::Pass,
|
|
LoginFocus::Pass => LoginFocus::User,
|
|
};
|
|
return;
|
|
}
|
|
|
|
KeyCode::Backspace => {
|
|
match app.login_focus {
|
|
LoginFocus::User => { app.login_user.pop(); }
|
|
LoginFocus::Pass => { app.login_pass.pop(); }
|
|
}
|
|
return;
|
|
}
|
|
|
|
KeyCode::Enter => {
|
|
let cfg = match &app.config {
|
|
Some(c) => c.clone(),
|
|
None => {
|
|
app.login_error = Some("No config loaded. Create config.toml with base_url.".to_string());
|
|
return;
|
|
}
|
|
};
|
|
|
|
let client = core::jellyfin::JellyfinClient::from_config(&cfg);
|
|
match client.login_by_name(&app.login_user, &app.login_pass) {
|
|
Ok(res) => {
|
|
let mut new_cfg = cfg;
|
|
new_cfg.access_token = Some(res.access_token);
|
|
new_cfg.user_id = res.user_id;
|
|
|
|
if let Err(e) = core::config::save_config(&new_cfg) {
|
|
app.login_error = Some(format!("login ok but failed to write config: {e:?}"));
|
|
return;
|
|
}
|
|
|
|
app.config_status = format!("auth: token present ({})", new_cfg.base_url);
|
|
app.config = Some(new_cfg);
|
|
app.login_error = None;
|
|
app.login_pass.clear(); // don't keep password around
|
|
app.view_stack = vec![View::Categories];
|
|
app.status = "Logged in".to_string();
|
|
}
|
|
Err(e) => {
|
|
app.login_error = Some(e);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
KeyCode::Char(c) => {
|
|
match app.login_focus {
|
|
LoginFocus::User => app.login_user.push(c),
|
|
LoginFocus::Pass => app.login_pass.push(c),
|
|
}
|
|
return;
|
|
}
|
|
|
|
_ => return,
|
|
}
|
|
}
|
|
|
|
match code {
|
|
KeyCode::Char('q') => app.should_exit = true,
|
|
|
|
KeyCode::Enter => {
|
|
match app.current_view() {
|
|
View::Categories => {
|
|
let selected = app.items[app.selected].clone();
|
|
app.view_stack.push(View::Placeholder(selected.clone()));
|
|
app.status = format!("Entered {}", selected);
|
|
app.selected = 0;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
KeyCode::Esc | KeyCode::Backspace => {
|
|
if app.view_stack.len() > 1 {
|
|
app.view_stack.pop();
|
|
app.status = "Went back".to_string();
|
|
app.selected = 0;
|
|
}
|
|
}
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => {
|
|
if app.items.is_empty() {
|
|
return;
|
|
}
|
|
if app.selected == 0 {
|
|
app.selected = app.items.len() - 1;
|
|
} else {
|
|
app.selected -= 1;
|
|
}
|
|
app.status = format!("Selected: {}", app.items[app.selected]);
|
|
}
|
|
|
|
KeyCode::Down | KeyCode::Char('j') => {
|
|
if app.items.is_empty() {
|
|
return;
|
|
}
|
|
app.selected = (app.selected + 1) % app.items.len();
|
|
app.status = format!("Selected: {}", app.items[app.selected]);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn ui(frame: &mut Frame, app: &App) {
|
|
let area = frame.area();
|
|
let panes = layout(area);
|
|
|
|
// Top
|
|
frame.render_widget(Block::bordered().title("cj-jftui"), panes.top);
|
|
|
|
// Main
|
|
|
|
match app.current_view() {
|
|
View::Categories => {
|
|
render_categories(frame, app, panes.left_main, panes.right_main);
|
|
}
|
|
View::Placeholder(name) => {
|
|
render_placeholder(frame, name, panes.left_main, panes.right_main);
|
|
}
|
|
View::Login => {
|
|
render_login(frame, app, panes.left_main, panes.right_main);
|
|
}
|
|
}
|
|
|
|
// Bottom
|
|
frame.render_widget(
|
|
Paragraph::new(app.status.as_str())
|
|
.block(Block::bordered().title("Status").merge_borders(MergeStrategy::Exact)),
|
|
panes.left_output,
|
|
);
|
|
frame.render_widget(
|
|
Paragraph::new(app.config_status.as_str())
|
|
.block(Block::bordered().title("Info").merge_borders(MergeStrategy::Exact)),
|
|
panes.right_output,
|
|
);
|
|
}
|
|
|
|
fn compute_window(len: usize, selected: usize, viewport: usize) -> (usize, usize) {
|
|
if len == 0 || viewport == 0 {
|
|
return (0, 0);
|
|
}
|
|
|
|
let viewport = viewport.min(len);
|
|
let half = viewport / 2;
|
|
|
|
let mut start = selected.saturating_sub(half);
|
|
if start + viewport > len {
|
|
start = len - viewport;
|
|
}
|
|
|
|
let end = start + viewport;
|
|
(start, end)
|
|
}
|
|
|
|
|
|
fn render_categories(frame: &mut Frame, app: &App, left: Rect, right: Rect) {
|
|
// how many rows can we show inside the bordered block?
|
|
let viewport = left.height.saturating_sub(2) as usize;
|
|
|
|
let len = app.items.len();
|
|
let selected = app.selected.min(len.saturating_sub(1));
|
|
|
|
let (start, end) = compute_window(len, selected, viewport);
|
|
|
|
let items: Vec<ListItem> = app.items[start..end]
|
|
.iter()
|
|
.map(|s| ListItem::new(s.as_str()))
|
|
.collect();
|
|
|
|
let list = List::new(items)
|
|
.block(Block::bordered().title("Categories").merge_borders(MergeStrategy::Exact))
|
|
.highlight_symbol("> ")
|
|
.highlight_style(Style::default().add_modifier(Modifier::REVERSED));
|
|
|
|
let mut state = ListState::default();
|
|
state.select(Some(selected - start)); // selection relative to slice
|
|
|
|
frame.render_stateful_widget(list, left, &mut state);
|
|
|
|
// Right pane details (use the real selected item)
|
|
let detail = if len > 0 {
|
|
format!("Details for:\n\n{}", app.items[selected])
|
|
} else {
|
|
"No items".to_string()
|
|
};
|
|
|
|
frame.render_widget(
|
|
Paragraph::new(detail)
|
|
.block(Block::bordered().title("Details").merge_borders(MergeStrategy::Exact)),
|
|
right,
|
|
);
|
|
}
|
|
|
|
fn render_placeholder(frame: &mut Frame, name: &str, left: Rect, right: Rect) {
|
|
frame.render_widget(
|
|
Paragraph::new(format!("Inside {}\n\nPress Esc to go back", name))
|
|
.block(Block::bordered().title("Library").merge_borders(MergeStrategy::Exact)),
|
|
left,
|
|
);
|
|
|
|
frame.render_widget(
|
|
Paragraph::new("Right pane reserved for item details / thumbnails")
|
|
.block(Block::bordered().title("Details").merge_borders(MergeStrategy::Exact)),
|
|
right,
|
|
);
|
|
}
|
|
|
|
struct Panes {
|
|
top: Rect,
|
|
left_main: Rect,
|
|
right_main: Rect,
|
|
left_output: Rect,
|
|
right_output: Rect,
|
|
}
|
|
|
|
fn layout(area: Rect) -> Panes {
|
|
use Constraint::Fill;
|
|
|
|
let [top, bottom] =
|
|
Layout::vertical([Fill(1), Fill(12)]).areas(area);
|
|
|
|
let [main_area, output_area] =
|
|
Layout::vertical([Fill(9), Fill(3)])
|
|
.spacing(Spacing::Overlap(1))
|
|
.areas(bottom);
|
|
|
|
let [left_main, right_main] =
|
|
Layout::horizontal([Fill(2), Fill(5)])
|
|
.spacing(Spacing::Overlap(1))
|
|
.areas(main_area);
|
|
|
|
let [left_output, right_output] =
|
|
Layout::horizontal([Fill(5), Fill(1)])
|
|
.spacing(Spacing::Overlap(1))
|
|
.areas(output_area);
|
|
|
|
Panes {
|
|
top,
|
|
left_main,
|
|
right_main,
|
|
left_output,
|
|
right_output,
|
|
}
|
|
}
|
|
|
|
|
|
fn render_login(frame: &mut Frame, app: &App, left: Rect, right: Rect) {
|
|
let focus = match app.login_focus {
|
|
LoginFocus::User => "username",
|
|
LoginFocus::Pass => "password",
|
|
};
|
|
|
|
let masked = "*".repeat(app.login_pass.len());
|
|
|
|
let left_text = format!(
|
|
"Login (Tab switch, Enter submit)\n\nusername: {}\npassword: {}\n\nfocus: {}\n",
|
|
app.login_user, masked, focus
|
|
);
|
|
|
|
frame.render_widget(
|
|
Paragraph::new(left_text)
|
|
.block(Block::bordered().title("Login").merge_borders(MergeStrategy::Exact)),
|
|
left,
|
|
);
|
|
|
|
let right_text = match &app.login_error {
|
|
Some(e) => format!("Error:\n{e}"),
|
|
None => "Enter Jellyfin credentials.\nToken will be stored in config.toml.".to_string(),
|
|
};
|
|
|
|
frame.render_widget(
|
|
Paragraph::new(right_text)
|
|
.block(Block::bordered().title("Info").merge_borders(MergeStrategy::Exact)),
|
|
right,
|
|
);
|
|
}
|