summaryrefslogtreecommitdiffstats
path: root/src/commands/cursor_move.rs
blob: 3b9aeda0a42788c583511c32d0be81a5bdc43fda (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
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::history::DirectoryHistory;
use crate::ui::TuiBackend;
use std::path::PathBuf;

pub fn cursor_move(new_index: usize, context: &mut JoshutoContext) -> JoshutoResult<()> {
    let mut path: Option<PathBuf> = None;
    let mut new_index = new_index;

    if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() {
        if curr_list.index.is_some() {
            let dir_len = curr_list.contents.len();
            if new_index >= dir_len {
                new_index = dir_len - 1;
            }
            curr_list.index = Some(new_index);

            let entry = &curr_list.contents[new_index];
            path = Some(entry.file_path().to_path_buf())
        }
    }

    // get preview
    if let Some(path) = path {
        if path.is_dir() {
            let sort_options = context.config_ref().sort_option.clone();
            context
                .tab_context_mut()
                .curr_tab_mut()
                .history_mut()
                .create_or_soft_update(path.as_path(), &sort_options)?;
        }
    }
    Ok(())
}

pub fn up(context: &mut JoshutoContext, u: usize) -> JoshutoResult<()> {
    let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        Some(curr_list) => curr_list.index.map(|idx| if idx > u { idx - u } else { 0 }),
        None => None,
    };

    if let Some(s) = movement {
        cursor_move(s, context)?;
    }
    Ok(())
}

pub fn down(context: &mut JoshutoContext, u: usize) -> JoshutoResult<()> {
    let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        Some(curr_list) => curr_list.index.map(|idx| idx + u),
        None => None,
    };
    if let Some(s) = movement {
        cursor_move(s, context)?;
    }
    Ok(())
}

pub fn home(context: &mut JoshutoContext) -> JoshutoResult<()> {
    let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        Some(curr_list) => {
            let len = curr_list.contents.len();
            if len == 0 {
                None
            } else {
                Some(0)
            }
        }
        None => None,
    };

    if let Some(s) = movement {
        cursor_move(s, context)?;
    }
    Ok(())
}

pub fn end(context: &mut JoshutoContext) -> JoshutoResult<()> {
    let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        Some(curr_list) => {
            let len = curr_list.contents.len();
            if len == 0 {
                None
            } else {
                Some(len - 1)
            }
        }
        None => None,
    };

    if let Some(s) = movement {
        cursor_move(s, context)?;
    }
    Ok(())
}

pub fn page_up(context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> {
    let half_page = {
        match backend.terminal.as_ref().unwrap().size() {
            Ok(rect) => rect.height as usize - 2,
            _ => 10,
        }
    };

    let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        Some(curr_list) => curr_list
            .index
            .map(|idx| if idx > half_page { idx - half_page } else { 0 }),
        None => None,
    };

    if let Some(s) = movement {
        cursor_move(s, context)?;
    }
    Ok(())
}

pub fn page_down(context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> {
    let half_page = {
        match backend.terminal.as_ref().unwrap().size() {
            Ok(rect) => rect.height as usize - 2,
            _ => 10,
        }
    };

    let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
        Some(curr_list) => {
            let dir_len = curr_list.contents.len();
            curr_list.index.map(|idx| {
                if idx + half_page > dir_len - 1 {
                    dir_len - 1
                } else {
                    idx + half_page
                }
            })
        }
        None => None,
    };

    if let Some(s) = movement {
        cursor_move(s, context)?;
    }
    Ok(())
}