Added history file management and fixed #9.

This commit is contained in:
Martin Dvorak 2013-12-15 16:20:41 +01:00
parent a994222f43
commit d078fb02d9
5 changed files with 39 additions and 16 deletions

View file

@ -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();
}

View file

@ -11,7 +11,6 @@
#include "include/hashset.h"
#include "include/hashmap.h"
#include "include/radixsort.h"
#include <stdbool.h>
#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);
}
}

View file

@ -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) {

View file

@ -16,6 +16,7 @@
#include <stdlib.h>
#include <readline/history.h>
#include <unistd.h>
#include <stdbool.h>
#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

View file

@ -14,9 +14,10 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
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