summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-08-31 15:05:14 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-08-31 15:05:58 -0400
commitdc5c3c96dbd4948b1be7194c2f86d473d935e199 (patch)
tree8fd3c8ecbf8fd4266ec734bb9d6f3f11e73290ad /src
parent78e23d75de3ece52c91bb9aeb1bceb085e1fe557 (diff)
move numbered_command into command that can be invoked
This fixes the issue where `numbered_command` conflicts with switch_tab_index - side: change how tabs are rendered for easier visualization
Diffstat (limited to 'src')
-rw-r--r--src/key_command/command.rs2
-rw-r--r--src/key_command/constants.rs1
-rw-r--r--src/key_command/impl_appcommand.rs1
-rw-r--r--src/key_command/impl_appexecute.rs3
-rw-r--r--src/key_command/impl_comment.rs1
-rw-r--r--src/key_command/impl_from_str.rs9
-rw-r--r--src/run.rs10
-rw-r--r--src/ui/views/tui_folder_view.rs37
-rw-r--r--src/ui/views/tui_hsplit_view.rs8
-rw-r--r--src/ui/widgets/tui_tab.rs43
10 files changed, 59 insertions, 56 deletions
diff --git a/src/key_command/command.rs b/src/key_command/command.rs
index 6c698ea..30f153f 100644
--- a/src/key_command/command.rs
+++ b/src/key_command/command.rs
@@ -70,10 +70,10 @@ pub enum Command {
ShowTasks,
ToggleHiddenFiles,
-
SwitchLineNums(LineNumberStyle),
Flat(usize),
+ NumberedCommand(char),
Sort(SortType),
SortReverse,
diff --git a/src/key_command/constants.rs b/src/key_command/constants.rs
index 68b99d2..c7cfaaa 100644
--- a/src/key_command/constants.rs
+++ b/src/key_command/constants.rs
@@ -77,6 +77,7 @@ cmd_constants![
(CMD_SUBDIR_FZF, "subdir_fzf"),
(CMD_ZOXIDE, "z"),
(CMD_ZOXIDE_INTERACTIVE, "zi"),
+ (CMD_NUMBERED_COMMAND, "numbered_command"),
(CMD_FLAT, "flat"),
(CMD_ESCAPE, "escape"),
];
diff --git a/src/key_command/impl_appcommand.rs b/src/key_command/impl_appcommand.rs
index f942d82..1990bbc 100644
--- a/src/key_command/impl_appcommand.rs
+++ b/src/key_command/impl_appcommand.rs
@@ -69,6 +69,7 @@ impl AppCommand for Command {
Self::ShowTasks => CMD_SHOW_TASKS,
Self::Flat(_) => CMD_FLAT,
+ Self::NumberedCommand(_) => CMD_NUMBERED_COMMAND,
Self::Sort(_) => CMD_SORT,
Self::SortReverse => CMD_SORT_REVERSE,
diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs
index b61980b..982825c 100644
--- a/src/key_command/impl_appexecute.rs
+++ b/src/key_command/impl_appexecute.rs
@@ -103,6 +103,9 @@ impl AppExecute for Command {
Self::SwitchLineNums(d) => line_nums::switch_line_numbering(context, *d),
Self::Flat(depth) => flat::flatten(*depth, context),
+ Self::NumberedCommand(c) => {
+ numbered_command::numbered_command(*c, context, backend, keymap_t)
+ }
Self::ToggleHiddenFiles => show_hidden::toggle_hidden(context),
diff --git a/src/key_command/impl_comment.rs b/src/key_command/impl_comment.rs
index 6f56c78..76e439b 100644
--- a/src/key_command/impl_comment.rs
+++ b/src/key_command/impl_comment.rs
@@ -92,6 +92,7 @@ impl CommandComment for Command {
Self::SwitchLineNums(_) => "Switch line numbering",
Self::Flat(_) => "Flattern directory list",
+ Self::NumberedCommand(_) => "Jump via input number",
Self::Sort(sort_type) => match sort_type {
SortType::Lexical => "Sort lexically",
diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs
index f358e3e..9507305 100644
--- a/src/key_command/impl_from_str.rs
+++ b/src/key_command/impl_from_str.rs
@@ -336,6 +336,15 @@ impl std::str::FromStr for Command {
format!("{}: {}", command, e),
)),
}
+ } else if command == CMD_NUMBERED_COMMAND {
+ let c = arg.chars().next();
+ match c {
+ Some(c) => Ok(Self::NumberedCommand(c)),
+ None => Err(JoshutoError::new(
+ JoshutoErrorKind::InvalidParameters,
+ format!("{}: no starting character given", command),
+ )),
+ }
} else {
Err(JoshutoError::new(
JoshutoErrorKind::UnrecognizedCommand,
diff --git a/src/run.rs b/src/run.rs
index 5f3d396..ba1f9a5 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -1,4 +1,3 @@
-use crate::commands::numbered_command;
use crate::commands::quit::QuitAction;
use crate::config::AppKeyMapping;
use crate::context::AppContext;
@@ -14,7 +13,7 @@ use crate::ui::views::TuiView;
use uuid::Uuid;
-use termion::event::{Event, Key};
+use termion::event::Event;
use tui::layout::Rect;
pub fn run_loop(
@@ -78,13 +77,6 @@ pub fn run_loop(
Event::Unsupported(s) => {
process_event::process_unsupported(context, backend, &keymap_t, s);
}
- Event::Key(Key::Char(c)) if c.is_numeric() && c != '0' => {
- if let Err(e) =
- numbered_command::numbered_command(c, context, backend, &keymap_t)
- {
- context.message_queue_mut().push_error(e.to_string());
- }
- }
key => match keymap_t.default_view.get(&key) {
None => {
context
diff --git a/src/ui/views/tui_folder_view.rs b/src/ui/views/tui_folder_view.rs
index e4e9795..10f16c7 100644
--- a/src/ui/views/tui_folder_view.rs
+++ b/src/ui/views/tui_folder_view.rs
@@ -15,8 +15,6 @@ use crate::ui::widgets::{
};
use crate::ui::PreviewArea;
-const TAB_VIEW_WIDTH: u16 = 15;
-
pub struct TuiFolderView<'a> {
pub context: &'a AppContext,
pub show_bottom_status: bool,
@@ -35,7 +33,6 @@ impl<'a> Widget for TuiFolderView<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let preview_context = self.context.preview_context_ref();
let curr_tab = self.context.tab_context_ref().curr_tab_ref();
- let curr_tab_id = self.context.tab_context_ref().curr_tab_id();
let curr_tab_cwd = curr_tab.cwd();
let curr_list = curr_tab.curr_list_ref();
@@ -199,23 +196,25 @@ impl<'a> Widget for TuiFolderView<'a> {
TuiTopBar::new(self.context, curr_tab_cwd).render(rect, buf);
// render tabs
- if self.context.tab_context_ref().len() > 1 {
- let topbar_width = area.width.saturating_sub(TAB_VIEW_WIDTH);
+ let topbar_width = (self.context.tab_context_ref().len() * 5) as u16;
+ let topbar_width = if topbar_width > area.width {
+ area.width
+ } else {
+ topbar_width
+ };
+ let topbar_x = area.width.saturating_sub(topbar_width);
- let rect = Rect {
- x: topbar_width,
- y: 0,
- width: TAB_VIEW_WIDTH,
- height: 1,
- };
- let name = curr_tab_id.to_string();
- TuiTabBar::new(
- &name[..5],
- self.context.tab_context_ref().index,
- self.context.tab_context_ref().len(),
- )
- .render(rect, buf);
- }
+ let rect = Rect {
+ x: topbar_x,
+ y: 0,
+ width: topbar_width,
+ height: 1,
+ };
+ TuiTabBar::new(
+ self.context.tab_context_ref().tab_order.as_slice(),
+ self.context.tab_context_ref().index,
+ )
+ .render(rect, buf);
}
}
diff --git a/src/ui/views/tui_hsplit_view.rs b/src/ui/views/tui_hsplit_view.rs
index 2c26bd6..d2de9d8 100644
--- a/src/ui/views/tui_hsplit_view.rs
+++ b/src/ui/views/tui_hsplit_view.rs
@@ -124,15 +124,9 @@ impl<'a> Widget for TuiHSplitView<'a> {
width: TAB_VIEW_WIDTH,
height: 1,
};
- let name = if let Some(ostr) = curr_tab.cwd().file_name() {
- ostr.to_str().unwrap_or("")
- } else {
- ""
- };
TuiTabBar::new(
- name,
+ self.context.tab_context_ref().tab_order.as_slice(),
self.context.tab_context_ref().index,
- self.context.tab_context_ref().len(),
)
.render(rect, buf);
}
diff --git a/src/ui/widgets/tui_tab.rs b/src/ui/widgets/tui_tab.rs
index 3ab0941..24003c1 100644
--- a/src/ui/widgets/tui_tab.rs
+++ b/src/ui/widgets/tui_tab.rs
@@ -1,44 +1,47 @@
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Modifier, Style};
-use tui::text::Span;
+use tui::text::{Span, Spans};
use tui::widgets::{Paragraph, Widget, Wrap};
-use unicode_width::UnicodeWidthStr;
+use uuid::Uuid;
pub struct TuiTabBar<'a> {
- name: &'a str,
- curr: usize,
- len: usize,
+ tabs: &'a [Uuid],
+ index: usize,
}
impl<'a> TuiTabBar<'a> {
- pub fn new(name: &'a str, curr: usize, len: usize) -> Self {
- Self { name, curr, len }
+ pub fn new(tabs: &'a [Uuid], index: usize) -> Self {
+ Self { tabs, index }
}
}
impl<'a> Widget for TuiTabBar<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
- let selected = Style::default()
+ let regular_style = Style::default().fg(Color::White);
+ let selected_style = Style::default()
.fg(Color::White)
.add_modifier(Modifier::REVERSED);
- let str1 = format!("{}/{}", self.curr + 1, self.len);
- let str2 = {
- let space_avail = if str1.width() >= area.width as usize {
- 0
+ let mut spans_vec = vec![];
+ for i in 0..self.tabs.len() {
+ if i == self.index {
+ spans_vec.push(Span::styled(
+ self.tabs[i].to_string()[..4].to_string(),
+ selected_style,
+ ));
+ spans_vec.push(Span::styled(" ", regular_style));
} else {
- area.width as usize - str1.len()
- };
- if space_avail >= self.name.width() {
- self.name
- } else {
- "…"
+ spans_vec.push(Span::styled(
+ self.tabs[i].to_string()[..4].to_string(),
+ regular_style,
+ ));
+ spans_vec.push(Span::styled(" ", regular_style));
}
- };
+ }
- Paragraph::new(Span::styled(format!("{}: {}", str1, str2), selected))
+ Paragraph::new(Spans::from(spans_vec))
.wrap(Wrap { trim: true })
.render(area, buf);
}