Files
hearth/package/gitea-kanban-tui/src/main.rs
T
yukkop 5b0ae6be5e
runner nix smoke / nix label and flake smoke (push) Failing after 1m20s
feat: kanban cli
2026-09-28 09:25:04 +00:00

126 lines
4.4 KiB
Rust

use std::error::Error;
use std::time::Duration;
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
use gitea_kanban_tui::api::{load_project_board, GiteaApi, GiteaClient};
use gitea_kanban_tui::app::{App, IssueAction, MoveAction};
use gitea_kanban_tui::config::Config;
use gitea_kanban_tui::operations;
use gitea_kanban_tui::terminal::TerminalGuard;
use gitea_kanban_tui::text::sanitize_terminal_text;
use gitea_kanban_tui::ui;
fn main() {
if let Err(error) = run() {
eprintln!("error: {}", sanitize_terminal_text(&error.to_string()));
std::process::exit(1);
}
}
fn run() -> Result<(), Box<dyn Error>> {
let config = Config::load()?;
let client = GiteaClient::new(&config)?;
let mut app = load_board(&client, &config)?;
let repository = format!("{}/{}", config.owner, config.repo);
let mut terminal = TerminalGuard::enter()?;
loop {
terminal
.terminal()
.draw(|frame| ui::draw(frame, &app, &repository))?;
if !event::poll(Duration::from_millis(250))? {
continue;
}
let Event::Key(key) = event::read()? else {
continue;
};
if key.kind != KeyEventKind::Press {
continue;
}
if app.editor.is_some() {
if let Some(action) = app.handle_editor_key(key) {
issue_action(&client, &config, &mut app, action);
}
continue;
}
match key.code {
KeyCode::Char('q') => break,
KeyCode::Left | KeyCode::Char('h') => app.focus_left(),
KeyCode::Right | KeyCode::Char('l') => app.focus_right(),
KeyCode::Up | KeyCode::Char('k') => app.focus_up(),
KeyCode::Down | KeyCode::Char('j') => app.focus_down(),
KeyCode::Char('H') => move_card(&client, &mut app, -1),
KeyCode::Char('L') => move_card(&client, &mut app, 1),
KeyCode::Char('n') => app.begin_create(),
KeyCode::Char('e') => app.begin_edit(),
KeyCode::Char('d') => app.begin_delete(),
KeyCode::Char('r') => match load_board(&client, &config) {
Ok(board) => app = board,
Err(error) => app.status = format!("Refresh failed: {error}"),
},
KeyCode::Char('?') => app.show_help = !app.show_help,
_ => {}
}
}
Ok(())
}
fn issue_action(client: &impl GiteaApi, config: &Config, app: &mut App, action: IssueAction) {
let retry_action = action.clone();
let message = match action {
IssueAction::Create {
project_id,
title,
body,
} => operations::create_project_issue(client, project_id, title, body)
.map(|issue| format!("Created issue #{}", issue.number)),
IssueAction::Edit {
issue_number,
payload,
} => operations::edit_issue(client, issue_number, &payload)
.map(|_| format!("Updated issue #{issue_number}")),
IssueAction::Delete { issue_number } => operations::delete_issue(client, issue_number)
.map(|_| format!("Deleted issue #{issue_number}")),
};
match message {
Ok(message) => match load_board(client, config) {
Ok(mut board) => {
board.status = message;
*app = board;
}
Err(error) => app.status = format!("Saved, refresh failed: {error}"),
},
Err(error) => {
app.restore_issue_action(retry_action);
app.status = format!("Issue operation failed: {error}");
}
}
}
fn load_board(client: &impl GiteaApi, config: &Config) -> Result<App, Box<dyn Error>> {
let board = load_project_board(client, config)?;
App::new_project(board.project, board.columns, board.issues_by_column)
.map_err(|error| error.into())
}
fn move_card(client: &impl GiteaApi, app: &mut App, offset: isize) {
let Some(request) = app.prepare_move(offset) else {
app.status = "Cannot move card beyond board edge".to_owned();
return;
};
let result = match &request.action {
MoveAction::Project {
issue_id,
project_id,
payload,
} => operations::move_project_issue(client, *project_id, *issue_id, payload),
};
match result {
Ok(()) => app.apply_move(request),
Err(error) => app.status = format!("Move failed: {error}"),
}
}