xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
Commit 5b35bbf2c044bfdb0a0755dbc2497a354fa0af5d

Favorites work E2E (more testing needed) + added env variable driven warnings and debugs
Author: Martin Dvorak
Author date (UTC): 2014-04-03 08:36
Committer name: Martin Dvorak
Committer date (UTC): 2014-04-03 08:36
Parent(s): a9e5750b3018250e4a48335e4150dee7b2551ece
Signing key:
Tree: 31f4791c30bf83049634f590081b1be790068b04
File Lines added Lines deleted
README.md 8 0
man/hh.1 6 0
src/hstr.c 24 1
src/hstr_favorites.c 12 2
src/hstr_history.c 4 3
src/include/hstr_favorites.h 1 1
src/include/hstr_history.h 2 2
src/include/radixsort.h 7 1
src/radixsort.c 13 8
File README.md changed (mode: 100644) (index 7a46876..4e0b94d)
... ... make search case sensitive (insensitive by default):
151 151 ```bash ```bash
152 152 export HH_CONFIG=casesensitive export HH_CONFIG=casesensitive
153 153 ``` ```
154 show warnings:
155 ```bash
156 export HH_CONFIG=warning
157 ```
158 show debug messages:
159 ```bash
160 export HH_CONFIG=warning
161 ```
154 162 more colors and case sensitive search: more colors and case sensitive search:
155 163 ```bash ```bash
156 164 export HH_CONFIG=hicolor,casesensitive export HH_CONFIG=hicolor,casesensitive
File man/hh.1 changed (mode: 100644) (index 42e97be..af6c212)
... ... Configuration options:
70 70 \fIrawhistory\fR \fIrawhistory\fR
71 71 Show normal history by default (default is metric-based). Show normal history by default (default is metric-based).
72 72
73 \fIwarning\fR
74 Show warning.
75
76 \fIdebug\fR
77 Show debug information.
78
73 79 Example: Example:
74 80 \fBexport HH_CONFIG=casesensitive,hicolor\fR \fBexport HH_CONFIG=casesensitive,hicolor\fR
75 81
File src/hstr.c changed (mode: 100644) (index 374c4d5..617c096)
65 65 #define HH_CONFIG_HICOLOR "hicolor" #define HH_CONFIG_HICOLOR "hicolor"
66 66 #define HH_CONFIG_CASE "casesensitive" #define HH_CONFIG_CASE "casesensitive"
67 67 #define HH_CONFIG_SORTING "rawhistory" #define HH_CONFIG_SORTING "rawhistory"
68 #define HH_CONFIG_DEBUG "debug"
69 #define HH_CONFIG_WARN "warning"
70
71 #define HH_DEBUG_LEVEL_NONE 0
72 #define HH_DEBUG_LEVEL_WARN 1
73 #define HH_DEBUG_LEVEL_DEBUG 2
68 74
69 75 #define HH_VIEW_RANKING 0 #define HH_VIEW_RANKING 0
70 76 #define HH_VIEW_HISTORY 1 #define HH_VIEW_HISTORY 1
 
... ... static unsigned selectionSize=0;
120 126 static bool caseSensitive=FALSE; static bool caseSensitive=FALSE;
121 127 static int historyView=HH_VIEW_RANKING; static int historyView=HH_VIEW_RANKING;
122 128 static bool hicolor=FALSE; static bool hicolor=FALSE;
129 static int debugLevel=0;
123 130 static char screenLine[CMDLINE_LNG]; static char screenLine[CMDLINE_LNG];
124 131 static char cmdline[CMDLINE_LNG]; static char cmdline[CMDLINE_LNG];
125 132
 
... ... void get_env_configuration()
136 143 if(strstr(hhconfig,HH_CONFIG_SORTING)) { if(strstr(hhconfig,HH_CONFIG_SORTING)) {
137 144 historyView=TRUE; historyView=TRUE;
138 145 } }
146 if(strstr(hhconfig,HH_CONFIG_DEBUG)) {
147 debugLevel=HH_DEBUG_LEVEL_DEBUG;
148 } else {
149 if(strstr(hhconfig,HH_CONFIG_WARN)) {
150 debugLevel=HH_DEBUG_LEVEL_WARN;
151 }
152 }
139 153 } }
140 154 } }
141 155
 
