1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
pub mod actions;
pub mod configs;
pub mod events;
pub mod file_handler;
pub mod widget;

use configs::Configs;
use serde::{Deserialize, Serialize};
use std::{error::Error, io::Stdout};
use tui::{
    backend::CrosstermBackend,
    style::{Modifier, Style},
    Frame, Terminal,
};

pub type DynResult = Result<(), Box<dyn Error>>;
pub type CrossTerminal = Terminal<CrosstermBackend<Stdout>>;
pub type TerminalFrame<'a> = Frame<'a, CrosstermBackend<Stdout>>;

/// Represent a task
#[derive(Serialize, Deserialize, Clone)]
pub struct Quest {
    pub title: String,
    pub completed: bool,
}

impl Quest {
    pub fn new(title: String) -> Self {
        Self {
            title,
            completed: false,
        }
    }
}

/// Represent a list of tasks
#[derive(Serialize, Deserialize, Default)]
pub struct QuestList {
    pub quests: Vec<Quest>,
}

impl QuestList {
    pub fn new(quests: &[Quest]) -> Self {
        Self {
            quests: quests.to_vec(),
        }
    }
}

/// Possible Input field states
pub enum InputMode {
    /// Browsing quests
    Normal,
    /// Adding a new quest
    Adding,
}

/// Application state
pub struct App {
    /// New quest input value
    pub input: String,
    /// Current input mode
    pub input_mode: InputMode,
    /// List of all quests
    pub quests: Vec<Quest>,
    /// Should be true when application wants to exit
    pub should_exit: bool,
    /// Current selected quest
    pub selected_quest: Option<usize>,
    /// Application Configs
    pub configs: Configs,
}

impl App {
    pub fn new(quests: &[Quest], configs: Configs) -> Self {
        Self {
            quests: quests.to_vec(),
            selected_quest: Some(0),
            input: String::new(),
            input_mode: InputMode::Normal,
            should_exit: false,
            configs,
        }
    }

    pub fn default_style(&self) -> Style {
        Style::default()
            .fg(self.configs.colors.foreground)
            .bg(self.configs.colors.background)
    }

    pub fn selection_style(&self) -> Style {
        self.default_style()
            .fg(self.configs.colors.selection_fg)
            .bg(self.configs.colors.selection_bg)
    }

    pub fn check_sign_style(&self, selected: bool) -> Style {
        if selected {
            self.selection_style().fg(self.configs.colors.check_sign)
        } else {
            self.default_style().fg(self.configs.colors.check_sign)
        }
    }

    pub fn checked_quest_style(&self, selected: bool) -> Style {
        if selected {
            self.selection_style().add_modifier(Modifier::CROSSED_OUT)
        } else {
            self.default_style().add_modifier(Modifier::CROSSED_OUT)
        }
    }
}