summaryrefslogtreecommitdiffstats
path: root/src/browser_states.rs
blob: 00d81f7db8db0ded3df6d17b7498dd4ef31e2ec2 (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
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
//! an application state dedicated to displaying a tree

use std::io::{self, Write};
use std::path::PathBuf;
use std::time::Instant;
use termion::color;

use crate::app::{AppState, AppStateCmdResult};
use crate::app_context::AppContext;
use crate::commands::{Action, Command};
use crate::external::Launchable;
use crate::flat_tree::Tree;
use crate::help_states::HelpState;
use crate::patterns::Pattern;
use crate::screens::{self, Screen};
use crate::status::Status;
use crate::task_sync::TaskLifetime;
use crate::tree_build::TreeBuilder;
use crate::tree_options::{OptionBool, TreeOptions};
use crate::tree_views::TreeView;
use crate::verbs::VerbExecutor;

pub struct BrowserState {
    pub tree: Tree,
    pub filtered_tree: Option<Tree>,
    pending_pattern: Option<Pattern>, // a pattern which has not yet be applied
}

impl BrowserState {
    pub fn new(path: PathBuf, mut options: TreeOptions, tl: &TaskLifetime) -> Option<BrowserState> {
        let pending_pattern = options.pattern;
        options.pattern = None;
        let builder = TreeBuilder::from(path, options);
        match builder.build(screens::max_tree_height() as usize, tl) {
            Some(tree) => Some(BrowserState {
                tree,
                filtered_tree: None,
                pending_pattern,
            }),
            None => None, // interrupted
        }
    }
    pub fn displayed_tree(&self) -> &Tree {
        match &self.filtered_tree {
            Some(tree) => &tree,
            None => &self.tree,
        }
    }
}

impl AppState for BrowserState {
    fn apply(&mut self, cmd: &mut Command, con: &AppContext) -> io::Result<AppStateCmdResult> {
        self.pending_pattern = None;
        let (_, page_height) = termion::terminal_size().unwrap();
        let mut page_height = page_height as i32;
        page_height -= 2;
        Ok(match &cmd.action {
            Action::Back => {
                if self.filtered_tree.is_some() {
                    self.filtered_tree = None;
                    cmd.raw.clear();
                    AppStateCmdResult::Keep
                } else if self.tree.selection > 0 {
                    self.tree.selection = 0;
                    cmd.raw.clear();
                    AppStateCmdResult::Keep
                } else {
                    AppStateCmdResult::PopState
                }
            }
            Action::MoveSelection(dy) => {
                match self.filtered_tree {
                    Some(ref mut tree) => {
                        tree.move_selection(*dy, page_height);
                    }
                    None => {
                        self.tree.move_selection(*dy, page_height);
                    }
                };
                AppStateCmdResult::Keep
            }
            Action::ScrollPage(dp) => {
                // this should not be computed here
                if page_height < self.displayed_tree().lines.len() as i32 {
                    let dy = dp * page_height;
                    match self.filtered_tree {
                        Some(ref mut tree) => {
                            tree.try_scroll(dy, page_height);
                        }
                        None => {
                            self.tree.try_scroll(dy, page_height);
                        }
                    };
                }
                AppStateCmdResult::Keep
            }
            Action::OpenSelection => {
                let tree = match &self.filtered_tree {
                    Some(tree) => tree,
                    None => &self.tree,
                };
                if tree.selected_line().is_dir() {
                    let tl = TaskLifetime::unlimited();
                    AppStateCmdResult::from_optional_state(BrowserState::new(
                        tree.selected_line().path.clone(),
                        // tree.options.clone(),
                        tree.options.without_pattern(),
                        &tl,
                    ))
                } else {
                    AppStateCmdResult::Launch(Launchable::opener(&tree.selected_line().path)?)
                }
            }
            Action::Verb(verb_key) => match con.verb_store.get(&verb_key) {
                Some(verb) => self.execute_verb(verb, con)?,
                None => AppStateCmdResult::verb_not_found(&verb_key),
            },
            Action::PatternEdit(pat) => match pat.len() {
                0 => {
                    self.filtered_tree = None;
                    AppStateCmdResult::Keep
                }
                _ => {
                    self.pending_pattern = Some(Pattern::from(pat));
                    AppStateCmdResult::Keep
                }
            },
            Action::Help(about) => AppStateCmdResult::NewState(Box::new(HelpState::new(&about))),
            Action::Next => {
                if let Some(ref mut tree) = self.filtered_tree {
                    tree.try_select_next_match();
                    tree.make_selection_visible(page_height);
                }
                AppStateCmdResult::Keep
            }
            _ => AppStateCmdResult::Keep,
        })
    }

    fn has_pending_tasks(&self) -> bool {
        if self.pending_pattern.is_some() {
            return true;
        }
        if self.displayed_tree().has_dir_missing_size() {
            return true;
        }
        false
    }

    fn do_pending_task(&mut self, tl: &TaskLifetime) {
        if let Some(pat) = &mut self.pending_pattern {
            let start = Instant::now();
            let mut options = self.tree.options.clone();
            options.pattern = Some(pat.clone());
            let root = self.tree.root().clone();
            let len = self.tree.lines.len() as u16;
            let mut filtered_tree = TreeBuilder::from(root, options).build(len as usize, tl);
            if let Some(ref mut filtered_tree) = filtered_tree {
                info!("Tree search took {:?}", start.elapsed());
                filtered_tree.try_select_best_match();
                let (_, page_height) = termion::terminal_size().unwrap();
                let mut page_height = page_height as i32;
                page_height -= 2;
                filtered_tree.make_selection_visible(page_height);
            } // if none: task was cancelled from elsewhere
            self.filtered_tree = filtered_tree;
            self.pending_pattern = None;
            return;
        }
        if let Some(ref mut tree) = self.filtered_tree {
            tree.fetch_some_missing_dir_size(tl);
        } else {
            self.tree.fetch_some_missing_dir_size(tl);
        }
    }

    fn display(&mut self, screen: &mut Screen, _con: &AppContext) -> io::Result<()> {
        screen.write_tree(&self.displayed_tree())
    }

    fn write_status(&self, screen: &mut Screen, cmd: &Command, con: &AppContext) -> io::Result<()> {
        match &cmd.action {
            Action::PatternEdit(_) => {
                screen.write_status_text("Hit <enter> to select, <esc> to remove the filter")
            }
            Action::VerbEdit(verb_key) => match con.verb_store.get(&verb_key) {
                Some(verb) => screen.write_status_text(
                    &format!(
                        "Hit <enter> to {} : {}",
                        &verb.name,
                        verb.description_for(&self)
                    )
                    .to_string(),
                ),
                None => screen.write_status_text(
                    // TODO show what verbs start with the currently edited verb key
                    "Type a verb then <enter> to execute it (hit '?' for the list of verbs)",
                ),
            },
            _ => {
                let tree = self.displayed_tree();
                if tree.selection == 0 {
                    screen.write_status_text(
                        "Hit <enter> to quit, '?' for help, or type a few file's letters to navigate",
                    )
                } else {
                    let line = &tree.lines[tree.selection];
                    screen.write_status_text(match line.is_dir() {
                        true => "Hit <enter> to focus, or type a space then a verb",
                        false => "Hit <enter> to open the file, or type a space then a verb",
                    })
                }
            }
        }
    }
    fn write_flags(&self, screen: &mut Screen, _con: &AppContext) -> io::Result<()>