From 4d3f46e26e8322c49f227c6b0ff30443da82a168 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Mon, 17 Mar 2014 00:04:51 +0100 Subject: [PATCH 01/13] Initial sync commit of favorites with fake loading. --- src/hstr_favorites.c | 47 ++++++++++++++++++++++++++++++++++++ src/include/hstr_favorites.h | 28 +++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/hstr_favorites.c create mode 100644 src/include/hstr_favorites.h diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c new file mode 100644 index 0000000..ee38533 --- /dev/null +++ b/src/hstr_favorites.c @@ -0,0 +1,47 @@ +/* + ============================================================================ + Name : hstr_favorites.c + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Favorite commands. + ============================================================================ +*/ + +#include "include/hstr_favorites.h" + +FavoriteItems *favorites; + +FavoriteItems *favorites_init() { + favorites=malloc(sizeof(FavoriteItems)); + return favorites; +} + +FavoriteItems *favorites_load() { + // TODO fake initialization + favorites->count=3; + favorites->items=malloc(sizeof(char *)*favorites->count); + favorites->items[0]="a"; + favorites->items[1]="b"; + favorites->items[2]="c"; + + return favorites; +} + +void favorites_add(char *newFavorite) { + favorites->items=realloc(favorites->items, sizeof(char *)*favorites->count); + favorites->items[favorites->count++]=newFavorite; +} + +void favorites_save() { +} + +void favorites_close() { + if(favorites) { + if(favorites->count) { + int i; + for(i=0; icount; i++) { + free(favorites->items[i]); + } + } + } +} diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h new file mode 100644 index 0000000..b60c9d9 --- /dev/null +++ b/src/include/hstr_favorites.h @@ -0,0 +1,28 @@ +/* + ============================================================================ + Name : hstr_favorites.h + Author : martin.dvorak@midforger.com + Copyright : Apache 2.0 + Description : Favorite commands. + ============================================================================ +*/ + +#ifndef _HSTR_FAVORITES_H_ +#define _HSTR_FAVORITES_H_ + +#include + +#define HH_RC_FILE ".hhrc" + +typedef struct { + char **items; + unsigned count; +} FavoriteItems; + +FavoriteItems *favorites_init(); +FavoriteItems *favorites_load(); +void favorites_add(); +void favorites_save(); +void favorites_close(); + +#endif From 683289f15ce687817a1881d552127453ec585a1d Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sun, 23 Mar 2014 03:37:36 +0100 Subject: [PATCH 02/13] View rotation, fake in memory favorites, selection SIGSEGV. --- src/hstr.c | 42 ++++++++++++++++++++++++++++-------- src/hstr_favorites.c | 29 +++++++------------------ src/hstr_history.c | 15 +++++++++++++ src/include/hstr_favorites.h | 9 ++++---- src/include/hstr_history.h | 2 ++ 5 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/hstr.c b/src/hstr.c index b0c3fbb..26e21e2 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -60,11 +60,15 @@ #define HH_COLOR_PROMPT 3 #define HH_COLOR_DELETE 4 -#define ENV_VAR_HH_CONFIG "HH_CONFIG" +#define HH_ENV_VAR_CONFIG "HH_CONFIG" #define HH_CONFIG_HICOLOR "hicolor" #define HH_CONFIG_CASE "casesensitive" #define HH_CONFIG_SORTING "rawhistory" +#define HH_VIEW_RANKING 0 +#define HH_VIEW_HISTORY 1 +#define HH_VIEW_FAVORITES 2 + #define SPACE_PADDING " " #ifdef DEBUG_KEYS @@ -79,6 +83,11 @@ #define LOGCURSOR(Y) #endif +static const char *HH_VIEW_LABELS[]={ + "ranking", + "history", + "favorites"}; + static const char *INSTALL_STRING= "\n# add this configuration to ~/.bashrc" "\nexport HH_CONFIG=hicolor # get more colors" @@ -108,14 +117,14 @@ static const char *LABEL_HELP= static char **selection=NULL; static unsigned selectionSize=0; static bool caseSensitive=FALSE; -static bool defaultOrder=FALSE; +static int historyView=HH_VIEW_RANKING; static bool hicolor=FALSE; static char screenLine[CMDLINE_LNG]; static char cmdline[CMDLINE_LNG]; void get_env_configuration() { - char *hhconfig=getenv(ENV_VAR_HH_CONFIG); + char *hhconfig=getenv(HH_ENV_VAR_CONFIG); if(hhconfig && strlen(hhconfig)>0) { if(strstr(hhconfig,HH_CONFIG_HICOLOR)) { hicolor=TRUE; @@ -124,7 +133,7 @@ void get_env_configuration() caseSensitive=TRUE; } if(strstr(hhconfig,HH_CONFIG_SORTING)) { - defaultOrder=TRUE; + historyView=TRUE; } } } @@ -179,9 +188,9 @@ void print_cmd_deleted_label(char *cmd, int occurences) void print_history_label(HistoryItems *history) { int width=getmaxx(stdscr); - snprintf(screenLine, width, "- HISTORY - case:%s (C-t) - order:%s (C-/) - %d/%d ", + snprintf(screenLine, width, "- HISTORY - case:%s (C-t) - view:%s (C-/) - %d/%d ", (caseSensitive?"sensitive":"insensitive"), - (defaultOrder?"history":"ranking"), + HH_VIEW_LABELS[historyView], history->count, history->rawCount); width -= strlen(screenLine); @@ -236,8 +245,23 @@ unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCou realloc_selection(sizeof(char*) * maxSelectionCount); unsigned i, selectionCount=0; - char **source=(defaultOrder?history->raw:history->items); - unsigned count=(defaultOrder?history->rawCount:history->count); + char **source; + unsigned count; + + switch(historyView) { + case HH_VIEW_RANKING: + source=history->items; + count=history->count; + break; + case HH_VIEW_HISTORY: + source=history->raw; + count=history->rawCount; + break; + case HH_VIEW_FAVORITES: + source=history->favorites; + count=history->favoritesCount; + break; + } for(i=0; iitems=NULL; + favorites->count=0; } -FavoriteItems *favorites_load() { - // TODO fake initialization +void favorites_load(FavoriteItems *favorites) { + // TODO fake initialization instead of .hhrc load favorites->count=3; favorites->items=malloc(sizeof(char *)*favorites->count); favorites->items[0]="a"; favorites->items[1]="b"; favorites->items[2]="c"; - - return favorites; } -void favorites_add(char *newFavorite) { +void favorites_add(FavoriteItems *favorites, char *newFavorite) { favorites->items=realloc(favorites->items, sizeof(char *)*favorites->count); favorites->items[favorites->count++]=newFavorite; } -void favorites_save() { -} - -void favorites_close() { - if(favorites) { - if(favorites->count) { - int i; - for(i=0; icount; i++) { - free(favorites->items[i]); - } - } - } +void favorites_save(FavoriteItems *favorites) { } diff --git a/src/hstr_history.c b/src/hstr_history.c index db01669..2d4b2ca 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -11,6 +11,7 @@ #include #include #include "include/hstr_history.h" +#include "include/hstr_favorites.h" #define NDEBUG #include @@ -150,6 +151,13 @@ HistoryItems *get_prioritized_history() radixsort_destroy(&rs); + + FavoriteItems favoriteItems; + favorites_init(&favoriteItems); + favorites_load(&favoriteItems); + prioritizedHistory->favorites=favoriteItems.items; + prioritizedHistory->favoritesCount=favoriteItems.count; + return prioritizedHistory; } else { return NULL; @@ -159,6 +167,13 @@ HistoryItems *get_prioritized_history() void free_prioritized_history() { free(prioritizedHistory->items); + if(prioritizedHistory->favorites) { + int i; + for(i=0; ifavoritesCount; i++) { + free(prioritizedHistory->favorites[i]); + } + free(prioritizedHistory->favorites); + } free(prioritizedHistory); } diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index b60c9d9..d6aeb6e 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -19,10 +19,9 @@ typedef struct { unsigned count; } FavoriteItems; -FavoriteItems *favorites_init(); -FavoriteItems *favorites_load(); -void favorites_add(); -void favorites_save(); -void favorites_close(); +void favorites_init(FavoriteItems *favorites); +void favorites_load(FavoriteItems *favorites); +void favorites_add(FavoriteItems *favorites, char *favorite); +void favorites_save(FavoriteItems *favorites); #endif diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index 0c7ef55..b9451ae 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -30,8 +30,10 @@ typedef struct { char **items; char **raw; + char **favorites; unsigned count; unsigned rawCount; + unsigned favoritesCount; } HistoryItems; HistoryItems *get_prioritized_history(); From a8337ad952515dedd560d0cd15895ba3321a9148 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sun, 23 Mar 2014 03:50:37 +0100 Subject: [PATCH 03/13] Fake in-memory favorites work without adding and load/save. --- src/hstr_favorites.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index dc423af..1dbfd81 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -7,28 +7,36 @@ ============================================================================ */ +#include #include "include/hstr_favorites.h" #define FAVORITE_SEGMENT_SIZE 10 -void favorites_init(FavoriteItems *favorites) { +void favorites_init(FavoriteItems *favorites) +{ favorites->items=NULL; favorites->count=0; } -void favorites_load(FavoriteItems *favorites) { +void favorites_load(FavoriteItems *favorites) +{ // TODO fake initialization instead of .hhrc load - favorites->count=3; + favorites->count=2; favorites->items=malloc(sizeof(char *)*favorites->count); - favorites->items[0]="a"; - favorites->items[1]="b"; - favorites->items[2]="c"; + favorites->items[0]=malloc(2); + strcpy(favorites->items[0],"a"); + favorites->items[1]=malloc(2); + strcpy(favorites->items[1],"b"); } -void favorites_add(FavoriteItems *favorites, char *newFavorite) { +void favorites_add(FavoriteItems *favorites, char *newFavorite) +{ favorites->items=realloc(favorites->items, sizeof(char *)*favorites->count); favorites->items[favorites->count++]=newFavorite; + + favorites_save(favorites); } -void favorites_save(FavoriteItems *favorites) { +void favorites_save(FavoriteItems *favorites) +{ } From 1aab89187f1e15a6ba39275cc17f1ec80fd42bd6 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sun, 23 Mar 2014 06:05:36 +0100 Subject: [PATCH 04/13] Favorites: in memory adding sketched. --- src/hstr.c | 18 +++++++++++++-- src/hstr_favorites.c | 45 ++++++++++++++++++++++++++++++++++-- src/hstr_history.c | 18 ++++----------- src/include/hstr_favorites.h | 3 +++ src/include/hstr_history.h | 4 ++-- 5 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/hstr.c b/src/hstr.c index 26e21e2..374c4d5 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -38,6 +38,7 @@ #define K_CTRL_A 1 #define K_CTRL_E 5 +#define K_CTRL_F 6 #define K_CTRL_G 7 #define K_CTRL_H 8 #define K_CTRL_L 12 @@ -258,8 +259,8 @@ unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCou count=history->rawCount; break; case HH_VIEW_FAVORITES: - source=history->favorites; - count=history->favoritesCount; + source=history->favorites->items; + count=history->favorites->count; break; } @@ -497,6 +498,19 @@ void loop_to_select(HistoryItems *history) print_history_label(history); selectionCursorPosition=0; break; + case K_CTRL_F: + if(selectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) { + result=selection[selectionCursorPosition]; + + if(historyView==HH_VIEW_FAVORITES) { + favorites_choose(history->favorites, result); + } else { + favorites_add(history->favorites, result); + } + result=print_selection(maxHistoryItems, pattern, history); + selectionCursorPosition=0; + } + break; case KEY_RESIZE: print_history_label(history); result=print_selection(maxHistoryItems, pattern, history); diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index 1dbfd81..905ee57 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -7,6 +7,7 @@ ============================================================================ */ +#include #include #include "include/hstr_favorites.h" @@ -31,8 +32,37 @@ void favorites_load(FavoriteItems *favorites) void favorites_add(FavoriteItems *favorites, char *newFavorite) { - favorites->items=realloc(favorites->items, sizeof(char *)*favorites->count); - favorites->items[favorites->count++]=newFavorite; + favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); + favorites->items[favorites->count-1]=newFavorite; + + favorites_choose(favorites, newFavorite); + favorites_save(favorites); +} + +void favorites_choose(FavoriteItems *favorites, char *choice) +{ + if(favorites->count && choice) { + int i; + char *b=0, *next; + for(i=0; icount; i++) { + if(!strcmp(favorites->items[i],choice)) { + favorites->items[0]=favorites->items[i]; + if(b) { + favorites->items[i]=b; + } + return; + } + next=favorites->items[i]; + favorites->items[i]=b; + b=next; + } + } + favorites_save(favorites); +} + +void favorites_remove(FavoriteItems *favorites, char *almostDead) +{ + // TODO: keept slot you have, just change count favorites_save(favorites); } @@ -40,3 +70,14 @@ void favorites_add(FavoriteItems *favorites, char *newFavorite) void favorites_save(FavoriteItems *favorites) { } + +void favorites_destroy(FavoriteItems *favorites) +{ + if(favorites) { + int i; + for(i=0; icount; i++) { + free(favorites->items[i]); + } + free(favorites); + } +} diff --git a/src/hstr_history.c b/src/hstr_history.c index 2d4b2ca..536292f 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -11,7 +11,6 @@ #include #include #include "include/hstr_history.h" -#include "include/hstr_favorites.h" #define NDEBUG #include @@ -152,11 +151,10 @@ HistoryItems *get_prioritized_history() radixsort_destroy(&rs); - FavoriteItems favoriteItems; - favorites_init(&favoriteItems); - favorites_load(&favoriteItems); - prioritizedHistory->favorites=favoriteItems.items; - prioritizedHistory->favoritesCount=favoriteItems.count; + FavoriteItems *favoriteItems=malloc(sizeof(FavoriteItems)); + favorites_init(favoriteItems); + favorites_load(favoriteItems); + prioritizedHistory->favorites=favoriteItems; return prioritizedHistory; } else { @@ -167,13 +165,7 @@ HistoryItems *get_prioritized_history() void free_prioritized_history() { free(prioritizedHistory->items); - if(prioritizedHistory->favorites) { - int i; - for(i=0; ifavoritesCount; i++) { - free(prioritizedHistory->favorites[i]); - } - free(prioritizedHistory->favorites); - } + favorites_destroy(prioritizedHistory->favorites); free(prioritizedHistory); } diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index d6aeb6e..b134f9f 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -22,6 +22,9 @@ typedef struct { void favorites_init(FavoriteItems *favorites); void favorites_load(FavoriteItems *favorites); void favorites_add(FavoriteItems *favorites, char *favorite); +void favorites_choose(FavoriteItems *favorites, char *choice); +void favorites_remove(FavoriteItems *favorites, char *almostDead); void favorites_save(FavoriteItems *favorites); +void favorites_destroy(FavoriteItems *favorites); #endif diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index b9451ae..e38fb68 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -17,6 +17,7 @@ #include #include #include +#include "hstr_favorites.h" #include "hstr_utils.h" #include "hashset.h" #include "radixsort.h" @@ -30,10 +31,9 @@ typedef struct { char **items; char **raw; - char **favorites; + FavoriteItems *favorites; unsigned count; unsigned rawCount; - unsigned favoritesCount; } HistoryItems; HistoryItems *get_prioritized_history(); From 0f32fe0c657da22ef6215700dfbf3b2f52f87a63 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Thu, 27 Mar 2014 14:16:22 +0100 Subject: [PATCH 05/13] Sync commit before branch switch. --- src/hstr_favorites.c | 36 ++++++++++++++++++++++++++++++++++++ src/include/hstr_favorites.h | 5 ++++- src/include/hstr_history.h | 4 +--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index 905ee57..83a2695 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -8,7 +8,9 @@ */ #include +#include #include +#include #include "include/hstr_favorites.h" #define FAVORITE_SEGMENT_SIZE 10 @@ -28,6 +30,38 @@ void favorites_load(FavoriteItems *favorites) strcpy(favorites->items[0],"a"); favorites->items[1]=malloc(2); strcpy(favorites->items[1],"b"); + + lazy loading (boolean indicator to FavoriteItems) + + // TODO load from file + + char *home = getenv(ENV_VAR_HOME); + char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1); + strcpy(fileName,home); + strcat(fileName,"/"); + strcat(fileName,FILE_HH_RC); + + char *file_contents=NULL; + if(access(fileName, F_OK) != -1) { + long input_file_size; + + FILE *input_file = fopen(fileName, "rb"); + fseek(input_file, 0, SEEK_END); + input_file_size = ftell(input_file); + rewind(input_file); + file_contents = malloc((input_file_size + 1) * (sizeof(char))); + if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { + exit(EXIT_FAILURE); + } + fclose(input_file); + file_contents[input_file_size] = 0; + } else { + fprintf(stderr,"\nHistory file not found: %s\n",fileName); + } + + if(file_contents) { + split & process & initilize favorites + } } void favorites_add(FavoriteItems *favorites, char *newFavorite) @@ -69,6 +103,8 @@ void favorites_remove(FavoriteItems *favorites, char *almostDead) void favorites_save(FavoriteItems *favorites) { + create/update history file + } void favorites_destroy(FavoriteItems *favorites) diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index b134f9f..45d4e91 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -12,7 +12,10 @@ #include -#define HH_RC_FILE ".hhrc" +#define ENV_VAR_USER "USER" +#define ENV_VAR_HOME "HOME" + +#define FILE_HH_RC ".hhrc" typedef struct { char **items; diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index e38fb68..9f78b8b 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -22,11 +22,9 @@ #include "hashset.h" #include "radixsort.h" -#define ENV_VAR_USER "USER" -#define ENV_VAR_HOME "HOME" #define ENV_VAR_HISTFILE "HISTFILE" -#define DEFAULT_HISTORY_FILE ".bash_history" +#define FILE_DEFAULT_HISTORY ".bash_history" typedef struct { char **items; From 105d66c75d883b8da24364c0b1e3602d09c356ae Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sat, 29 Mar 2014 13:19:15 +0100 Subject: [PATCH 06/13] Loading of favorites from resource file added. --- src/hstr_favorites.c | 99 +++++++++++++++++++++--------------- src/include/hstr_favorites.h | 6 +-- 2 files changed, 62 insertions(+), 43 deletions(-) diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index 83a2695..ad30018 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -19,57 +19,75 @@ void favorites_init(FavoriteItems *favorites) { favorites->items=NULL; favorites->count=0; + favorites->loaded=false; } -void favorites_load(FavoriteItems *favorites) +void favorites_get(FavoriteItems *favorites) { - // TODO fake initialization instead of .hhrc load - favorites->count=2; - favorites->items=malloc(sizeof(char *)*favorites->count); - favorites->items[0]=malloc(2); - strcpy(favorites->items[0],"a"); - favorites->items[1]=malloc(2); - strcpy(favorites->items[1],"b"); + if(!favorites->loaded) { + char *home = getenv(ENV_VAR_HOME); + char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1); + strcpy(fileName,home); + strcat(fileName,"/"); + strcat(fileName,FILE_HH_RC); - lazy loading (boolean indicator to FavoriteItems) + char *file_contents=NULL; + if(access(fileName, F_OK) != -1) { + long input_file_size; - // TODO load from file + FILE *input_file = fopen(fileName, "rb"); + fseek(input_file, 0, SEEK_END); + input_file_size = ftell(input_file); + rewind(input_file); + file_contents = malloc((input_file_size + 1) * (sizeof(char))); + if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { + exit(EXIT_FAILURE); + } + fclose(input_file); + file_contents[input_file_size] = 0; - char *home = getenv(ENV_VAR_HOME); - char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1); - strcpy(fileName,home); - strcat(fileName,"/"); - strcat(fileName,FILE_HH_RC); + if(file_contents && strlen(file_contents)) { + favorites->count = 0; + char *p=strchr(file_contents,'\n'); + while (p!=NULL) { + favorites->count++; + p=strchr(p+1,'\n'); + } - char *file_contents=NULL; - if(access(fileName, F_OK) != -1) { - long input_file_size; - - FILE *input_file = fopen(fileName, "rb"); - fseek(input_file, 0, SEEK_END); - input_file_size = ftell(input_file); - rewind(input_file); - file_contents = malloc((input_file_size + 1) * (sizeof(char))); - if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { - exit(EXIT_FAILURE); + favorites->items = malloc(sizeof(char*) * favorites->count); + int i = 0; + char *pb=file_contents, *pe; + pe=strchr(file_contents, '\n'); + while(pe!=NULL) { + favorites->items[i]=pb; + *pe=0; + favorites->items[i]=strdup(pb); + pb=pe+1; + pe=strchr(pb, '\n'); + i++; + } + free(file_contents); + } + } else { + // favorites file not found > favorites don't exist yet + favorites->loaded=true; + return; } - fclose(input_file); - file_contents[input_file_size] = 0; - } else { - fprintf(stderr,"\nHistory file not found: %s\n",fileName); - } - - if(file_contents) { - split & process & initilize favorites } } void favorites_add(FavoriteItems *favorites, char *newFavorite) { - favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); - favorites->items[favorites->count-1]=newFavorite; + if(favorites->count) { + favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); + favorites->items[favorites->count-1]=strdup(newFavorite); + favorites_choose(favorites, newFavorite); + } else { + favorites->items=malloc(sizeof(char*)); + favorites->items[0]=strdup(newFavorite); + favorites->count=1; + } - favorites_choose(favorites, newFavorite); favorites_save(favorites); } @@ -96,15 +114,16 @@ void favorites_choose(FavoriteItems *favorites, char *choice) void favorites_remove(FavoriteItems *favorites, char *almostDead) { - // TODO: keept slot you have, just change count + // TODO: keep slot you have, just change count favorites_save(favorites); } void favorites_save(FavoriteItems *favorites) { - create/update history file - + if(favorites->count) { + // TODO shrink file and rewrite it (can be shorter) + } } void favorites_destroy(FavoriteItems *favorites) diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index 45d4e91..5a91a86 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -15,19 +15,19 @@ #define ENV_VAR_USER "USER" #define ENV_VAR_HOME "HOME" -#define FILE_HH_RC ".hhrc" +#define FILE_HH_RC ".hh_favorites" typedef struct { char **items; unsigned count; + bool loaded; } FavoriteItems; void favorites_init(FavoriteItems *favorites); -void favorites_load(FavoriteItems *favorites); +void favorites_get(FavoriteItems *favorites); void favorites_add(FavoriteItems *favorites, char *favorite); void favorites_choose(FavoriteItems *favorites, char *choice); void favorites_remove(FavoriteItems *favorites, char *almostDead); -void favorites_save(FavoriteItems *favorites); void favorites_destroy(FavoriteItems *favorites); #endif From a9e5750b3018250e4a48335e4150dee7b2551ece Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sat, 29 Mar 2014 17:11:19 +0100 Subject: [PATCH 07/13] Adding favorites saving. --- src/hstr_favorites.c | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index ad30018..afe316a 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -22,15 +22,20 @@ void favorites_init(FavoriteItems *favorites) favorites->loaded=false; } +char* favorites_get_filename() +{ + char *home = getenv(ENV_VAR_HOME); + char *fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_RC) + 1); + strcpy(fileName, home); + strcat(fileName, "/"); + strcat(fileName, FILE_HH_RC); + return fileName; +} + void favorites_get(FavoriteItems *favorites) { if(!favorites->loaded) { - char *home = getenv(ENV_VAR_HOME); - char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1); - strcpy(fileName,home); - strcat(fileName,"/"); - strcat(fileName,FILE_HH_RC); - + char* fileName = favorites_get_filename(); char *file_contents=NULL; if(access(fileName, F_OK) != -1) { long input_file_size; @@ -73,6 +78,7 @@ void favorites_get(FavoriteItems *favorites) favorites->loaded=true; return; } + free(fileName); } } @@ -114,16 +120,36 @@ void favorites_choose(FavoriteItems *favorites, char *choice) void favorites_remove(FavoriteItems *favorites, char *almostDead) { - // TODO: keep slot you have, just change count + // TODO: keep slot you have, just change count > ? by pointer or strstr? favorites_save(favorites); } void favorites_save(FavoriteItems *favorites) { + char *fileName=favorites_get_filename(); + if(favorites->count) { - // TODO shrink file and rewrite it (can be shorter) + FILE *output_file = fopen(fileName, "wb"); + rewind(output_file); + int i; + for(i=0; icount; i++) { + if(fwrite(favorites->items[i], sizeof(char), strlen(favorites->items[i]), output_file)==-1) { + exit(EXIT_FAILURE); + } + if(fwrite("\n", sizeof(char), strlen("\n"), output_file)==-1) { + exit(EXIT_FAILURE); + } + } + fclose(output_file); + } else { + if(access(fileName, F_OK) != -1) { + FILE *output_file = fopen(fileName, "wb"); + fclose(output_file); + } } + + free(fileName); } void favorites_destroy(FavoriteItems *favorites) From 5b35bbf2c044bfdb0a0755dbc2497a354fa0af5d Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Thu, 3 Apr 2014 09:36:57 +0100 Subject: [PATCH 08/13] Favorites work E2E (more testing needed) + added env variable driven warnings and debugs --- README.md | 8 ++++++++ man/hh.1 | 6 ++++++ src/hstr.c | 25 ++++++++++++++++++++++++- src/hstr_favorites.c | 14 ++++++++++++-- src/hstr_history.c | 7 ++++--- src/include/hstr_favorites.h | 2 +- src/include/hstr_history.h | 4 ++-- src/include/radixsort.h | 8 +++++++- src/radixsort.c | 21 +++++++++++++-------- 9 files changed, 77 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 7a46876..4e0b94d 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,14 @@ make search case sensitive (insensitive by default): ```bash export HH_CONFIG=casesensitive ``` +show warnings: +```bash +export HH_CONFIG=warning +``` +show debug messages: +```bash +export HH_CONFIG=warning +``` more colors and case sensitive search: ```bash export HH_CONFIG=hicolor,casesensitive diff --git a/man/hh.1 b/man/hh.1 index 42e97be..af6c212 100644 --- a/man/hh.1 +++ b/man/hh.1 @@ -70,6 +70,12 @@ Configuration options: \fIrawhistory\fR Show normal history by default (default is metric-based). +\fIwarning\fR + Show warning. + +\fIdebug\fR + Show debug information. + Example: \fBexport HH_CONFIG=casesensitive,hicolor\fR diff --git a/src/hstr.c b/src/hstr.c index 374c4d5..617c096 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -65,6 +65,12 @@ #define HH_CONFIG_HICOLOR "hicolor" #define HH_CONFIG_CASE "casesensitive" #define HH_CONFIG_SORTING "rawhistory" +#define HH_CONFIG_DEBUG "debug" +#define HH_CONFIG_WARN "warning" + +#define HH_DEBUG_LEVEL_NONE 0 +#define HH_DEBUG_LEVEL_WARN 1 +#define HH_DEBUG_LEVEL_DEBUG 2 #define HH_VIEW_RANKING 0 #define HH_VIEW_HISTORY 1 @@ -120,6 +126,7 @@ static unsigned selectionSize=0; static bool caseSensitive=FALSE; static int historyView=HH_VIEW_RANKING; static bool hicolor=FALSE; +static int debugLevel=0; static char screenLine[CMDLINE_LNG]; static char cmdline[CMDLINE_LNG]; @@ -136,6 +143,13 @@ void get_env_configuration() if(strstr(hhconfig,HH_CONFIG_SORTING)) { historyView=TRUE; } + if(strstr(hhconfig,HH_CONFIG_DEBUG)) { + debugLevel=HH_DEBUG_LEVEL_DEBUG; + } else { + if(strstr(hhconfig,HH_CONFIG_WARN)) { + debugLevel=HH_DEBUG_LEVEL_WARN; + } + } } } @@ -416,6 +430,15 @@ void signal_callback_handler_ctrl_c(int signum) } } +int seletion_source_remove(char* delete, HistoryItems *history) +{ + if(historyView!=HH_VIEW_FAVORITES) { + return history_mgmt_remove(delete); + } else { + return favorites_remove(history->favorites, delete); + } +} + void loop_to_select(HistoryItems *history) { signal(SIGINT, signal_callback_handler_ctrl_c); @@ -478,7 +501,7 @@ void loop_to_select(HistoryItems *history) msg=malloc(strlen(delete)+1); strcpy(msg,delete); selection_remove(delete, history); - deleteOccurences=history_mgmt_remove(delete); + deleteOccurences=seletion_source_remove(delete, history); result=print_selection(maxHistoryItems, pattern, history); print_cmd_deleted_label(msg, deleteOccurences); move(y, basex+strlen(pattern)); diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index afe316a..f5bc85b 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -118,11 +118,21 @@ void favorites_choose(FavoriteItems *favorites, char *choice) favorites_save(favorites); } -void favorites_remove(FavoriteItems *favorites, char *almostDead) +int favorites_remove(FavoriteItems *favorites, char *almostDead) { - // TODO: keep slot you have, just change count > ? by pointer or strstr? + int i, j=0; + for(i=0; icount && jcount; i++) { + if(j) { + favorites->items[i]=favorites->items[j++]; + } else { + if(favorites->items[i] == almostDead) { + j=i+1; + } + } + } favorites_save(favorites); + return 1; // true or false } void favorites_save(FavoriteItems *favorites) diff --git a/src/hstr_history.c b/src/hstr_history.c index 536292f..b199610 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -47,8 +47,8 @@ char *get_history_file_name() char *historyFile=getenv(ENV_VAR_HISTFILE); if(!historyFile || strlen(historyFile)==0) { char *home = getenv(ENV_VAR_HOME); - historyFile = malloc(strlen(home) + 1 + strlen(DEFAULT_HISTORY_FILE) + 1); - strcat(strcat(strcpy(historyFile, home), "/"), DEFAULT_HISTORY_FILE); + historyFile = malloc(strlen(home) + 1 + strlen(FILE_DEFAULT_HISTORY) + 1); + strcat(strcat(strcpy(historyFile, home), "/"), FILE_DEFAULT_HISTORY); } return historyFile; } @@ -153,7 +153,8 @@ HistoryItems *get_prioritized_history() FavoriteItems *favoriteItems=malloc(sizeof(FavoriteItems)); favorites_init(favoriteItems); - favorites_load(favoriteItems); + // TODO make favorites loading lazy > github issue + favorites_get(favoriteItems); prioritizedHistory->favorites=favoriteItems; return prioritizedHistory; diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index 5a91a86..8b70b62 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -27,7 +27,7 @@ void favorites_init(FavoriteItems *favorites); void favorites_get(FavoriteItems *favorites); void favorites_add(FavoriteItems *favorites, char *favorite); void favorites_choose(FavoriteItems *favorites, char *choice); -void favorites_remove(FavoriteItems *favorites, char *almostDead); +int favorites_remove(FavoriteItems *favorites, char *almostDead); void favorites_destroy(FavoriteItems *favorites); #endif diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index 9f78b8b..ab3737c 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -28,10 +28,10 @@ typedef struct { char **items; - char **raw; - FavoriteItems *favorites; unsigned count; + char **raw; unsigned rawCount; + FavoriteItems *favorites; } HistoryItems; HistoryItems *get_prioritized_history(); diff --git a/src/include/radixsort.h b/src/include/radixsort.h index 5b2b607..30ed4f1 100644 --- a/src/include/radixsort.h +++ b/src/include/radixsort.h @@ -17,7 +17,11 @@ #include #include "hstr_utils.h" -#define SLOT_SIZE 1000 +#define RADIX_SLOT_SIZE 1000 + +#define RADIX_DEBUG_LEVEL_NONE 0 +#define RADIX_DEBUG_LEVEL_WARN 1 +#define RADIX_DEBUG_LEVEL_DEBUG 2 typedef struct radixitem { unsigned key; @@ -43,9 +47,11 @@ typedef struct { RadixSlot **_slotDescriptors; unsigned _slotsCount; unsigned _topIndexLimit; + unsigned _debug; } RadixSorter; void radixsort_init(RadixSorter *rs, unsigned keyLimit); +void radixsort_set_debug_level(RadixSorter *rs, unsigned debugLevel); void radixsort_add(RadixSorter *rs, RadixItem *item); RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data); RadixItem **radixsort_dump(RadixSorter *rs); diff --git a/src/radixsort.c b/src/radixsort.c index b8a01dc..b4e5c71 100644 --- a/src/radixsort.c +++ b/src/radixsort.c @@ -9,10 +9,8 @@ #include "include/radixsort.h" -#define GET_TOP_INDEX(KEY) KEY/SLOT_SIZE -#define GET_LOW_INDEX(KEY) KEY%SLOT_SIZE - -#define RADIX_DEBUG 1 +#define GET_TOP_INDEX(KEY) KEY/RADIX_SLOT_SIZE +#define GET_LOW_INDEX(KEY) KEY%RADIX_SLOT_SIZE void radixsort_init(RadixSorter *rs, unsigned keyLimit) { @@ -30,10 +28,15 @@ void radixsort_init(RadixSorter *rs, unsigned keyLimit) rs->_slotsCount=0; } +void radixsort_set_debug_level(RadixSorter *rs, unsigned debugLevel) +{ + rs->_debug=debugLevel; +} + RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex) { - RadixItem **slot=malloc(SLOT_SIZE * sizeof(RadixItem *)); - memset(slot, 0, SLOT_SIZE * sizeof(RadixItem *)); + RadixItem **slot=malloc(RADIX_SLOT_SIZE * sizeof(RadixItem *)); + memset(slot, 0, RADIX_SLOT_SIZE * sizeof(RadixItem *)); RadixSlot *descriptor=malloc(sizeof(RadixSlot)); descriptor->min=rs->keyLimit; @@ -48,7 +51,9 @@ RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex) void radixsort_add(RadixSorter *rs, RadixItem *item) { if(item->key > rs->keyLimit) { - if(RADIX_DEBUG) fprintf(stderr, "ERROR: Radix sort overflow - inserted key is bigger than limit (%u): %u\n", rs->keyLimit, item->key); + if(rs->_debug > RADIX_DEBUG_LEVEL_NONE) { + fprintf(stderr, "WARNING: Radix sort overflow - inserted key is bigger than limit (%u): %u\n", rs->keyLimit, item->key); + } if(rs->optFloorAndInsertBigKeys) { item->key = rs->keyLimit-1; } else { @@ -174,7 +179,7 @@ void radixsort_stat(RadixSorter *rs, bool listing) printf("\n Radixsort (size/max/limit/slot count): %u %u %u %u", rs->size, rs->maxKey, rs->keyLimit, rs->_slotsCount); unsigned memory=rs->_topIndexLimit * sizeof(RadixItem ***); memory+=memory; - memory+=rs->_slotsCount*(SLOT_SIZE * sizeof(RadixItem *)); + memory+=rs->_slotsCount*(RADIX_SLOT_SIZE * sizeof(RadixItem *)); printf("\n Memory: %u\n", memory); if(listing && rs->size>0) { int t = GET_TOP_INDEX(rs->maxKey); From c9d9cff9d9d8176b7c9abd43a3679b92b5b9a03e Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Fri, 4 Apr 2014 21:54:08 +0100 Subject: [PATCH 09/13] Sync commit (source not compilable). --- src/hstr.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/hstr.c b/src/hstr.c index 617c096..946df91 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -403,15 +403,32 @@ void highlight_selection(int selectionCursorPosition, int previousSelectionCurso void selection_remove(char *cmd, HistoryItems *history) { - if(history->count) { - int i, w; - for(i=0, w=0; icount; i++) { - if(strcmp(history->items[i], cmd)) { - history->items[w]=history->items[i]; - w++; + if(historyView==HH_VIEW_FAVORITES) { + if(history->favorites->count) { + + selection must be remade & shown OR move the favorites remove code to here + + int i, w; + for(i=0, w=0; icount; i++) { + if(strcmp(history->items[i], cmd)) { + history->items[w]=history->items[i]; + w++; + } } + history->count=w; + + } + } else { + if(history->count) { + int i, w; + for(i=0, w=0; icount; i++) { + if(strcmp(history->items[i], cmd)) { + history->items[w]=history->items[i]; + w++; + } + } + history->count=w; } - history->count=w; } } From 5c89871692e0787ebe89305ffad71465ed4a678e Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sat, 12 Apr 2014 11:58:56 +0200 Subject: [PATCH 10/13] Fixing removal from favorites; label message showing successful add. --- src/hstr.c | 45 +++++++++++---------- src/hstr_favorites.c | 76 +++++++++++++++++++----------------- src/include/hstr_favorites.h | 2 +- 3 files changed, 66 insertions(+), 57 deletions(-) diff --git a/src/hstr.c b/src/hstr.c index 946df91..aacea3a 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -119,7 +119,7 @@ static const char *HELP_STRING= "\n"; static const char *LABEL_HELP= - "Type to filter, UP/DOWN to move, DEL to remove, TAB to select, C-g to cancel"; + "Type to filter, UP/DOWN move, DEL remove, TAB select, C-f add favorite, C-g cancel"; static char **selection=NULL; static unsigned selectionSize=0; @@ -200,10 +200,26 @@ void print_cmd_deleted_label(char *cmd, int occurences) refresh(); } +void print_cmd_added_favorite_label(char *cmd) +{ + snprintf(screenLine, getmaxx(stdscr), "Command '%s' added to favorites (C-/ to show favorites)", cmd); + if(hicolor) { + color_attr_on(COLOR_PAIR(4)); + color_attr_on(A_BOLD); + } + mvprintw(Y_OFFSET_HELP, 0, screenLine); + if(hicolor) { + color_attr_off(A_BOLD); + color_attr_on(COLOR_PAIR(1)); + } + clrtoeol(); + refresh(); +} + void print_history_label(HistoryItems *history) { int width=getmaxx(stdscr); - snprintf(screenLine, width, "- HISTORY - case:%s (C-t) - view:%s (C-/) - %d/%d ", + snprintf(screenLine, width, "- HISTORY - match:%s (C-t) - view:%s (C-/) - %d/%d ", (caseSensitive?"sensitive":"insensitive"), HH_VIEW_LABELS[historyView], history->count, @@ -404,20 +420,7 @@ void highlight_selection(int selectionCursorPosition, int previousSelectionCurso void selection_remove(char *cmd, HistoryItems *history) { if(historyView==HH_VIEW_FAVORITES) { - if(history->favorites->count) { - - selection must be remade & shown OR move the favorites remove code to here - - int i, w; - for(i=0, w=0; icount; i++) { - if(strcmp(history->items[i], cmd)) { - history->items[w]=history->items[i]; - w++; - } - } - history->count=w; - - } + favorites_remove(history->favorites, cmd); } else { if(history->count) { int i, w; @@ -477,7 +480,7 @@ void loop_to_select(HistoryItems *history) print_selection(get_max_history_items(stdscr), NULL, history); color_attr_off(COLOR_PAIR(HH_COLOR_NORMAL)); - bool done=FALSE, skip=TRUE, executeResult=FALSE, lowercase=TRUE, justDeleted=FALSE; + bool done=FALSE, skip=TRUE, executeResult=FALSE, lowercase=TRUE, printDefaultLabel=FALSE; int basex=print_prompt(stdscr); int x=basex, y=0, c, cursorX=0, cursorY=0, maxHistoryItems, deleteOccurences; int width=getmaxx(stdscr); @@ -506,9 +509,9 @@ void loop_to_select(HistoryItems *history) continue; } - if(justDeleted) { + if(printDefaultLabel) { print_help_label(); - justDeleted=FALSE; + printDefaultLabel=FALSE; } switch (c) { @@ -522,7 +525,7 @@ void loop_to_select(HistoryItems *history) result=print_selection(maxHistoryItems, pattern, history); print_cmd_deleted_label(msg, deleteOccurences); move(y, basex+strlen(pattern)); - justDeleted=TRUE; + printDefaultLabel=TRUE; } print_history_label(history); break; @@ -546,6 +549,8 @@ void loop_to_select(HistoryItems *history) favorites_choose(history->favorites, result); } else { favorites_add(history->favorites, result); + print_cmd_added_favorite_label(result); + printDefaultLabel=TRUE; } result=print_selection(maxHistoryItems, pattern, history); selectionCursorPosition=0; diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index f5bc85b..bdfd272 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -82,6 +82,33 @@ void favorites_get(FavoriteItems *favorites) } } +void favorites_save(FavoriteItems *favorites) +{ + char *fileName=favorites_get_filename(); + + if(favorites->count) { + FILE *output_file = fopen(fileName, "wb"); + rewind(output_file); + int i; + for(i=0; icount; i++) { + if(fwrite(favorites->items[i], sizeof(char), strlen(favorites->items[i]), output_file)==-1) { + exit(EXIT_FAILURE); + } + if(fwrite("\n", sizeof(char), strlen("\n"), output_file)==-1) { + exit(EXIT_FAILURE); + } + } + fclose(output_file); + } else { + if(access(fileName, F_OK) != -1) { + FILE *output_file = fopen(fileName, "wb"); + fclose(output_file); + } + } + + free(fileName); +} + void favorites_add(FavoriteItems *favorites, char *newFavorite) { if(favorites->count) { @@ -118,48 +145,25 @@ void favorites_choose(FavoriteItems *favorites, char *choice) favorites_save(favorites); } -int favorites_remove(FavoriteItems *favorites, char *almostDead) +bool favorites_remove(FavoriteItems *favorites, char *almostDead) { - int i, j=0; - for(i=0; icount && jcount; i++) { - if(j) { - favorites->items[i]=favorites->items[j++]; - } else { - if(favorites->items[i] == almostDead) { - j=i+1; - } - } - } - - favorites_save(favorites); - return 1; // true or false -} - -void favorites_save(FavoriteItems *favorites) -{ - char *fileName=favorites_get_filename(); - if(favorites->count) { - FILE *output_file = fopen(fileName, "wb"); - rewind(output_file); - int i; - for(i=0; icount; i++) { - if(fwrite(favorites->items[i], sizeof(char), strlen(favorites->items[i]), output_file)==-1) { - exit(EXIT_FAILURE); - } - if(fwrite("\n", sizeof(char), strlen("\n"), output_file)==-1) { - exit(EXIT_FAILURE); + int i, j; + for(i=0, j=0; icount && jcount; i++, j++) { + if(!strcmp(favorites->items[i], almostDead)) { + j=i+1; + favorites->count--; + } else { + if(j>i) { + favorites->items[i]=favorites->items[j]; + } } } - fclose(output_file); + favorites_save(favorites); + return true; } else { - if(access(fileName, F_OK) != -1) { - FILE *output_file = fopen(fileName, "wb"); - fclose(output_file); - } + return false; } - - free(fileName); } void favorites_destroy(FavoriteItems *favorites) diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index 8b70b62..9140d88 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -27,7 +27,7 @@ void favorites_init(FavoriteItems *favorites); void favorites_get(FavoriteItems *favorites); void favorites_add(FavoriteItems *favorites, char *favorite); void favorites_choose(FavoriteItems *favorites, char *choice); -int favorites_remove(FavoriteItems *favorites, char *almostDead); +bool favorites_remove(FavoriteItems *favorites, char *almostDead); void favorites_destroy(FavoriteItems *favorites); #endif From 2446ad85091bd86e3f9820cc947c09c3b4452262 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sat, 12 Apr 2014 12:21:36 +0200 Subject: [PATCH 11/13] Added configuration parameter allowing to start hh in favorites view. --- src/hstr.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/hstr.c b/src/hstr.c index aacea3a..a28bdbe 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -65,6 +65,7 @@ #define HH_CONFIG_HICOLOR "hicolor" #define HH_CONFIG_CASE "casesensitive" #define HH_CONFIG_SORTING "rawhistory" +#define HH_CONFIG_FAVORITES "favorites" #define HH_CONFIG_DEBUG "debug" #define HH_CONFIG_WARN "warning" @@ -141,7 +142,11 @@ void get_env_configuration() caseSensitive=TRUE; } if(strstr(hhconfig,HH_CONFIG_SORTING)) { - historyView=TRUE; + historyView=HH_VIEW_HISTORY; + } else { + if(strstr(hhconfig,HH_CONFIG_FAVORITES)) { + historyView=HH_VIEW_FAVORITES; + } } if(strstr(hhconfig,HH_CONFIG_DEBUG)) { debugLevel=HH_DEBUG_LEVEL_DEBUG; From a237568a1000dff45a21809c6b86dd373153a3a9 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sun, 13 Apr 2014 09:37:40 +0200 Subject: [PATCH 12/13] Man --- man/hh.1 | 25 ++++++++++++++++++------- src/hstr.c | 4 ++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/man/hh.1 b/man/hh.1 index af6c212..85a356b 100644 --- a/man/hh.1 +++ b/man/hh.1 @@ -9,16 +9,21 @@ uses shell history to provide suggest box like functionality for commands used in the past. By default it parses .bash-history file that is filtered as you type a command substring. Commands are not just filtered, but also ordered by a ranking algorithm -that considers number of occurences, length and timestamp. In addition -hh allows removal of commands from history - for instance with a typo or with a sensitive content. +that considers number of occurences, length and timestamp. +Favorite and frequently used commands can be bookmarked. In addition +hh allows removal of commands from history - for instance with a typo +or with a sensitive content. .SH OPTIONS .TP +\fB--favorites\fR +Show favorites view immediately +.TP \fB--show-configuration\fR -Show configuration that can be added to .bashrc +Show configuration that can be added to ~/.bashrc .TP \fB--help\fR Show help -.SH COMMANDS +.SH KEYS .TP \fBpattern\fR Type to filter shell history. @@ -27,7 +32,7 @@ Type to filter shell history. Toggle case sensitive search. .TP \fBCtrl\-/\fR -Toggle history as provided by shell vs. ranked history ordered by the number of occurences, length and timestamp. +Rotate view of history as provided by BASH, ranked history ordered by the number of occurences/length/timestamp and favorites. .TP \fBCtrl\-l\fR Make search pattern lowercase or uppercase. @@ -65,10 +70,13 @@ Configuration options: Get more colors with this option (default is monochromatic). \fIcasesensitive\fR - Make the pattern-based filtering case sensitive by default. + Make the pattern-based filtering case sensitive (it's case insensitive by default). \fIrawhistory\fR - Show normal history by default (default is metric-based). + Show normal history as a default view (metric-based view is shown otherwise). + +\fIfavorites\fR + Show favorites as a default view (metric-based view is shown otherwise). \fIwarning\fR Show warning. @@ -79,6 +87,9 @@ Configuration options: Example: \fBexport HH_CONFIG=casesensitive,hicolor\fR +.SH FILES +\fB~/.hh_favorites\fR bookmarked favorite commands + .SH CONFIGURATION Optionally add the following lines to ~/.bashrc: .nf diff --git a/src/hstr.c b/src/hstr.c index a28bdbe..21f2706 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -112,6 +112,7 @@ static const char *HELP_STRING= "Usage: hh [option] [arg1] [arg2]..." "\nShell history suggest box (build: "__DATE__" " __TIME__")" "\n" + "\n --favorites ... show command favorites" "\n --show-configuration ... show configuration to be added to .bashrc" "\n --help ... display this help and exit" "\n" @@ -713,6 +714,9 @@ int main(int argc, char *argv[]) { if(argc>0) { if(argc==2) { + if(strstr(argv[1], "--favorites") || strstr(argv[1], "-f")) { + xxx + } if(strstr(argv[1], "--show-configuration")) { printf("%s", INSTALL_STRING); return EXIT_SUCCESS; From 41dabb75bdb222808bbf313ab1b0bf86bee9777c Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sun, 13 Apr 2014 09:47:04 +0200 Subject: [PATCH 13/13] Favorites view enforcement from the command line. --- src/hstr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hstr.c b/src/hstr.c index 21f2706..0fe8f37 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -702,7 +702,6 @@ void hstr() HistoryItems *history=get_prioritized_history(); if(history) { history_mgmt_open(); - get_env_configuration(); loop_to_select(history); hstr_on_exit(); } else { @@ -712,10 +711,11 @@ void hstr() int main(int argc, char *argv[]) { + get_env_configuration(); if(argc>0) { if(argc==2) { if(strstr(argv[1], "--favorites") || strstr(argv[1], "-f")) { - xxx + historyView=HH_VIEW_FAVORITES; } if(strstr(argv[1], "--show-configuration")) { printf("%s", INSTALL_STRING);