mirror of
https://github.com/dvorka/hstr.git
synced 2024-12-25 00:44:26 +08:00
Added history ranking, filtering and blacklisting. Enabled by the
implementation of hash map (based on hash set) and radix sort. Memory not freed.
This commit is contained in:
parent
87d4d5844c
commit
273c8456e2
6 changed files with 107 additions and 28 deletions
19
src/hstr.c
19
src/hstr.c
|
@ -52,7 +52,7 @@
|
|||
#endif
|
||||
|
||||
static char **selection=NULL;
|
||||
static int selectionSize=0;
|
||||
static unsigned selectionSize=0;
|
||||
static bool terminalHasColors=FALSE;
|
||||
|
||||
|
||||
|
@ -80,7 +80,7 @@ void print_history_label(WINDOW *win) {
|
|||
|
||||
strcpy(message, LABEL_HISTORY);
|
||||
width -= strlen(LABEL_HISTORY);
|
||||
int i;
|
||||
unsigned i;
|
||||
for (i=0; i < width; i++) {
|
||||
strcat(message, " ");
|
||||
}
|
||||
|
@ -92,12 +92,12 @@ void print_history_label(WINDOW *win) {
|
|||
refresh();
|
||||
}
|
||||
|
||||
int get_max_history_items(WINDOW *win) {
|
||||
unsigned get_max_history_items(WINDOW *win) {
|
||||
return (getmaxy(win)-(Y_OFFSET_ITEMS+2));
|
||||
}
|
||||
|
||||
|
||||
void alloc_selection(int size) {
|
||||
void alloc_selection(unsigned size) {
|
||||
selectionSize=size;
|
||||
if(selection!=NULL) {
|
||||
free(selection);
|
||||
|
@ -108,9 +108,9 @@ void alloc_selection(int size) {
|
|||
}
|
||||
}
|
||||
|
||||
int make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) {
|
||||
unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) {
|
||||
alloc_selection(sizeof(char*) * maxSelectionCount); // TODO realloc
|
||||
int i, selectionCount=0;
|
||||
unsigned i, selectionCount=0;
|
||||
|
||||
HashSet set;
|
||||
hashset_init(&set);
|
||||
|
@ -145,15 +145,15 @@ int make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) {
|
|||
return selectionCount;
|
||||
}
|
||||
|
||||
char *print_selection(WINDOW *win, int maxHistoryItems, char *prefix, HistoryItems *history) {
|
||||
char *print_selection(WINDOW *win, unsigned maxHistoryItems, char *prefix, HistoryItems *history) {
|
||||
char *result="";
|
||||
int selectionCount=make_selection(prefix, history, maxHistoryItems);
|
||||
unsigned selectionCount=make_selection(prefix, history, maxHistoryItems);
|
||||
if (selectionCount > 0) {
|
||||
result = selection[0];
|
||||
}
|
||||
|
||||
int height=get_max_history_items(win);
|
||||
int i;
|
||||
unsigned i;
|
||||
int y=Y_OFFSET_ITEMS;
|
||||
|
||||
move(Y_OFFSET_ITEMS, 0);
|
||||
|
@ -245,7 +245,6 @@ char *selection_loop(HistoryItems *history) {
|
|||
break;
|
||||
case 91:
|
||||
// TODO 91 killed > debug to determine how to distinguish \e and [
|
||||
//mvprintw(Y_OFFSET_HELP, 0, "91 killed");
|
||||
break;
|
||||
case KEY_BACKSPACE:
|
||||
case 127:
|
||||
|
|
|
@ -1,4 +1,12 @@
|
|||
#include "include/hstr_history.h"
|
||||
#include "include/hashset.h"
|
||||
#include "include/hashmap.h"
|
||||
#include "include/radixsort.h"
|
||||
|
||||
typedef struct {
|
||||
char *item;
|
||||
unsigned rank;
|
||||
} RankedHistoryItem;
|
||||
|
||||
static HistoryItems *history;
|
||||
static HistoryItems *prioritizedHistory;
|
||||
|
@ -13,16 +21,90 @@ char *get_history_file() {
|
|||
return fileName;
|
||||
}
|
||||
|
||||
#define history_ranking_function(RANK, NEWORDEROCCURENCE) (RANK?RANK+NEWORDEROCCURENCE:NEWORDEROCCURENCE)
|
||||
|
||||
void dump_prioritized_history(HistoryItems *ph) {
|
||||
printf("\n\nPrioritized history:");
|
||||
int i;
|
||||
for(i=0; i<ph->count; i++) {
|
||||
if(ph->items[i]!=NULL) {
|
||||
printf("\n%s",ph->items[i]); fflush(stdout);
|
||||
} else {
|
||||
printf("\n %d NULL",i); fflush(stdout);
|
||||
}
|
||||
}
|
||||
printf("\n"); fflush(stdout);
|
||||
}
|
||||
|
||||
HistoryItems *prioritize_history(HistoryItems *historyFileItems) {
|
||||
return historyFileItems;
|
||||
HashMap rankmap;
|
||||
hashmap_init(&rankmap);
|
||||
|
||||
HashSet blacklist;
|
||||
hashset_init(&blacklist);
|
||||
hashset_add(&blacklist, "ls");
|
||||
hashset_add(&blacklist, "pwd");
|
||||
hashset_add(&blacklist, "cd");
|
||||
hashset_add(&blacklist, "hh");
|
||||
|
||||
RadixSorter rs;
|
||||
radixsort_init(&rs);
|
||||
|
||||
RankedHistoryItem *r;
|
||||
RadixItem *radixItem;
|
||||
int i;
|
||||
for(i=0; i<historyFileItems->count; i++) {
|
||||
if(hashset_contains(&blacklist, historyFileItems->items[i])) {
|
||||
continue;
|
||||
}
|
||||
if((r=hashmap_get(&rankmap, historyFileItems->items[i]))==NULL) {
|
||||
r=(RankedHistoryItem *)malloc(sizeof(RankedHistoryItem));
|
||||
r->rank=history_ranking_function(0, i);
|
||||
r->item=historyFileItems->items[i];
|
||||
|
||||
hashmap_put(&rankmap, historyFileItems->items[i], r);
|
||||
|
||||
radixItem=(RadixItem *)malloc(sizeof(RadixItem));
|
||||
radixItem->key=r->rank;
|
||||
radixItem->data=r;
|
||||
radixItem->next=NULL;
|
||||
radixsort_add(&rs, radixItem);
|
||||
} else {
|
||||
//printf("\n>>> %s ", r->item); fflush(stdout);
|
||||
radixItem=radix_cut(&rs, r->rank, r);
|
||||
|
||||
if(radixItem!=NULL) {
|
||||
r->rank=history_ranking_function(r->rank, i);
|
||||
radixItem->key=r->rank;
|
||||
radixsort_add(&rs, radixItem);
|
||||
} // TODO else assert
|
||||
}
|
||||
}
|
||||
|
||||
RadixItem **prioritizedRadix=radixsort_dump(&rs);
|
||||
prioritizedHistory=(HistoryItems *)malloc(sizeof(HistoryItems));
|
||||
prioritizedHistory->count=rs.size;
|
||||
prioritizedHistory->items=malloc(rs.size * sizeof(char*));
|
||||
for(i=0; i<rs.size; i++) {
|
||||
printf("\n %d %p ",i,prioritizedRadix[i]->data);
|
||||
if(prioritizedRadix[i]->data) {
|
||||
prioritizedHistory->items[i]=((RankedHistoryItem *)(prioritizedRadix[i]->data))->item;
|
||||
}
|
||||
printf("\n %d %s ",i,((RankedHistoryItem *)(prioritizedRadix[i]->data))->item);
|
||||
}
|
||||
|
||||
radixsort_destroy(&rs);
|
||||
|
||||
return prioritizedHistory;
|
||||
}
|
||||
|
||||
void free_prioritized_history() {
|
||||
//free(prioritizedHistory->items);
|
||||
//free(prioritizedHistory);
|
||||
// TODO free(prioritizedHistory->items);
|
||||
// TODO free(prioritizedHistory);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef GET_HISTORY_FROM_FILE
|
||||
|
||||
static char *historyAsString;
|
||||
|
|
|
@ -12,7 +12,7 @@ void tiocsti() {
|
|||
|
||||
void fill_terminal_input(char *cmd){
|
||||
size_t size = strlen(cmd);
|
||||
int i;
|
||||
unsigned i;
|
||||
char *c;
|
||||
for (i = 0; i < size; i++) {
|
||||
// terminal I/O control, simulate terminal input
|
||||
|
@ -22,9 +22,9 @@ void fill_terminal_input(char *cmd){
|
|||
printf("\n");
|
||||
}
|
||||
|
||||
void reverse_char_pointer_array(char **array, int length) {
|
||||
int i;
|
||||
void reverse_char_pointer_array(char **array, unsigned length) {
|
||||
char *temp;
|
||||
unsigned i;
|
||||
for (i=0; i<length/2; i++) {
|
||||
temp = array[i];
|
||||
array[i] = array[length-i-1];
|
||||
|
|
|
@ -12,18 +12,16 @@ struct HashNode {
|
|||
struct HashNode *next;
|
||||
};
|
||||
|
||||
struct HashSetStruct {
|
||||
typedef struct {
|
||||
struct HashNode * lists[TABLE_SIZE];
|
||||
int currentSize;
|
||||
};
|
||||
} HashSet;
|
||||
|
||||
typedef struct HashSetStruct HashSet;
|
||||
|
||||
void hashset_init( HashSet * hs );
|
||||
int hashset_contains( const HashSet * hs, const char *key );
|
||||
int hashset_add( HashSet * hs, const char *key );
|
||||
int hashset_remove( HashSet * hs, const char *key );
|
||||
int hashset_size( const HashSet * hs );
|
||||
void hashset_print( const HashSet * hs );
|
||||
void hashset_init( HashSet *hs );
|
||||
int hashset_contains( const HashSet *hs, const char *key );
|
||||
int hashset_add( HashSet *hs, const char *key );
|
||||
int hashset_remove( HashSet *hs, const char *key );
|
||||
int hashset_size( const HashSet *hs );
|
||||
void hashset_print( const HashSet *hs );
|
||||
|
||||
#endif
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
typedef struct {
|
||||
char **items;
|
||||
int count;
|
||||
unsigned count;
|
||||
} HistoryItems;
|
||||
|
||||
HistoryItems *get_history_items();
|
||||
|
|
|
@ -8,6 +8,6 @@
|
|||
|
||||
void tiocsti();
|
||||
void fill_terminal_input(char* cmd);
|
||||
void reverse_char_pointer_array(char **array, int length);
|
||||
void reverse_char_pointer_array(char **array, unsigned length);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue