summaryrefslogtreecommitdiffstats
path: root/src/joshuto/command/tab_switch.rs
blob: 577ea341f4ab65a30bff8441a017f91c518bbf16 (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
use std;
use std::fmt;

use joshuto;
use joshuto::command;
use joshuto::ui;

#[derive(Clone, Debug)]
pub struct TabSwitch {
    movement: i32,
}

impl TabSwitch {
    pub fn new(movement: i32) -> Self {
        TabSwitch {
            movement,
        }
    }
    pub const fn command() -> &'static str { "tab_switch" }

    pub fn tab_switch(new_index: i32, context: &mut joshuto::JoshutoContext)
    {
        context.tab_index = new_index as usize;
        ui::refresh(context);
        ui::redraw_tab_view(&context.views.tab_win, &context);

        ncurses::doupdate();
    }
}

impl command::JoshutoCommand for TabSwitch {}

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

impl command::Runnable for TabSwitch {
    fn execute(&self, context: &mut joshuto::JoshutoContext)
    {
        let mut new_index = context.tab_index as i32 + self.movement;
        if new_index < 0 {
            new_index = 0;
        } else if new_index >= context.tabs.len() as i32 {
            new_index = context.tabs.len() as i32 - 1;
        }
        Self::tab_switch(new_index, context);
    }
}