332 lines
No EOL
9.9 KiB
Rust
332 lines
No EOL
9.9 KiB
Rust
// Libraries
|
|
use crate::{board, card, manager};
|
|
use std::{io, process::exit};
|
|
|
|
// Structures
|
|
pub struct Interface<'a> {
|
|
manager: &'a mut manager::Manager,
|
|
|
|
pub running: bool,
|
|
page: u32,
|
|
mode: u32,
|
|
on: u32
|
|
}
|
|
|
|
// Implementations
|
|
impl<'a> Interface<'a> {
|
|
pub fn init(manager: &'a mut manager::Manager) -> Interface<'a>{
|
|
// Return result
|
|
return Interface {manager: manager, running: true, page: 0, mode: 0, on: 0};
|
|
}
|
|
|
|
fn p_0(&mut self) {
|
|
// Showing options
|
|
println!(" - Tasky - ");
|
|
println!("1) Open Board");
|
|
println!("2) Create Board");
|
|
println!("3) Exit");
|
|
println!("\nSelect option [1-3]: ");
|
|
|
|
// Listening for option
|
|
let mut user_input: String = String::new();
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
println!("");
|
|
|
|
// Deciding what to do
|
|
if user_input == "1" {
|
|
self.page = 1;
|
|
} else if user_input == "2"{
|
|
self.page = 6;
|
|
} else if user_input == "3" {
|
|
exit(0);
|
|
}
|
|
}
|
|
fn p_1(&mut self) {
|
|
// Showing options
|
|
println!(" - Open Board - ");
|
|
for i in 0..self.manager.boards.len() {
|
|
println!("{}) {}", i+1, self.manager.boards[i].name);
|
|
}
|
|
println!("\nSelect board [1-{}]: ", self.manager.boards.len());
|
|
|
|
|
|
// Selecting option
|
|
let mut user_input: String = String::new();
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
println!("");
|
|
|
|
// Parsing option
|
|
self.on = user_input.parse::<u32>().unwrap() - 1;
|
|
self.page = 2;
|
|
}
|
|
fn p_2(&mut self) {
|
|
// Active board
|
|
let board: &board::TaskBoard = &self.manager.boards[self.on as usize];
|
|
|
|
// Title
|
|
println!(" - {} - \n", board.name);
|
|
|
|
// Going through cards
|
|
for card in &board.cards {
|
|
println!("- {} ({})", card.name, card.desc);
|
|
}
|
|
|
|
// Presenting options
|
|
println!("\n1) Create Card");
|
|
println!("2) Edit Card");
|
|
println!("3) Delete Card");
|
|
println!("4) Delete Board");
|
|
println!("5) Exit");
|
|
println!("\nSelect Option [1-5]: ");
|
|
|
|
// Selection option
|
|
let mut user_input: String = String::new();
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read lines");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
println!("");
|
|
|
|
// What to do
|
|
if user_input == "1" {
|
|
self.page = 3;
|
|
} else if user_input == "2"{
|
|
self.page = 4;
|
|
} else if user_input == "3"{
|
|
self.mode = 0;
|
|
self.page = 5;
|
|
} else if user_input == "4"{
|
|
self.mode = 1;
|
|
self.page = 5;
|
|
} else if user_input == "5"{
|
|
self.page = 0;
|
|
}
|
|
}
|
|
fn p_3(&mut self){
|
|
// Title
|
|
println!(" - Create Card - \n");
|
|
|
|
// Variables
|
|
let mut user_input: String = String::new();
|
|
|
|
// Asking the user what the name is
|
|
println!("What is your card's name?");
|
|
println!("Select name: ");
|
|
|
|
// Reading input
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
let _name: String = String::from(user_input.trim());
|
|
println!("");
|
|
|
|
// Reset user input
|
|
user_input = String::new();
|
|
|
|
// Asking the user what the description is
|
|
println!("Give your card a description!");
|
|
println!("Select description: ");
|
|
|
|
// Reading input
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
let _desc: String = String::from(user_input.trim());
|
|
println!("");
|
|
|
|
// Creating a card
|
|
let _new_card: card::TaskCard = card::TaskCard::init(_name, _desc);
|
|
|
|
// Throw this into our manager
|
|
self.manager.boards[self.on as usize].cards.push(_new_card);
|
|
|
|
// Finished!
|
|
self.page = 2;
|
|
}
|
|
fn p_4(&mut self){
|
|
// Title
|
|
println!(" - Edit Card - \n");
|
|
|
|
// Getting active board
|
|
let _active: &mut board::TaskBoard = &mut self.manager.boards[self.on as usize];
|
|
|
|
// Showing all cards
|
|
for i in 0.._active.cards.len() {
|
|
println!("{}) {}", i+1, _active.cards[i].name);
|
|
}
|
|
|
|
// Asking for an option
|
|
println!("\nSelect card[1-{}]: ", _active.cards.len());
|
|
|
|
// Reading input
|
|
let mut user_input: String = String::new();
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
println!("");
|
|
|
|
// Selecting the card
|
|
let _card_index: usize = user_input.parse::<usize>().unwrap() - 1;
|
|
let _card: &mut card::TaskCard = &mut _active.cards[_card_index];
|
|
|
|
// Printing options
|
|
println!(" - {} - ", _card.name);
|
|
println!("1) Change Name");
|
|
println!("2) Change Description");
|
|
println!("3) Exit");
|
|
println!("\nSelect option [1-3]: ");
|
|
|
|
// Reset input
|
|
user_input = String::new();
|
|
|
|
// Reading input
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
|
|
// Decide what to do
|
|
if user_input == "1" {
|
|
// Printing prompt
|
|
println!("\nWhat should the new name be?");
|
|
println!("Select name:");
|
|
|
|
// Reset input
|
|
user_input = String::new();
|
|
|
|
// Reading input
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.trim());
|
|
|
|
// Setting the cards name
|
|
_card.name = user_input;
|
|
} else if user_input == "2" {
|
|
// Printing prompt
|
|
println!("\nWhat should the new description be?");
|
|
println!("Select description:");
|
|
|
|
// Reset input
|
|
user_input = String::new();
|
|
|
|
// Reading input
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.trim());
|
|
|
|
// Setting the cards name
|
|
_card.desc = user_input;
|
|
}
|
|
|
|
// Changing page
|
|
self.page = 2;
|
|
}
|
|
fn p_5(&mut self){
|
|
// Which title do we use
|
|
let title: String = String::from(match self.mode {
|
|
0 => "Card",
|
|
1 => "Board",
|
|
_ => "INVALID MODE"
|
|
});
|
|
|
|
// Printing out the title
|
|
println!(" - Delete {} - ", title);
|
|
|
|
// Active board
|
|
let _active: &board::TaskBoard = &self.manager.boards[self.on as usize];
|
|
|
|
// Branching
|
|
if self.mode == 0 {
|
|
// Presenting all cards
|
|
for i in 0.._active.cards.len() {
|
|
println!("{}) {}", i+1, _active.cards[i].name);
|
|
}
|
|
|
|
// Displaying prompt
|
|
println!("\nSelect option [1-{}]: ", _active.cards.len());
|
|
|
|
// Getting user input
|
|
let mut user_input: String = String::new();
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
|
|
// Parse user input into a usize
|
|
let card_index: usize = user_input.parse::<usize>().unwrap() - 1;
|
|
|
|
// Reset user input
|
|
user_input = String::new();
|
|
|
|
// Confirming
|
|
println!("\nAre you sure?\nSelect option [y/n]: ");
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
println!("");
|
|
|
|
// If we don't want to do this:
|
|
if user_input != "y" {
|
|
self.page = 2;
|
|
return;
|
|
}
|
|
|
|
// Delete the card
|
|
self.manager.boards[self.on as usize].cards.remove(card_index);
|
|
|
|
// Set page back to normal
|
|
self.page = 2;
|
|
} else if self.mode == 1 {
|
|
// Confirming
|
|
let mut user_input: String = String::new();
|
|
println!("\nAre you sure?\nSelect option [y/n]: ");
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
user_input = String::from(user_input.to_lowercase().trim());
|
|
println!("");
|
|
|
|
// If the user says no
|
|
if user_input != "y" {
|
|
self.page = 2;
|
|
return;
|
|
}
|
|
|
|
// Deleting the board
|
|
self.manager.boards.remove(self.on as usize);
|
|
|
|
// Resetting all of the stuff
|
|
self.page = 0;
|
|
self.on = 0;
|
|
}
|
|
}
|
|
fn p_6(&mut self){
|
|
// Title
|
|
println!(" - Create Board - \n");
|
|
|
|
// Variables
|
|
let mut user_input: String = String::new();
|
|
|
|
// Asking the user what the name is
|
|
println!("What is your boards's name?");
|
|
println!("Select name: ");
|
|
|
|
// Reading input
|
|
io::stdin().read_line(&mut user_input).expect("Failed to read line");
|
|
let _name: String = String::from(user_input.trim());
|
|
println!("");
|
|
|
|
// Creating the new board
|
|
self.manager.boards.push(board::TaskBoard::init(_name));
|
|
|
|
// Open the board
|
|
self.page = 2;
|
|
self.on = (self.manager.boards.len()-1) as u32;
|
|
}
|
|
|
|
fn error(msg: &str){
|
|
println!("INTERFACE ERROR: {}", msg);
|
|
}
|
|
|
|
pub fn run(&mut self) {
|
|
// Based on the page do something
|
|
match self.page {
|
|
0 => self.p_0(),
|
|
1 => self.p_1(),
|
|
2 => self.p_2(),
|
|
3 => self.p_3(),
|
|
4 => self.p_4(),
|
|
5 => self.p_5(),
|
|
6 => self.p_6(),
|
|
_ => Interface::error("Invalid Page")
|
|
}
|
|
|
|
// Save
|
|
}
|
|
} |