englog 0.1.3

Keep a work log as a software developer for looking back on during yearly reviews etc.
Documentation
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use chrono::NaiveDate;
use ratatui::{
    style::{Color, Style},
    widgets::{Block, Borders, Padding},
};
use serde::{Deserialize, Serialize};
use std::{char, fs};
use tui_textarea::{CursorMove, Input, Key, TextArea};

use crate::config::Config;

#[derive(PartialEq)]
pub enum CurrentScreen {
    // determines whether or not we're typing into the filter
    Main(bool),
    ViewingDay,
}

#[derive(PartialEq, Clone)]
pub enum Info {
    About,
}

#[derive(PartialEq, Clone)]
pub enum Popup {
    NewDay,
    ConfDeleteDay,
    Info(Info),
    Config(bool), // bool: whether or not we're editing
}

pub enum PopupBuffer {
    NewDay(NewDayBuffer),
    Config(ConfigBuffer),
}

pub struct ConfigBuffer {
    pub word_limit: String,
}

impl ConfigBuffer {
    pub fn new(word_limit: Option<usize>) -> Self {
        let word_limit = if let Some(limit) = word_limit {
            limit.to_string()
        } else {
            String::new()
        };
        Self { word_limit }
    }

    pub fn push(&mut self, char: char) {
        self.word_limit.push(char);
    }

    pub fn pop(&mut self) {
        self.word_limit.pop();
    }

    pub fn clear(&mut self) {
        self.word_limit.clear();
    }
}

pub struct NewDayBuffer {
    pub day: String,
    pub month: String,
    pub year: String,
    pub currently_selected: u8,
}

impl NewDayBuffer {
    fn new() -> Self {
        Self {
            day: String::new(),
            month: String::new(),
            year: String::new(),
            currently_selected: 0,
        }
    }

    pub fn push(&mut self, char: char) {
        match self.currently_selected {
            0 => self.day.push(char),
            1 => self.month.push(char),
            2 => self.year.push(char),
            _ => panic!("Use an enum!"),
        };
    }

    pub fn pop(&mut self) {
        match self.currently_selected {
            0 => self.day.pop(),
            1 => self.month.pop(),
            2 => self.year.pop(),
            _ => panic!("Use an enum!"),
        };
    }

    pub fn clear(&mut self) {
        self.day.clear();
        self.month.clear();
        self.year.clear();
        self.currently_selected = 0;
    }
}

pub struct App<'a> {
    pub days: Days,
    pub should_quit: bool,
    pub current_screen: CurrentScreen,
    pub date: chrono::NaiveDate,
    pub currently_selected: usize,
    pub text_buffer: TextArea<'a>,
    pub filter_buffer: TextArea<'a>,
    pub popup: Option<Popup>,
    pub popup_buffer: NewDayBuffer,
    pub config_buffer: ConfigBuffer,
    pub file_path: String,
    pub min_index: isize, // kind of a hack. think of a better solution
    pub max_index: isize, // kind of a hack. think of a better solution
    pub saving: bool,
    pub loading: bool,
    pub filter: Option<String>,
    pub config: Config,
}

impl<'a> App<'a> {
    pub fn new(file_path: String, config: Config) -> Self {
        App {
            days: Days::default(),
            should_quit: false,
            current_screen: CurrentScreen::Main(false),
            date: chrono::Local::now().date_naive(),
            currently_selected: 0,
            text_buffer: day_text_area(None),
            filter_buffer: day_text_area(None),
            popup: None,
            popup_buffer: NewDayBuffer::new(),
            config_buffer: ConfigBuffer::new(config.chars_per_line),
            file_path,
            min_index: 0,
            max_index: -1,
            saving: false,
            loading: false,
            filter: None,
            config,
        }
    }

    pub fn switch_to_current_day(&mut self) {
        self.remove_filter();
        let now = self.now();
        self.currently_selected = self.days.iter().position(|d| d.date == now).unwrap();
    }

    fn now(&self) -> NaiveDate {
        chrono::Local::now().date_naive()
    }

    pub fn remove_filter(&mut self) {
        self.filter = None;
        self.current_screen = CurrentScreen::Main(false);
    }

    pub fn load_days(&mut self, switch_screen: bool) {
        let mut days = match fs::read(self.file_path.as_str()) {
            Ok(serialized) => postcard::from_bytes(&serialized).unwrap(),
            Err(_) => {
                let days = Days::default();
                save_inner(&days, self.file_path.as_str());
                days
            }
        };
        let now = self.now();

        if !days.contains_day(now) {
            days.add(Day::new(now));
        }
        let currently_selected = days.iter().position(|d| d.date == now).unwrap();
        self.days = days;
        self.currently_selected = currently_selected;
        if switch_screen {
            self.current_screen = CurrentScreen::ViewingDay;
            self.load_text();
        }
    }

    pub fn load_text(&mut self) {
        let filtered_days: Vec<_> = self.filtered_days().collect();
        if filtered_days.is_empty() {
            self.text_buffer = day_text_area(None);
        } else {
            self.text_buffer =
                day_text_area(Some(filtered_days[self.currently_selected].content_into()));
        }
    }

    pub fn input_to_current_day(&mut self, input: Input) {
        if let Key::Char(_) = input.key {
            if let Some(limit) = self.config.chars_per_line {
                if !input.ctrl {
                    let (y, _) = self.text_buffer.cursor();
                    let current_line = &self.text_buffer.lines()[y];
                    if current_line.len() + 1 > limit {
                        if current_line.split_whitespace().count() > 1 {
                            self.text_buffer.move_cursor(CursorMove::WordBack);
                            self.text_buffer.delete_next_word();
                            self.text_buffer.insert_newline();
                            self.text_buffer.insert_str(self.text_buffer.yank_text());
                        } else {
                            self.text_buffer.insert_newline();
                        }
                    }
                }
            }
        }
        self.text_buffer.input(input);
    }