... ... void signal_callback_handler_ctrl_c(int signum)
416 430 } }
417 431 } }
418 432
433 int seletion_source_remove(char* delete, HistoryItems *history)
434 {
435 if(historyView!=HH_VIEW_FAVORITES) {
436 return history_mgmt_remove(delete);
437 } else {
438 return favorites_remove(history->favorites, delete);
439 }
440 }
441
419 442 void loop_to_select(HistoryItems *history) void loop_to_select(HistoryItems *history)
420 443 { {
421 444 signal(SIGINT, signal_callback_handler_ctrl_c); signal(SIGINT, signal_callback_handler_ctrl_c);
 
... ... void loop_to_select(HistoryItems *history)
478 501 msg=malloc(strlen(delete)+1); msg=malloc(strlen(delete)+1);
479 502 strcpy(msg,delete); strcpy(msg,delete);
480 503 selection_remove(delete, history); selection_remove(delete, history);
481 deleteOccurences=history_mgmt_remove(delete);
504 deleteOccurences=seletion_source_remove(delete, history);
482 505 result=print_selection(maxHistoryItems, pattern, history); result=print_selection(maxHistoryItems, pattern, history);
483 506 print_cmd_deleted_label(msg, deleteOccurences); print_cmd_deleted_label(msg, deleteOccurences);
484 507 move(y, basex+strlen(pattern)); move(y, basex+strlen(pattern));
File src/hstr_favorites.c changed (mode: 100644) (index afe316a..f5bc85b)
... ... void favorites_choose(FavoriteItems *favorites, char *choice)
118 118 favorites_save(favorites); favorites_save(favorites);
119 119 } }
120 120
121 void favorites_remove(FavoriteItems *favorites, char *almostDead)
121 int favorites_remove(FavoriteItems *favorites, char *almostDead)
122 122 { {
123 // TODO: keep slot you have, just change count > ? by pointer or strstr?
123 int i, j=0;
124 for(i=0; i<favorites->count && j<favorites->count; i++) {
125 if(j) {
126 favorites->items[i]=favorites->items[j++];
127 } else {
128 if(favorites->items[i] == almostDead) {
129 j=i+1;
130 }
131 }
132 }
124 133
125 134 favorites_save(favorites); favorites_save(favorites);
135 return 1; // true or false
126 136 } }
127 137
128 138 void favorites_save(FavoriteItems *favorites) void favorites_save(FavoriteItems *favorites)
File src/hstr_history.c changed (mode: 100644) (index 536292f..b199610)
... ... char *get_history_file_name()
47 47 char *historyFile=getenv(ENV_VAR_HISTFILE); char *historyFile=getenv(ENV_VAR_HISTFILE);
48 48 if(!historyFile || strlen(historyFile)==0) { if(!historyFile || strlen(historyFile)==0) {
49 49 char *home = getenv(ENV_VAR_HOME); char *home = getenv(ENV_VAR_HOME);
50 historyFile = malloc(strlen(home) + 1 + strlen(DEFAULT_HISTORY_FILE) + 1);
51 strcat(strcat(strcpy(historyFile, home), "/"), DEFAULT_HISTORY_FILE);
50 historyFile = malloc(strlen(home) + 1 + strlen(FILE_DEFAULT_HISTORY) + 1);
51 strcat(strcat(strcpy(historyFile, home), "/"), FILE_DEFAULT_HISTORY);
52 52 } }
53 53 return historyFile; return historyFile;
54 54 } }
 
