yazi/yazi-core/src/manager/commands/tab_switch.rs
2024-01-31 08:59:47 +08:00

35 lines
661 B
Rust

use yazi_shared::{event::Cmd, render};
use crate::manager::Tabs;
pub struct Opt {
step: isize,
relative: bool,
}
impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self {
Self {
step: c.take_first().and_then(|s| s.parse().ok()).unwrap_or(0),
relative: c.named.contains_key("relative"),
}
}
}
impl Tabs {
pub fn switch(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
let idx = if opt.relative {
(self.idx as isize + opt.step).rem_euclid(self.items.len() as isize) as usize
} else {
opt.step as usize
};
if idx == self.idx || idx >= self.items.len() {
return;
}
self.set_idx(idx);
render!();
}
}