-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,14 @@ extern crate regex; | |
mod mods; | ||
|
||
use chrono::Local; | ||
use std::env::args; | ||
use std::path::Path; | ||
use std::process::exit; | ||
|
||
use ncurses::*; | ||
use std::sync::atomic::Ordering; | ||
|
||
use mods::todo::*; | ||
use mods::ui::*; | ||
use mods::utils::*; | ||
|
||
const SELECTED_PAIR: i16 = 1; | ||
const UNSELECTED_PAIR: i16 = 2; | ||
|
@@ -40,7 +40,14 @@ Author: Iurii Kondrakov <[email protected]> | |
|
||
const FILE_PATH: &str = "TODO.list"; | ||
|
||
#[derive(PartialEq)] | ||
enum Mode { | ||
Edit, | ||
Normal, | ||
} | ||
|
||
fn main() { | ||
set_sig_handler(); | ||
let file_path: String = get_args(); | ||
let file_name: String = Path::new(&file_path) | ||
.file_name() | ||
|
@@ -50,13 +57,13 @@ fn main() { | |
.to_string(); | ||
|
||
ncurses_init(); | ||
let mut editing: bool = false; | ||
let mut mode: Mode = Mode::Normal; | ||
let mut editing_cursor: usize = 0; | ||
let mut app: TodoApp = TodoApp::new(); | ||
let mut ui = UI::new(); | ||
|
||
app.parse(&file_path); | ||
loop { | ||
while !QUIT.load(Ordering::SeqCst) { | ||
erase(); | ||
let date = Local::now().format("%Y %a %b %d %H:%M:%S"); | ||
let mut w = 0; | ||
|
@@ -76,15 +83,20 @@ fn main() { | |
app.get_dones_n() | ||
), | ||
UI_PAIR, | ||
Some(A_BOLD()), | ||
); | ||
ui.label_styled( | ||
&format!("[MESSAGE]: {}", app.get_message()), | ||
UI_PAIR, | ||
Some(A_BOLD()), | ||
); | ||
ui.label_styled(&format!("[MESSAGE]: {}", app.get_message()), UI_PAIR); | ||
} | ||
ui.end_layout(); | ||
|
||
ui.begin_layout(LayoutKind::Vert); | ||
{ | ||
ui.label_styled(&format!("[DATE]: {date}"), UI_PAIR); | ||
ui.label_styled(&format!("[FILE]: {file_name}"), UI_PAIR); | ||
ui.label_styled(&format!("[DATE]: {date}"), UI_PAIR, Some(A_BOLD())); | ||
ui.label_styled(&format!("[FILE]: {file_name}"), UI_PAIR, Some(A_BOLD())); | ||
} | ||
ui.end_layout(); | ||
} | ||
|
@@ -98,15 +110,15 @@ fn main() { | |
ui.begin_layout(LayoutKind::Vert); | ||
{ | ||
if app.is_in_todo_panel() { | ||
ui.label_styled("[TODO]", HIGHLIGHT_PAIR); | ||
ui.label_styled("[TODO]", HIGHLIGHT_PAIR, None); | ||
} else { | ||
ui.label_styled(" TODO ", UNSELECTED_PAIR); | ||
ui.label_styled(" TODO ", UNSELECTED_PAIR, None); | ||
} | ||
ui.hl(); | ||
for todo in app.get_todos() { | ||
if app.is_cur_todo(todo) { | ||
if app.is_in_todo_panel() { | ||
if editing { | ||
if mode == Mode::Edit { | ||
ui.edit_label( | ||
todo.get_text(), | ||
editing_cursor, | ||
|
@@ -116,12 +128,14 @@ fn main() { | |
ui.label_styled( | ||
&format!("- [ ] {}", todo.get_text()), | ||
SELECTED_PAIR, | ||
None, | ||
); | ||
} | ||
} else { | ||
ui.label_styled( | ||
&format!("- [ ] {}", todo.get_text()), | ||
UNSELECTED_PAIR, | ||
None, | ||
); | ||
} | ||
} else { | ||
|
@@ -134,15 +148,15 @@ fn main() { | |
ui.begin_layout(LayoutKind::Vert); | ||
{ | ||
if app.is_in_done_panel() { | ||
ui.label_styled("[DONE]", HIGHLIGHT_PAIR); | ||
ui.label_styled("[DONE]", HIGHLIGHT_PAIR, None); | ||
} else { | ||
ui.label_styled(" DONE ", UNSELECTED_PAIR); | ||
ui.label_styled(" DONE ", UNSELECTED_PAIR, None); | ||
} | ||
ui.hl(); | ||
for done in app.get_dones() { | ||
if app.is_cur_done(done) { | ||
if app.is_in_done_panel() { | ||
if editing { | ||
if mode == Mode::Edit { | ||
ui.edit_label( | ||
done.get_text(), | ||
editing_cursor, | ||
|
@@ -152,12 +166,14 @@ fn main() { | |
ui.label_styled( | ||
&format!("- [X] ({}) {}", done.get_date(), done.get_text()), | ||
SELECTED_PAIR, | ||
None, | ||
); | ||
} | ||
} else { | ||
ui.label_styled( | ||
&format!("- [X]|{}| {}", done.get_date(), done.get_text()), | ||
UNSELECTED_PAIR, | ||
None, | ||
); | ||
} | ||
} else { | ||
|
@@ -174,37 +190,55 @@ fn main() { | |
refresh(); | ||
let key = getch(); | ||
if key != ERR { | ||
if !editing { | ||
app.clear_message(); | ||
match char::from_u32(key as u32).unwrap() { | ||
'k' | '\u{103}' => app.go_up(), | ||
'j' | '\u{102}' => app.go_down(), | ||
'g' => app.go_top(), | ||
'G' => app.go_bottom(), | ||
'K' => app.drag_up(), | ||
'J' => app.drag_down(), | ||
'\n' => app.transfer_item(), | ||
'd' => app.delete_item(), | ||
'i' => { | ||
editing_cursor = app.insert_item(); | ||
editing = editing_cursor == 0; | ||
} | ||
'r' => { | ||
editing_cursor = app.edit_item(); | ||
editing = editing_cursor > 0; | ||
match mode { | ||
Mode::Normal => { | ||
app.clear_message(); | ||
match char::from_u32(key as u32).unwrap() { | ||
'k' | '\u{103}' => app.go_up(), | ||
'j' | '\u{102}' => app.go_down(), | ||
'g' => app.go_top(), | ||
'G' => app.go_bottom(), | ||
'K' => app.drag_up(), | ||
'J' => app.drag_down(), | ||
'\n' => app.transfer_item(), | ||
'd' => app.delete_item(), | ||
'i' => { | ||
if let Some(cur) = app.insert_item() { | ||
editing_cursor = cur; | ||
mode = Mode::Edit; | ||
} | ||
} | ||
'a' => { | ||
if let Some(cur) = app.append_item() { | ||
editing_cursor = cur; | ||
mode = Mode::Edit; | ||
} | ||
} | ||
'r' => { | ||
if let Some(cur) = app.edit_item() { | ||
editing_cursor = cur; | ||
mode = Mode::Edit; | ||
} | ||
} | ||
'u' => app.undo(), | ||
'\t' => app.toggle_panel(), | ||
'q' | '\u{3}' => break, | ||
_ => {} | ||
} | ||
'u' => app.undo(), | ||
'\t' => app.toggle_panel(), | ||
'q' | '\u{3}' => break, | ||
_ => {} | ||
} | ||
} else { | ||
match key as u8 as char { | ||
'\n' | '\u{1b}' => { | ||
editing = app.finish_edit(); | ||
editing_cursor = if editing { editing_cursor } else { 0 }; | ||
Mode::Edit => { | ||
match key as u8 as char { | ||
'\n' => { | ||
//'\u{1b}' | ||
mode = if app.finish_edit() { | ||
editing_cursor = 0; | ||
Mode::Normal | ||
} else { | ||
Mode::Edit | ||
}; | ||
} | ||
_ => app.edit_item_with(&mut editing_cursor, key), | ||
} | ||
_ => app.edit_item_with(&mut editing_cursor, key), | ||
} | ||
} | ||
} | ||
|
@@ -213,48 +247,3 @@ fn main() { | |
endwin(); | ||
app.save(&file_path).unwrap(); | ||
} | ||
|
||
fn ncurses_init() { | ||
setlocale(LcCategory::all, ""); | ||
// Init ncurses | ||
initscr(); | ||
raw(); | ||
// Allow for extended keyboard (like F1). | ||
noecho(); | ||
keypad(stdscr(), true); | ||
curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE); | ||
// Set timeout and esc delay | ||
timeout(16); | ||
set_escdelay(0); | ||
// Set colors | ||
use_default_colors(); | ||
start_color(); | ||
init_pair(HIGHLIGHT_PAIR, COLOR_BLACK, COLOR_GREEN); | ||
init_pair(SELECTED_PAIR, COLOR_BLACK, COLOR_CYAN); | ||
init_pair(UNSELECTED_PAIR, COLOR_BLACK, COLOR_WHITE); | ||
init_pair(UI_PAIR, COLOR_WHITE, COLOR_BLACK); | ||
} | ||
|
||
fn get_args() -> String { | ||
let mut args = args().skip(1); | ||
|
||
match args.next() { | ||
Some(arg) => match arg.as_str() { | ||
"-f" | "--file" => args.next().unwrap_or_else(|| { | ||
eprintln!("[ERROR]: No file given for '{arg}'."); | ||
eprintln!("{USAGE}"); | ||
exit(1); | ||
}), | ||
"-h" | "--help" => { | ||
println!("{HELP}\n{USAGE}"); | ||
exit(0); | ||
} | ||
_ => { | ||
eprintln!("[ERROR]: Unknown argument: '{arg}'."); | ||
eprintln!("{USAGE}"); | ||
exit(1); | ||
} | ||
}, | ||
None => FILE_PATH.to_string(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod todo; | ||
pub mod ui; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.