... ... HistoryItems *get_prioritized_history()
153 153
154 154 FavoriteItems *favoriteItems=malloc(sizeof(FavoriteItems)); FavoriteItems *favoriteItems=malloc(sizeof(FavoriteItems));
155 155 favorites_init(favoriteItems); favorites_init(favoriteItems);
156 favorites_load(favoriteItems);
156 // TODO make favorites loading lazy > github issue
157 favorites_get(favoriteItems);
157 158 prioritizedHistory->favorites=favoriteItems; prioritizedHistory->favorites=favoriteItems;
158 159
159 160 return prioritizedHistory; return prioritizedHistory;
File src/include/hstr_favorites.h changed (mode: 100644) (index 5a91a86..8b70b62)
... ... void favorites_init(FavoriteItems *favorites);
27 27 void favorites_get(FavoriteItems *favorites); void favorites_get(FavoriteItems *favorites);
28 28 void favorites_add(FavoriteItems *favorites, char *favorite); void favorites_add(FavoriteItems *favorites, char *favorite);
29 29 void favorites_choose(FavoriteItems *favorites, char *choice); void favorites_choose(FavoriteItems *favorites, char *choice);
30 void favorites_remove(FavoriteItems *favorites, char *almostDead);
30 int favorites_remove(FavoriteItems *favorites, char *almostDead);
31 31 void favorites_destroy(FavoriteItems *favorites); void favorites_destroy(FavoriteItems *favorites);
32 32
33 33 #endif #endif
File src/include/hstr_history.h changed (mode: 100644) (index 9f78b8b..ab3737c)
28 28
29 29 typedef struct { typedef struct {
30 30 char **items; char **items;
31 char **raw;
32 FavoriteItems *favorites;
33 31 unsigned count; unsigned count;
32 char **raw;
34 33 unsigned rawCount; unsigned rawCount;
34 FavoriteItems *favorites;
35 35 } HistoryItems; } HistoryItems;
36 36
37 37 HistoryItems *get_prioritized_history(); HistoryItems *get_prioritized_history();
File src/include/radixsort.h changed (mode: 100644) (index 5b2b607..30ed4f1)
17 17 #include <stddef.h> #include <stddef.h>
18 18 #include "hstr_utils.h" #include "hstr_utils.h"
19 19
20 #define SLOT_SIZE 1000
20 #define RADIX_SLOT_SIZE 1000
21
22 #define RADIX_DEBUG_LEVEL_NONE 0
23 #define RADIX_DEBUG_LEVEL_WARN 1
24 #define RADIX_DEBUG_LEVEL_DEBUG 2
21 25
22 26 typedef struct radixitem { typedef struct radixitem {
23 27 unsigned key; unsigned key;
 
... ... typedef struct {
43 47 RadixSlot **_slotDescriptors; RadixSlot **_slotDescriptors;
44 48 unsigned _slotsCount; unsigned _slotsCount;
45 49 unsigned _topIndexLimit; unsigned _topIndexLimit;
50 unsigned _debug;
46 51 } RadixSorter; } RadixSorter;
47 52
48 53 void radixsort_init(RadixSorter *rs, unsigned keyLimit); void radixsort_init(RadixSorter *rs, unsigned keyLimit);
54 void radixsort_set_debug_level(RadixSorter *rs, unsigned debugLevel);
49 55 void radixsort_add(RadixSorter *rs, RadixItem *item); void radixsort_add(RadixSorter *rs, RadixItem *item);
50 56 RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data); RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data);
51 57 RadixItem **radixsort_dump(RadixSorter *rs); RadixItem **radixsort_dump(RadixSorter *rs);
File src/radixsort.c changed (mode: 100644) (index b8a01dc..b4e5c71)
9 9
10 10 #include "include/radixsort.h" #include "include/radixsort.h"
11 11
12 #define GET_TOP_INDEX(KEY) KEY/SLOT_SIZE
13 #define GET_LOW_INDEX(KEY) KEY%SLOT_SIZE
14
15 #define RADIX_DEBUG 1
12 #define GET_TOP_INDEX(KEY) KEY/RADIX_SLOT_SIZE
13 #define GET_LOW_INDEX(KEY) KEY%RADIX_SLOT_SIZE
16 14
17 15 void radixsort_init(RadixSorter *rs, unsigned keyLimit) void radixsort_init(RadixSorter *rs, unsigned keyLimit)
18 16 { {
 
... ... void radixsort_init(RadixSorter *rs, unsigned keyLimit)
30 28 rs->_slotsCount=0; rs->_slotsCount=0;
31 29 } }
32 30
31 void radixsort_set_debug_level(RadixSorter *rs, unsigned debugLevel)
32 {
33 rs->_debug=debugLevel;
34 }
35
33 36 RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex) RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex)
34 37 { {
35 RadixItem **slot=malloc(SLOT_SIZE * sizeof(RadixItem *));
36 memset(slot, 0, SLOT_SIZE * sizeof(RadixItem *));
38 RadixItem **slot=malloc(RADIX_SLOT_SIZE * sizeof(RadixItem *));
39 memset(slot, 0, RADIX_SLOT_SIZE * sizeof(RadixItem *));
37 40
38 41 RadixSlot *descriptor=malloc(sizeof(RadixSlot)); RadixSlot *descriptor=malloc(sizeof(RadixSlot));
39 42 descriptor->min=rs->keyLimit; descriptor->min=rs->keyLimit;
 
... ... RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex)
48 51 void radixsort_add(RadixSorter *rs, RadixItem *item) void radixsort_add(RadixSorter *rs, RadixItem *item)
49 52 { {
50 53 if(item->key > rs->keyLimit) { if(item->key > rs->keyLimit) {
51 if(RADIX_DEBUG) fprintf(stderr, "ERROR: Radix sort overflow - inserted key is bigger than limit (%u): %u\n", rs->keyLimit, item->key);
54 if(rs->_debug > RADIX_DEBUG_LEVEL_NONE) {
55 fprintf(stderr, "WARNING: Radix sort overflow - inserted key is bigger than limit (%u): %u\n", rs->keyLimit, item->key);
56 }
52 57 if(rs->optFloorAndInsertBigKeys) { if(rs->optFloorAndInsertBigKeys) {
53 58 item->key = rs->keyLimit-1; item->key = rs->keyLimit-1;
54 59 } else { } else {
 
... ... void radixsort_stat(RadixSorter *rs, bool listing)
174 179 printf("\n Radixsort (size/max/limit/slot count): %u %u %u %u", rs->size, rs->maxKey, rs->keyLimit, rs->_slotsCount); printf("\n Radixsort (size/max/limit/slot count): %u %u %u %u", rs->size, rs->maxKey, rs->keyLimit, rs->_slotsCount);
175 180 unsigned memory=rs->_topIndexLimit * sizeof(RadixItem ***); unsigned memory=rs->_topIndexLimit * sizeof(RadixItem ***);
176 181 memory+=memory; memory+=memory;
177 memory+=rs->_slotsCount*(SLOT_SIZE * sizeof(RadixItem *));
182 memory+=rs->_slotsCount*(RADIX_SLOT_SIZE * sizeof(RadixItem *));
178 183 printf("\n Memory: %u\n", memory); printf("\n Memory: %u\n", memory);
179 184 if(listing && rs->size>0) { if(listing && rs->size>0) {
180 185 int t = GET_TOP_INDEX(rs->maxKey); int t = GET_TOP_INDEX(rs->maxKey);
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master