Repository: tarkah/tickrs (language: rust)

Issue:
Allow reordering ticker tab entries
Just a thought, but perhaps it would be nice to have the ability to reorder tabs after adding them.

This would reflect in the tabs view and also in the summary view.

Currently, you can move forward between tabs with the <kbd>tab</kbd> key, and in the inverse direction with the <kbd>shift</kbd>+<kbd>tab</kbd>.

However, (and maybe it's just me), I can also use the <kbd>ctrl</kbd> key with that to the same effect.

So a <kbd>ctrl</kbd>+<kbd>tab</kbd> has the same effect as a <kbd>tab</kbd>, and so does a <kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>tab</kbd> have the same effect as a <kbd>shift</kbd>+<kbd>tab</kbd>.

Maybe the <kbd>ctrl</kbd> specifier can be used to signify reordering.

So a <kbd>ctrl</kbd>+<kbd>tab</kbd> would move a tab forward by an index, and a <kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>tab</kbd> would move the selected tab and index backwards.

This is just a suggestion and the key specifiers can be anything, I just thought these appropriate since they're apparently redundant in effect and can be repurposed but can be a topic for discussion.


Discussion / hints:
I really like this idea! I've found myself removing tabs and readding them in the order I want, so this would be a nice improvement.
For some extra info, here's what I get with the `SHOW_DEBUG=1` env.

<kbd>tab</kbd>

``` rust
DebugInfo { ..., last_event: Some(Key(KeyEvent { code: Tab, modifiers: NONE })), mode: DisplayStock }
```

<kbd>ctrl</kbd>+<kbd>tab</kbd>

``` rust
DebugInfo { ..., last_event: Some(Key(KeyEvent { code: Tab, modifiers: NONE })), mode: DisplayStock }
```

<kbd>shift</kbd>+<kbd>tab</kbd>

``` rust
DebugInfo { ..., last_event: Some(Key(KeyEvent { code: BackTab, modifiers: NONE })), mode: DisplayStock }
```

<kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>tab</kbd>

``` rust
DebugInfo { ..., last_event: Some(Key(KeyEvent { code: BackTab, modifiers: NONE })), mode: DisplayStock }
```

It doesn't seem to register the <kbd>ctrl</kbd> key
Does ctrl not work with other combinations?
<kbd>ctrl</kbd>+<kbd>v</kbd>

```rust
DebugInfo { ..., last_event: Some(Key(KeyEvent { code: Char('v'), modifiers: CONTROL })), mode: DisplayStock }
```

So, it does

EDIT: does <kbd>ctrl</kbd>+<kbd>tab</kbd> register something different for you?
I'm not at my computer right now, I'll test it tomorrow
Yeah <kbd>ctrl</kbd>+<kbd>tab</kbd> doesn't work for me either, but I noticed the shortcut also gets captured by some terminals (like Windows Terminal), so I don't think it'd be a good fit anyways.

We could do <kbd>ctrl</kbd>+<kbd><-</kbd> and <kbd>ctrl</kbd>+<kbd>-></kbd> ?

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a877be8..b405966 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ and `Removed`.
   - Linux: `$HOME/.config/tickrs/config.yml`
   - macOS: `$HOME/Library/Application Support/tickrs/config.yml`
   - Windows: `%APPDATA%\tickrs\config.yml`
+- Current tab can be reordered by using `Ctrl + Left / Right`
 
 ## [0.10.2] - 2021-02-10
 
diff --git a/src/event.rs b/src/event.rs
index b6a220e..c661d82 100644
--- a/src/event.rs
+++ b/src/event.rs
@@ -98,9 +98,28 @@ pub fn handle_keys_display_stock(
             _ => {}
         }
     } else if key_event.modifiers == KeyModifiers::CONTROL {
-        if let KeyCode::Char('c') = key_event.code {
-            cleanup_terminal();
-            std::process::exit(0);
+        match key_event.code {
+            KeyCode::Left => {
+                let new_idx = if app.current_tab == 0 {
+                    app.stocks.len() - 1
+                } else {
+                    app.current_tab - 1
+                };
+                app.stocks.swap(app.current_tab, new_idx);
+                app.current_tab = new_idx;
+                let _ = request_redraw.try_send(());
+            }
+            KeyCode::Right => {
+                let new_idx = (app.current_tab + 1) % app.stocks.len();
+                app.stocks.swap(app.current_tab, new_idx);
+                app.current_tab = new_idx;
+                let _ = request_redraw.try_send(());
+            }
+            KeyCode::Char('c') => {
+                cleanup_terminal();
+                std::process::exit(0);
+            }
+            _ => {}
         }
     } else if key_event.modifiers == KeyModifiers::SHIFT {
         if let KeyCode::Char('?') = key_event.code {
diff --git a/src/widget/help.rs b/src/widget/help.rs
index fb18fa0..9366ba5 100644
--- a/src/widget/help.rs
+++ b/src/widget/help.rs
@@ -18,6 +18,9 @@ Remove Stock:
 Change Tab:
   - <Tab>: next stock
   - <Shift+Tab>: previous stock
+Reorder Current Tab:
+  - <Ctrl+Left>: move 1 tab left
+  - <Ctrl+Right>: move 1 tab right
 Change Time Frame:
   - <Right>: next time frame
   - <Left>: previous time frame
@@ -36,8 +39,8 @@ Graphing Display:
   - x: toggle labels
 "#;
 
-pub const HELP_WIDTH: u16 = 36;
-pub const HELP_HEIGHT: u16 = 30;
+pub const HELP_WIDTH: u16 = 37;
+pub const HELP_HEIGHT: u16 = 33;
 
 #[derive(Copy, Clone)]
 pub struct HelpWidget {}


Test patch: