diff --git a/src/hstr.c b/src/hstr.c index 51b6058..0a16e41 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -21,7 +21,7 @@ #include "include/hstr_history.h" #define LABEL_HISTORY " HISTORY " -#define LABEL_HELP "Type to filter history, use UP and DOWN arrows to navigate, ENTER to select" +#define LABEL_HELP "Type to filter history, use UP and DOWN arrows to navigate, Ctrl-r to delete item, ENTER to select" #define SELECTION_CURSOR_IN_PROMPT -1 #define Y_OFFSET_PROMPT 0 @@ -32,6 +32,7 @@ #define KEY_TERMINAL_RESIZE 410 #define KEY_CTRL_A 1 #define KEY_CTRL_E 5 +#define KEY_CTRL_R 18 #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) @@ -70,6 +71,12 @@ void print_help_label(WINDOW *win) { refresh(); } +void print_cmd_deleted_label(WINDOW *win, char *cmd, int occurences) { + mvwprintw(win, Y_OFFSET_HELP, 0, "History item '%s' deleted (%d occurrences)", cmd, occurences); + clrtoeol(); + refresh(); +} + void print_history_label(WINDOW *win) { char message[512]; @@ -216,10 +223,10 @@ char *selection_loop(HistoryItems *history) { int selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; int previousSelectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; - int y = 1, c, maxHistoryItems, cursorX, cursorY; + int y = 1, c, maxHistoryItems, cursorX, cursorY, deleteOccurences; bool done = FALSE; char prefix[500]=""; - char *result=""; + char *result="", *delete; while (!done) { maxHistoryItems=get_max_history_items(stdscr); @@ -232,6 +239,14 @@ char *selection_loop(HistoryItems *history) { case KEY_CTRL_A: case KEY_CTRL_E: break; + case KEY_CTRL_R: + if(selectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) { + delete=selection[selectionCursorPosition]; + deleteOccurences=history_mgmt_remove(delete); + print_cmd_deleted_label(stdscr, delete, deleteOccurences); + move(y, basex+strlen(prefix)); + } + break; case 91: // TODO 91 killed > debug to determine how to distinguish \e and [ break; @@ -315,8 +330,10 @@ char *selection_loop(HistoryItems *history) { void hstr() { HistoryItems *history=get_prioritized_history(); + history_mgmt_open(); char *command = selection_loop(history); - fill_terminal_input(command); + history_mgmt_close(); + fill_terminal_input(command, true); free_prioritized_history(); free_history_items(); } diff --git a/src/hstr_history.c b/src/hstr_history.c index c5b9d02..5ff24e3 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -11,7 +11,6 @@ #include "include/hashset.h" #include "include/hashmap.h" #include "include/radixsort.h" -#include #include "include/hstr_utils.h" @@ -173,21 +172,22 @@ void history_mgmt_open() { dirty=false; } -void history_mgmt_remove(char *cmd) { - get_history_items(); - - int offset=history_search_pos(cmd, 0, 0); +int history_mgmt_remove(char *cmd) { + int offset=history_search_pos(cmd, 0, 0), occurences=0; while(offset>=0) { + occurences++; free_history_entry(remove_history(offset)); offset=history_search_pos(cmd, 0, ++offset); } - - write_history(get_history_file_name()); - dirty=true; + if(occurences) { + write_history(get_history_file_name()); + dirty=true; + } + return occurences; } void history_mgmt_close() { if(dirty) { - fill_terminal_input("history -r\n"); + fill_terminal_input("history -r\n", false); } } diff --git a/src/hstr_utils.c b/src/hstr_utils.c index fb1bcb2..97d5032 100644 --- a/src/hstr_utils.c +++ b/src/hstr_utils.c @@ -19,7 +19,7 @@ void tiocsti() { } } -void fill_terminal_input(char *cmd){ +void fill_terminal_input(char *cmd, bool padding){ size_t size = strlen(cmd); unsigned i; char *c; @@ -28,7 +28,7 @@ void fill_terminal_input(char *cmd){ c=(cmd+i); ioctl(0, TIOCSTI, c); } - printf("\n"); + if(padding) printf("\n"); } void reverse_char_pointer_array(char **array, unsigned length) { diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index 198c2c9..00b8a1b 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -16,6 +16,7 @@ #include #include #include +#include #include "hstr_utils.h" #define ENV_VAR_USER "USER" @@ -37,4 +38,8 @@ void free_history_items(); HistoryItems *prioritize_history(HistoryItems *historyFileItems); void free_prioritized_history(); +void history_mgmt_open(); +int history_mgmt_remove(char *cmd); +void history_mgmt_close(); + #endif diff --git a/src/include/hstr_utils.h b/src/include/hstr_utils.h index 451f747..f887125 100644 --- a/src/include/hstr_utils.h +++ b/src/include/hstr_utils.h @@ -14,9 +14,10 @@ #include #include #include +#include void tiocsti(); -void fill_terminal_input(char* cmd); +void fill_terminal_input(char* cmd, bool padding); void reverse_char_pointer_array(char **array, unsigned length); #endif