Compare commits
No commits in common. "c030984d5123e283580b059989aa39eafaad8adb" and "6cbba103257995bd6001b559f830c6ce9341f7ea" have entirely different histories.
c030984d51
...
6cbba10325
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,4 +4,3 @@
|
||||
/kanban.json
|
||||
/db.sqlite
|
||||
/kanban.db
|
||||
/sql/queries.sql
|
||||
|
@ -20,8 +20,7 @@ create table if not exists kb_column
|
||||
|
||||
create table if not exists setting
|
||||
(
|
||||
key text not null primary key,
|
||||
id integer primary key autoincrement,
|
||||
name text not null,
|
||||
value text not null
|
||||
);
|
||||
|
||||
insert into kb_column(name) values ("Todo"),("InProgress"),("Done"),("Ideas");
|
17
src/app.rs
17
src/app.rs
@ -2,7 +2,7 @@
|
||||
// use int_enum::IntEnum;
|
||||
use rusqlite::Connection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{cmp::min, error::Error};
|
||||
use std::cmp::min;
|
||||
use tui_textarea::TextArea;
|
||||
|
||||
use crate::db;
|
||||
@ -29,6 +29,7 @@ pub struct Task {
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Project {
|
||||
pub name: String,
|
||||
pub filepath: String,
|
||||
pub selected_column_idx: usize,
|
||||
pub columns: Vec<Column>,
|
||||
}
|
||||
@ -144,7 +145,7 @@ impl<'a> Column {
|
||||
}
|
||||
|
||||
pub fn select_last_task(&mut self) {
|
||||
self.selected_task_idx = self.tasks.len().saturating_sub(1);
|
||||
self.selected_task_idx = self.tasks.len() - 1;
|
||||
}
|
||||
|
||||
pub fn move_task_up(&mut self) -> bool {
|
||||
@ -159,7 +160,7 @@ impl<'a> Column {
|
||||
}
|
||||
|
||||
pub fn move_task_down(&mut self) -> bool {
|
||||
if self.selected_task_idx < self.tasks.len().saturating_sub(1) {
|
||||
if self.selected_task_idx < self.tasks.len() - 1 {
|
||||
self.tasks
|
||||
.swap(self.selected_task_idx, self.selected_task_idx + 1);
|
||||
self.selected_task_idx += 1;
|
||||
@ -181,16 +182,12 @@ impl<'a> Column {
|
||||
}
|
||||
|
||||
impl Project {
|
||||
/// .
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return an error if it has issues reading from the database
|
||||
pub fn load(conn: &Connection) -> Result<Self, Box<dyn Error>> {
|
||||
let columns = db::get_all_columns(conn)?;
|
||||
pub async fn load(pool: &Connection) -> Result<Self, KanbanError> {
|
||||
let columns = db::get_all_columns(&pool).unwrap();
|
||||
|
||||
Ok(Project {
|
||||
name: String::from("Kanban Board"),
|
||||
filepath: String::from("path"),
|
||||
columns,
|
||||
selected_column_idx: 0,
|
||||
})
|
||||
|
50
src/db.rs
50
src/db.rs
@ -1,11 +1,6 @@
|
||||
use crate::{Column, Task};
|
||||
use rusqlite::{params, Connection, Result};
|
||||
|
||||
/// .
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return an error if something is wrong with the SQL
|
||||
pub fn get_tasks_by_column(conn: &Connection, column_name: &String) -> Result<Vec<Task>> {
|
||||
let mut stmt = conn.prepare(
|
||||
r#"
|
||||
@ -16,7 +11,7 @@ pub fn get_tasks_by_column(conn: &Connection, column_name: &String) -> Result<Ve
|
||||
"#,
|
||||
)?;
|
||||
let mut tasks = Vec::new();
|
||||
let rows = stmt.query_map([column_name], |row| {
|
||||
let rows = stmt.query_map(&[column_name], |row| {
|
||||
Ok(Task {
|
||||
id: row.get(0)?,
|
||||
title: row.get(1)?,
|
||||
@ -29,11 +24,6 @@ pub fn get_tasks_by_column(conn: &Connection, column_name: &String) -> Result<Ve
|
||||
Ok(tasks)
|
||||
}
|
||||
|
||||
/// .
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return an error if there are issues with the SQL
|
||||
pub fn get_all_columns(conn: &Connection) -> Result<Vec<Column>> {
|
||||
let mut stmt = conn.prepare("select id, name from kb_column")?;
|
||||
let query_rows = stmt.query_map((), |row| {
|
||||
@ -43,11 +33,11 @@ pub fn get_all_columns(conn: &Connection) -> Result<Vec<Column>> {
|
||||
for row in query_rows {
|
||||
let r = &row?;
|
||||
let name = &r.1;
|
||||
let tasks = get_tasks_by_column(conn, name)?;
|
||||
let tasks = get_tasks_by_column(conn, &name).unwrap();
|
||||
let col = Column {
|
||||
id: r.0,
|
||||
name: name.clone(),
|
||||
tasks,
|
||||
tasks: tasks,
|
||||
selected_task_idx: 0,
|
||||
};
|
||||
columns.push(col);
|
||||
@ -55,11 +45,6 @@ pub fn get_all_columns(conn: &Connection) -> Result<Vec<Column>> {
|
||||
Ok(columns)
|
||||
}
|
||||
|
||||
/// .
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if something goes wrong with the SQL
|
||||
pub fn insert_new_task(
|
||||
conn: &Connection,
|
||||
title: String,
|
||||
@ -79,21 +64,11 @@ pub fn insert_new_task(
|
||||
}
|
||||
}
|
||||
|
||||
/// .
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if something goes wrong with the SQL
|
||||
pub fn delete_task(conn: &Connection, task: &Task) {
|
||||
let mut stmt = conn.prepare("delete from task where id = ?1").unwrap();
|
||||
stmt.execute([task.id]).unwrap();
|
||||
}
|
||||
|
||||
/// .
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if something goes wrong with the SQL
|
||||
pub fn update_task_text(conn: &Connection, task: &Task) {
|
||||
let mut stmt = conn
|
||||
.prepare("update task set title = ?2, description = ?3 where id = ?1")
|
||||
@ -102,38 +77,27 @@ pub fn update_task_text(conn: &Connection, task: &Task) {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// .
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if something goes wrong with the SQL
|
||||
pub fn move_task_to_column(conn: &Connection, task: &Task, target_column: &Column) {
|
||||
let mut stmt = conn
|
||||
.prepare(
|
||||
"update task
|
||||
set
|
||||
column_id = ?2,
|
||||
sort_order = coalesce(1 +
|
||||
sort_order = 1 +
|
||||
(select sort_order from task
|
||||
where column_id = ?2 order by sort_order desc limit 1),
|
||||
0)
|
||||
where column_id = ?2 order by sort_order desc limit 1)
|
||||
where task.id = ?1",
|
||||
)
|
||||
.unwrap();
|
||||
stmt.execute((&task.id, &target_column.id)).unwrap();
|
||||
}
|
||||
|
||||
/// .
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if something goes wrong with the SQL
|
||||
pub fn swap_task_order(conn: &mut Connection, task1: &Task, task2: &Task) {
|
||||
let tx = conn.transaction().unwrap();
|
||||
|
||||
tx.execute(
|
||||
"create temp table temp_order as select sort_order from task where id = ?1",
|
||||
[&task1.id],
|
||||
&[&task1.id],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@ -146,7 +110,7 @@ pub fn swap_task_order(conn: &mut Connection, task1: &Task, task2: &Task) {
|
||||
|
||||
tx.execute(
|
||||
"update task set sort_order = (select sort_order from temp_order) where id = ?1",
|
||||
[&task2.id],
|
||||
&[&task2.id],
|
||||
)
|
||||
.unwrap();
|
||||
tx.execute("drop table temp_order", ()).unwrap();
|
||||
|
59
src/input.rs
59
src/input.rs
@ -3,9 +3,16 @@ use crate::db;
|
||||
use crossterm::event;
|
||||
use crossterm::event::{Event, KeyCode};
|
||||
|
||||
pub fn handle_task_edit(state: &mut State<'_>, key: event::KeyEvent, mut task: TaskState<'_>) {
|
||||
/// # Errors
|
||||
///
|
||||
/// Crossterm `event::read()` might return an error
|
||||
pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> {
|
||||
let project = &mut state.project;
|
||||
let column = project.get_selected_column_mut();
|
||||
if let Event::Key(key) = event::read()? {
|
||||
match &mut state.task_edit_state {
|
||||
Some(task) => {
|
||||
// TODO: Extract this code to a separate function to avoid nesting
|
||||
match task.focus {
|
||||
// TODO: Handle wrapping around the enum rather than doing it manually
|
||||
TaskEditFocus::Title => match key.code {
|
||||
@ -33,10 +40,15 @@ pub fn handle_task_edit(state: &mut State<'_>, key: event::KeyEvent, mut task: T
|
||||
if let Some(selected_task) = column.get_selected_task_mut() {
|
||||
selected_task.title = title;
|
||||
selected_task.description = description;
|
||||
db::update_task_text(&state.db_conn, selected_task);
|
||||
db::update_task_text(&state.db_conn, &selected_task);
|
||||
}
|
||||
} else {
|
||||
let task = db::insert_new_task(&state.db_conn, title, description, column);
|
||||
let task = db::insert_new_task(
|
||||
&state.db_conn,
|
||||
title,
|
||||
description,
|
||||
&column,
|
||||
);
|
||||
column.add_task(task);
|
||||
}
|
||||
state.task_edit_state = None;
|
||||
@ -52,12 +64,8 @@ pub fn handle_task_edit(state: &mut State<'_>, key: event::KeyEvent, mut task: T
|
||||
_ => (),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn handle_main(state: &mut State<'_>, key: event::KeyEvent) {
|
||||
let project = &mut state.project;
|
||||
let column = project.get_selected_column_mut();
|
||||
match key.code {
|
||||
}
|
||||
None => match key.code {
|
||||
KeyCode::Char('q') => state.quit = true,
|
||||
KeyCode::Char('h') | KeyCode::Left => {
|
||||
project.select_previous_column();
|
||||
@ -70,62 +78,41 @@ pub fn handle_main(state: &mut State<'_>, key: event::KeyEvent) {
|
||||
KeyCode::Char('g') => column.select_first_task(),
|
||||
KeyCode::Char('G') => column.select_last_task(),
|
||||
KeyCode::Char('H') => {
|
||||
if !column.tasks.is_empty() {
|
||||
project.move_task_previous_column();
|
||||
let col = project.get_selected_column();
|
||||
let t = col.get_selected_task().unwrap();
|
||||
db::move_task_to_column(&state.db_conn, t, col);
|
||||
}
|
||||
db::move_task_to_column(&state.db_conn, &t, &col);
|
||||
}
|
||||
KeyCode::Char('L') => {
|
||||
if !column.tasks.is_empty() {
|
||||
project.move_task_next_column();
|
||||
let col = project.get_selected_column();
|
||||
let t = col.get_selected_task().unwrap();
|
||||
db::move_task_to_column(&state.db_conn, t, col);
|
||||
}
|
||||
db::move_task_to_column(&state.db_conn, &t, &col);
|
||||
}
|
||||
KeyCode::Char('J') => {
|
||||
if column.move_task_down() {
|
||||
let task1 = column.get_selected_task().unwrap();
|
||||
let task2 = column.get_previous_task().unwrap();
|
||||
db::swap_task_order(&mut state.db_conn, task1, task2);
|
||||
db::swap_task_order(&mut state.db_conn, &task1, &task2);
|
||||
}
|
||||
}
|
||||
KeyCode::Char('K') => {
|
||||
if column.move_task_up() {
|
||||
let task1 = column.get_selected_task().unwrap();
|
||||
let task2 = column.get_next_task().unwrap();
|
||||
db::swap_task_order(&mut state.db_conn, task1, task2);
|
||||
db::swap_task_order(&mut state.db_conn, &task1, &task2);
|
||||
}
|
||||
}
|
||||
KeyCode::Char('n') => state.task_edit_state = Some(TaskState::default()),
|
||||
KeyCode::Char('e') => {
|
||||
state.task_edit_state = column.get_task_state_from_curr_selected_task();
|
||||
state.task_edit_state = column.get_task_state_from_curr_selected_task()
|
||||
}
|
||||
KeyCode::Char('D') => {
|
||||
if !column.tasks.is_empty() {
|
||||
db::delete_task(&state.db_conn, column.get_selected_task().unwrap());
|
||||
column.remove_task();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// # Errors
|
||||
///
|
||||
/// Crossterm `event::read()` might return an error
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Shouldn't really panic because there are checks to ensure we can unwrap safely
|
||||
pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> {
|
||||
if let Event::Key(key) = event::read()? {
|
||||
if let Some(task) = state.task_edit_state.take() {
|
||||
handle_task_edit(state, key, task);
|
||||
} else {
|
||||
handle_main(state, key);
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
24
src/main.rs
24
src/main.rs
@ -15,34 +15,20 @@ use tui::Terminal;
|
||||
/// kanban-tui is a simple, interactive TUI based task manager using kanban columns
|
||||
pub struct CliArgs {
|
||||
#[arg(value_name="DATABASE", value_hint=FilePath, index=1)]
|
||||
/// Path to the SQLite database
|
||||
/// Path to the
|
||||
pub filepath: Option<PathBuf>,
|
||||
}
|
||||
|
||||
// TODO: We either make everything async or we remove the dependency
|
||||
#[async_std::main]
|
||||
async fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
||||
let dbpath = CliArgs::parse()
|
||||
.filepath
|
||||
.unwrap_or(PathBuf::from("./kanban.db"));
|
||||
.map(PathBuf::into_os_string)
|
||||
.unwrap_or("kanban.db".into());
|
||||
|
||||
let migrate = !dbpath.exists();
|
||||
let conn = Connection::open(dbpath)?;
|
||||
|
||||
let mut conn = Connection::open(dbpath)?;
|
||||
|
||||
if migrate {
|
||||
let migrations = include_str!("../sql/migrations.sql");
|
||||
let migrations: Vec<&str> = migrations.split(';').collect();
|
||||
let tx = conn.transaction()?;
|
||||
for m in migrations {
|
||||
if !m.trim().is_empty() {
|
||||
tx.execute(m, ())?;
|
||||
}
|
||||
}
|
||||
tx.commit()?;
|
||||
}
|
||||
|
||||
let project = Project::load(&conn)?;
|
||||
let project = Project::load(&conn).await?;
|
||||
let mut state = State::new(conn, project);
|
||||
|
||||
enable_raw_mode()?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user