Skip to content

Commit

Permalink
ncurses finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Deezzir committed Feb 5, 2023
1 parent 01073a0 commit 68724bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
54 changes: 34 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const HIGHLIGHT_PAIR: i16 = 3;
const UI_PAIR: i16 = 4;

const USAGE: &str = "Usage: todo [-f | --file <file>] [-h | --help]";

const HELP: &str = r#"ToDors - a simple todo list manager in terminal.
Author: Iurii Kondrakov <[email protected]>
Expand All @@ -43,25 +42,16 @@ const FILE_PATH: &str = "TODO";
fn main() {
let file_path: String = get_args();

initscr();
raw();
noecho();
keypad(stdscr(), true);
timeout(16);
curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE);
use_default_colors();
init_colors();

ncurses_init();
let mut editing: bool = false;
let mut editing_cursor: usize = 0;
let mut app: TodoApp = TodoApp::new();
let mut ui = UI::new();

app.parse(&file_path);

loop {
erase();
let date = Local::now().format("%Y-%m-%d %H:%M:%S");
let date = Local::now().format("%Y %a %b %d %H:%M:%S");
let mut w = 0;
let mut h = 0;
getmaxyx(stdscr(), &mut h, &mut w);
Expand All @@ -72,17 +62,28 @@ fn main() {
{
ui.begin_layout(LayoutKind::Vert);
{
ui.label_styled(
&format!(
"[CONTENT]: ({})todos and ({})dones",
app.get_todos_n(),
app.get_dones_n()
),
UI_PAIR,
);
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!("[DATE]: {date}"), UI_PAIR);
ui.label_styled(&format!("[FILE]: {file_path}"), UI_PAIR);
}
ui.end_layout();
}
ui.end_layout();

ui.hl();
ui.br();

ui.begin_layout(LayoutKind::Horz);
Expand Down Expand Up @@ -168,9 +169,9 @@ fn main() {
if key != ERR {
if !editing {
app.clear_message();
match key as u8 as char {
'k' => app.go_up(),
'j' => app.go_down(),
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(),
Expand All @@ -187,12 +188,12 @@ fn main() {
}
'u' => app.undo(),
'\t' => app.toggle_panel(),
'q' => break,
'q' | '\u{3}' => break,
_ => {}
}
} else {
match key as u8 as char {
'\n' => {
'\n' | '\u{1b}' => {
editing = app.finish_edit();
editing_cursor = if editing { editing_cursor } else { 0 };
}
Expand All @@ -206,7 +207,20 @@ fn main() {
app.save(&file_path).unwrap();
}

fn init_colors() {
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);
Expand All @@ -225,7 +239,7 @@ fn get_args() -> String {
exit(1);
}),
"-h" | "--help" => {
eprintln!("{HELP}\n{USAGE}");
println!("{HELP}\n{USAGE}");
exit(0);
}
_ => {
Expand Down
15 changes: 12 additions & 3 deletions src/mods/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ impl List {
*cur += 1;
}
}
constants::KEY_BACKSPACE => {
constants::KEY_BACKSPACE | 127 => {
// 127 is backspace
if *cur > 0 {
*cur -= 1;
if *cur < item.text.len() {
Expand All @@ -244,8 +245,8 @@ impl List {
item.text.remove(*cur);
}
}
constants::KEY_HOME => *cur = 0,
constants::KEY_END => *cur = item.text.len(),
constants::KEY_HOME | 1 => *cur = 0, // 1 is ctrl + a
constants::KEY_END | 5 => *cur = item.text.len(), // 5 is ctrl + e
_ => {}
}
}
Expand Down Expand Up @@ -298,10 +299,18 @@ impl TodoApp {
&self.todos.list
}

pub fn get_todos_n(&self) -> usize {
self.todos.list.len()
}

pub fn get_dones(&self) -> &Vec<Item> {
&self.dones.list
}

pub fn get_dones_n(&self) -> usize {
self.dones.list.len()
}

pub fn clear_message(&mut self) {
self.message.clear();
}
Expand Down
6 changes: 3 additions & 3 deletions src/mods/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl UI {
.last()
.expect("Tried to render horizontal line outside of any layout");

let text = "-".repeat(layout.borrow().max_size.x as usize);
let text = "".repeat(layout.borrow().max_size.x as usize);
self.label(&text);
}

Expand All @@ -210,7 +210,7 @@ impl UI {
" ".repeat((layout.borrow().max_size.x as usize).saturating_sub(text.len()));

mv(pos.y, pos.x);
addstr(&format!("{}{}", text, space_fill));
addstr(&format!("{text}{space_fill}"));

layout
.borrow_mut()
Expand Down Expand Up @@ -242,7 +242,7 @@ impl UI {
{
mv(pos.y, pos.x + cur as i32 + prefix.len() as i32);
attr_on(A_REVERSE());
addstr(&text.get(cur..=cur).unwrap_or(" "));
addstr(text.get(cur..=cur).unwrap_or(" "));
attr_off(A_REVERSE());
}
}
Expand Down

0 comments on commit 68724bc

Please sign in to comment.