Skip to content

Commit

Permalink
Add K and J keybindings to main menu screen
Browse files Browse the repository at this point in the history
  • Loading branch information
PlankCipher committed Apr 2, 2022
1 parent e24b505 commit 9ae5206
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ make install
## Usage

```
kabmat 2.6.7
kabmat 2.7.0
TUI program for managing kanban boards with vim-like keybindings
Usage: kabmat [OPTION]...
Expand Down
8 changes: 7 additions & 1 deletion doc/kabmat.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH KABMAT 1 2022-04-02 2.6.7
.TH KABMAT 1 2022-04-02 2.7.0

.SH NAME
kabmat \- kanban board management TUI program
Expand Down Expand Up @@ -72,6 +72,12 @@ highlight the first board name
.B G
highlight the last board name
.TP
.B K
move highlighted board up
.TP
.B J
move highlighted board down
.TP
.B d
delete the currently highlighted board
.TP
Expand Down
26 changes: 26 additions & 0 deletions src/DataManager/DataManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ void DataManager::delete_board(string name) {
this->write_data_to_file();
}

bool DataManager::move_board_up(size_t board_index) {
bool moved = false;

if (board_index > 0) {
swap(this->boards[board_index], this->boards[board_index - 1]);
moved = true;
}

this->write_data_to_file();

return moved;
}

bool DataManager::move_board_down(size_t board_index) {
bool moved = false;

if (board_index < this->boards.size() - 1) {
swap(this->boards[board_index], this->boards[board_index + 1]);
moved = true;
}

this->write_data_to_file();

return moved;
}

Board *DataManager::get_board_if_exists(string name) {
for (size_t i = 0; i < this->boards.size(); ++i)
if (this->boards[i].name == name)
Expand Down
3 changes: 3 additions & 0 deletions src/DataManager/DataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class DataManager {
void rename_board(string old_name, string new_name);
void delete_board(string name);

bool move_board_up(size_t board_index);
bool move_board_down(size_t board_index);

Board *get_board_if_exists(string name);
vector<string> get_boards_names();

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/consts.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#define NAME "kabmat"
#define VERSION "2.6.7"
#define VERSION "2.7.0"
#define DATA_FILE (string(getenv("HOME")) + "/.local/share/kabmat/data")
#define DATA_BACKUP_FILE \
(string(getenv("HOME")) + "/.local/share/kabmat/data_bkp")
Expand Down
2 changes: 2 additions & 0 deletions src/ui/components/Help/Help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void Help::show() {
"j highlight the below board name",
"g highlight the first board name",
"G highlight the last board name",
"K move highlighted board up",
"J move highlighted board down",
"d delete the currently highlighted board",
"r rename the currently highlighted board",
"c create a new board and highlight it",
Expand Down
86 changes: 66 additions & 20 deletions src/ui/screens/MainMenu/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,11 @@ void MainMenu::handle_key_press(char key) {
break;
}
case 'k': {
// highlight the above board
if (this->boards_count > 0) {
if (--this->highlighted_index == -1) {
this->highlighted_index = 0;
this->menu_window.scroll_up();
} else
this->highlight_current();
}
this->highlight_above();
break;
}
case 'j': {
// highlight the below board
if (this->boards_count > 0) {
this->highlighted_index =
min(this->boards_count - 1, (size_t)this->highlighted_index + 1);

if (this->highlighted_index == this->menu_window.max_items_in_win) {
this->highlighted_index = this->menu_window.max_items_in_win - 1;
this->menu_window.scroll_down();
} else
this->highlight_current();
}
this->highlight_below();

break;
}
Expand All @@ -163,6 +146,46 @@ void MainMenu::handle_key_press(char key) {

break;
}
case 'K': {
// move highlighted board up
if (this->boards_count > 0) {
size_t highlighted_board_index =
(this->menu_window.window_start - this->boards_names.begin()) +
this->highlighted_index;
size_t prev_offset =
this->menu_window.window_start - this->boards_names.begin();

bool moved = this->data_manager->move_board_up(highlighted_board_index);

if (moved) {
this->boards_names = this->data_manager->get_boards_names();
this->menu_window.scroll_to_offset(prev_offset);
this->highlight_above();
}
}

break;
}
case 'J': {
// move highlighted board down
if (this->boards_count > 0) {
size_t highlighted_board_index =
(this->menu_window.window_start - this->boards_names.begin()) +
this->highlighted_index;
size_t prev_offset =
this->menu_window.window_start - this->boards_names.begin();

bool moved = this->data_manager->move_board_down(highlighted_board_index);

if (moved) {
this->boards_names = this->data_manager->get_boards_names();
this->menu_window.scroll_to_offset(prev_offset);
this->highlight_below();
}
}

break;
}
case 'd': {
// delete highlighted board
if (this->boards_count > 0) {
Expand All @@ -189,7 +212,7 @@ void MainMenu::handle_key_press(char key) {

this->data_manager->delete_board(board_to_delete);

this->boards_names = data_manager->get_boards_names();
this->boards_names = this->data_manager->get_boards_names();
this->boards_count = this->boards_names.size();

this->highlighted_index =
Expand Down Expand Up @@ -264,6 +287,29 @@ void MainMenu::handle_key_press(char key) {
}
}

void MainMenu::highlight_above() {
if (this->boards_count > 0) {
if (--this->highlighted_index == -1) {
this->highlighted_index = 0;
this->menu_window.scroll_up();
} else
this->highlight_current();
}
}

void MainMenu::highlight_below() {
if (this->boards_count > 0) {
this->highlighted_index =
min(this->boards_count - 1, (size_t)this->highlighted_index + 1);

if (this->highlighted_index == this->menu_window.max_items_in_win) {
this->highlighted_index = this->menu_window.max_items_in_win - 1;
this->menu_window.scroll_down();
} else
this->highlight_current();
}
}

string MainMenu::create_input_window(string title, string content,
bool focused) {
int max_y, max_x;
Expand Down
5 changes: 4 additions & 1 deletion src/ui/screens/MainMenu/MainMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class MainMenu {
private:
void setup_window();
void draw_menu_items(vector<string> shown_boards, WINDOW *scrollable_window);
void highlight_current();
void handle_key_press(char key);
string create_input_window(string title, string content = "",
bool focused = false);

void highlight_above();
void highlight_current();
void highlight_below();

DataManager *data_manager;
Config *config;
int highlighted_index;
Expand Down

0 comments on commit 9ae5206

Please sign in to comment.