    pub fn input_to_filter_buffer(&mut self, input: Input) {
        self.filter_buffer.input(input);
        self.filter = Some(self.filter_buffer.lines().join(""));
        let count = self.filtered_days().count();
        if self.currently_selected >= count && count > 0 {
            self.currently_selected = count - 1;
        }
    }

    // This count is getting relied on quite heavily
    // Inefficient but it seems quite fast ATM
    pub fn filtered_days(&self) -> impl Iterator<Item = &Day> {
        self.days.iter_filtered(self.filter.as_deref())
    }

    pub fn filtered_days_mut(&mut self) -> impl Iterator<Item = &mut Day> {
        self.days.iter_mut_filtered(self.filter.as_deref())
    }

    pub fn increment_selected(&mut self) {
        if self.currently_selected < self.filtered_days().count() - 1 {
            self.currently_selected += 1;
        }
    }

    pub fn decrement_selected(&mut self) {
        if self.currently_selected > 0 {
            self.currently_selected -= 1;
        }
    }

    pub fn save(&mut self) {
        save_inner(&self.days, &self.file_path);
    }

    pub fn update_day_from_buffer(&mut self) {
        let selected = self.currently_selected;
        let content = Vec::from(self.text_buffer.lines());
        let mut filtered_days: Vec<_> = self.filtered_days_mut().collect();
        if !filtered_days.is_empty() {
            filtered_days[selected].content = content;
        }
    }

    pub fn init_filter_text(&mut self) {
        self.filter_buffer = new_text_area(
            None,
            Block::default()
                .style(Style::default().fg(Color::White))
                .borders(Borders::NONE)
                .padding(Padding::horizontal(1)),
        );
    }

    pub fn remove_day(&mut self) {
        if self.days.len() > 0 {
            self.days.days.remove(self.currently_selected);
            if self.currently_selected >= self.days.len() && self.currently_selected > 0 {
                self.currently_selected -= 1;
            }
            self.save();
        }
    }
}

pub fn save_inner(days: &Days, file_path: &str) {
    let serialized: Vec<u8> = postcard::to_allocvec(days).unwrap();
    let _ = fs::File::create(file_path);
    fs::write(file_path, serialized).expect("Failed to write to file");
}

fn new_text_area(input: Option<Vec<String>>, block: Block<'_>) -> TextArea<'_> {
    let mut textarea = match input {
        Some(input) => TextArea::new(input),
        None => TextArea::default(),
    };
    textarea.set_block(block);
    textarea.set_style(Style::default().fg(Color::White));
    textarea.set_cursor_line_style(Style::default());
    textarea.move_cursor(CursorMove::Bottom);
    textarea.move_cursor(CursorMove::End);
    textarea
}

pub fn day_text_area<'a>(input: Option<Vec<String>>) -> TextArea<'a> {
    let mut textarea = new_text_area(
        input,
        Block::default()
            .title("Note")
            .style(Style::default().fg(Color::White))
            .borders(Borders::ALL)
            .padding(Padding::horizontal(1)),
    );
    textarea.set_placeholder_text("Start typing..");
    textarea
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Days {
    pub days: Vec<Day>,
}

impl Days {
    fn default() -> Self {
        Self { days: Vec::new() }
    }

    pub fn iter_mut_filtered<'a>(
        &'a mut self,
        string: Option<&'a str>,
    ) -> impl Iterator<Item = &mut Day> + 'a {
        self.days.iter_mut().filter(move |d| {
            let date_str = d.date.format("%d/%m/%Y").to_string();
            if let Some(string) = string {
                date_str.contains(string)
            } else {
                true
            }
        })
    }

    pub fn iter_filtered<'a>(&'a self, string: Option<&'a str>) -> impl Iterator<Item = &Day> + 'a {
        self.days.iter().filter(move |d| {
            let date_str = d.date.format("%d/%m/%Y").to_string();
            if let Some(string) = string {
                date_str.contains(string)
            } else {
                true
            }
        })
    }

    fn len(&self) -> usize {
        self.days.len()
    }

    pub fn add(&mut self, day: Day) -> usize {
        match self.days.binary_search(&day) {
            Ok(pos) => pos,
            Err(pos) => {
                self.days.insert(pos, day);
                pos
            }
        }
    }

    fn contains_day(&self, date: NaiveDate) -> bool {
        self.days.iter().any(|d| d.date == date)
    }

    pub fn iter(&self) -> impl Iterator<Item = &Day> {
        self.days.iter()
    }
}

#[derive(Serialize, Deserialize, Debug, Eq, Clone)]
pub struct Day {
    pub date: NaiveDate,
    pub content: Vec<String>,
}

impl PartialEq for Day {
    fn eq(&self, other: &Self) -> bool {
        self.date == other.date
    }
}

impl PartialOrd for Day {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Day {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.date.cmp(&other.date).reverse()
    }
}

impl Day {
    pub fn new(date: NaiveDate) -> Self {
        Self {
            date,
            content: Vec::new(),
        }
    }

    pub fn date_pretty(&self) -> String {
        self.date.format("%d/%m/%Y").to_string()
    }

    pub fn content_into(&self) -> Vec<String> {
        self.content.clone()
    }
}