summaryrefslogtreecommitdiffstats
path: root/src/commands/sort.rs
blob: c264c4b2ab6c37639c74807a8809fc0f3699f627 (plain)
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
use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::history::DirectoryHistory;
use crate::ui::TuiBackend;

use crate::util::load_child::LoadChild;
use crate::util::sort::SortType;

#[derive(Clone, Debug)]
pub struct Sort {
    sort_method: SortType,
}

impl Sort {
    pub fn new(sort_method: SortType) -> Self {
        Self { sort_method }
    }
    pub const fn command() -> &'static str {
        "sort"
    }
}

impl JoshutoCommand for Sort {}

impl std::fmt::Display for Sort {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{} {}", Self::command(), self.sort_method.as_str())
    }
}

impl JoshutoRunnable for Sort {
    fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> {
        context.config_t.sort_option.sort_method = self.sort_method;
        for tab in context.tab_context_mut().iter_mut() {
            tab.history_mut().depreciate_all_entries();
        }
        ReloadDirList::soft_reload(context.tab_context_ref().get_index(), context)?;
        LoadChild::load_child(context)?;
        Ok(())
    }
}

#[derive(Clone, Debug)]
pub struct SortReverse;

impl SortReverse {
    pub const fn new() -> Self {
        Self {}
    }
    pub const fn command() -> &'static str {
        "sort"
    }
}

impl JoshutoCommand for SortReverse {}

impl std::fmt::Display for SortReverse {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{} reverse", Self::command())
    }
}

impl JoshutoRunnable for SortReverse {
    fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> {
        context.config_t.sort_option.reverse = !context.config_t.sort_option.reverse;
        for tab in context.tab_context_mut().iter_mut() {
            tab.history_mut().depreciate_all_entries();
        }
        ReloadDirList::soft_reload(context.tab_context_ref().get_index(), context)?;
        LoadChild::load_child(context)?;
        Ok(())
    }
}