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 683289f15ce687817a1881d552127453ec585a1d

View rotation, fake in memory favorites, selection SIGSEGV.
Author: Martin Dvorak
Author date (UTC): 2014-03-23 02:37
Committer name: Martin Dvorak
Committer date (UTC): 2014-03-23 02:37
Parent(s): 4d3f46e26e8322c49f227c6b0ff30443da82a168
Signing key:
Tree: 0fdad0362f3c78102a27fcd0732cdcabb3875945
File Lines added Lines deleted
src/hstr.c 33 9
src/hstr_favorites.c 8 21
src/hstr_history.c 15 0
src/include/hstr_favorites.h 4 5
src/include/hstr_history.h 2 0
File src/hstr.c changed (mode: 100644) (index b0c3fbb..26e21e2)
60 60 #define HH_COLOR_PROMPT 3 #define HH_COLOR_PROMPT 3
61 61 #define HH_COLOR_DELETE 4 #define HH_COLOR_DELETE 4
62 62
63 #define ENV_VAR_HH_CONFIG "HH_CONFIG"
63 #define HH_ENV_VAR_CONFIG "HH_CONFIG"
64 64 #define HH_CONFIG_HICOLOR "hicolor" #define HH_CONFIG_HICOLOR "hicolor"
65 65 #define HH_CONFIG_CASE "casesensitive" #define HH_CONFIG_CASE "casesensitive"
66 66 #define HH_CONFIG_SORTING "rawhistory" #define HH_CONFIG_SORTING "rawhistory"
67 67
68 #define HH_VIEW_RANKING 0
69 #define HH_VIEW_HISTORY 1
70 #define HH_VIEW_FAVORITES 2
71
68 72 #define SPACE_PADDING " " #define SPACE_PADDING " "
69 73
70 74 #ifdef DEBUG_KEYS #ifdef DEBUG_KEYS
 
79 83 #define LOGCURSOR(Y) #define LOGCURSOR(Y)
80 84 #endif #endif
81 85
86 static const char *HH_VIEW_LABELS[]={
87 "ranking",
88 "history",
89 "favorites"};
90
82 91 static const char *INSTALL_STRING= static const char *INSTALL_STRING=
83 92 "\n# add this configuration to ~/.bashrc" "\n# add this configuration to ~/.bashrc"
84 93 "\nexport HH_CONFIG=hicolor # get more colors" "\nexport HH_CONFIG=hicolor # get more colors"
 
... ... static const char *LABEL_HELP=
108 117 static char **selection=NULL; static char **selection=NULL;
109 118 static unsigned selectionSize=0; static unsigned selectionSize=0;
110 119 static bool caseSensitive=FALSE; static bool caseSensitive=FALSE;
111 static bool defaultOrder=FALSE;
120 static int historyView=HH_VIEW_RANKING;
112 121 static bool hicolor=FALSE; static bool hicolor=FALSE;
113 122 static char screenLine[CMDLINE_LNG]; static char screenLine[CMDLINE_LNG];
114 123 static char cmdline[CMDLINE_LNG]; static char cmdline[CMDLINE_LNG];
115 124
116 125 void get_env_configuration() void get_env_configuration()
117 126 { {
118 char *hhconfig=getenv(ENV_VAR_HH_CONFIG);
127 char *hhconfig=getenv(HH_ENV_VAR_CONFIG);
119 128 if(hhconfig && strlen(hhconfig)>0) { if(hhconfig && strlen(hhconfig)>0) {
120 129 if(strstr(hhconfig,HH_CONFIG_HICOLOR)) { if(strstr(hhconfig,HH_CONFIG_HICOLOR)) {
121 130 hicolor=TRUE; hicolor=TRUE;
 
... ... void get_env_configuration()
124 133 caseSensitive=TRUE; caseSensitive=TRUE;
125 134 } }
126 135 if(strstr(hhconfig,HH_CONFIG_SORTING)) { if(strstr(hhconfig,HH_CONFIG_SORTING)) {
127 defaultOrder=TRUE;
136 historyView=TRUE;
128 137 } }
129 138 } }
130 139 } }
 
... ... void print_cmd_deleted_label(char *cmd, int occurences)
179 188 void print_history_label(HistoryItems *history) void print_history_label(HistoryItems *history)
180 189 { {
181 190 int width=getmaxx(stdscr); int width=getmaxx(stdscr);
182 snprintf(screenLine, width, "- HISTORY - case:%s (C-t) - order:%s (C-/) - %d/%d ",
191 snprintf(screenLine, width, "- HISTORY - case:%s (C-t) - view:%s (C-/) - %d/%d ",
183 192 (caseSensitive?"sensitive":"insensitive"), (caseSensitive?"sensitive":"insensitive"),
184 (defaultOrder?"history":"ranking"),
193 HH_VIEW_LABELS[historyView],
185 194 history->count, history->count,
186 195 history->rawCount); history->rawCount);
187 196 width -= strlen(screenLine); width -= strlen(screenLine);
 
... ... unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCou
236 245 realloc_selection(sizeof(char*) * maxSelectionCount); realloc_selection(sizeof(char*) * maxSelectionCount);
237 246
238 247 unsigned i, selectionCount=0; unsigned i, selectionCount=0;
239 char **source=(defaultOrder?history->raw:history->items);
240 unsigned count=(defaultOrder?history->rawCount:history->count);
248 char **source;
249 unsigned count;
250
251 switch(historyView) {
252 case HH_VIEW_RANKING:
253 source=history->items;
254 count=history->count;
255 break;
256 case HH_VIEW_HISTORY:
257 source=history->raw;
258 count=history->rawCount;
259 break;
260 case HH_VIEW_FAVORITES:
261 source=history->favorites;
262 count=history->favoritesCount;
263 break;
264 }
241 265
242 266 for(i=0; i<count && selectionCount<maxSelectionCount; i++) { for(i=0; i<count && selectionCount<maxSelectionCount; i++) {
243 267 if(source[i]) { if(source[i]) {
 
... ... void loop_to_select(HistoryItems *history)
468 492 selectionCursorPosition=0; selectionCursorPosition=0;
469 493 break; break;
470 494 case K_CTRL_SLASH: case K_CTRL_SLASH:
471 defaultOrder=!defaultOrder;
495 historyView=(++historyView)%3;
472 496 result=print_selection(maxHistoryItems, pattern, history); result=print_selection(maxHistoryItems, pattern, history);
473 497 print_history_label(history); print_history_label(history);
474 498 selectionCursorPosition=0; selectionCursorPosition=0;
File src/hstr_favorites.c changed (mode: 100644) (index ee38533..dc423af)
9 9
10 10 #include "include/hstr_favorites.h" #include "include/hstr_favorites.h"
11 11
12 FavoriteItems *favorites;
12 #define FAVORITE_SEGMENT_SIZE 10
13 13
14 FavoriteItems *favorites_init() {
15 favorites=malloc(sizeof(FavoriteItems));
16 return favorites;
14 void favorites_init(FavoriteItems *favorites) {
15 favorites->items=NULL;
16 favorites->count=0;
17 17 } }
18 18
19 FavoriteItems *favorites_load() {
20 // TODO fake initialization
19 void favorites_load(FavoriteItems *favorites) {
20 // TODO fake initialization instead of .hhrc load
21 21 favorites->count=3; favorites->count=3;
22 22 favorites->items=malloc(sizeof(char *)*favorites->count); favorites->items=malloc(sizeof(char *)*favorites->count);
23 23 favorites->items[0]="a"; favorites->items[0]="a";
24 24 favorites->items[1]="b"; favorites->items[1]="b";
25 25 favorites->items[2]="c"; favorites->items[2]="c";
26
27 return favorites;
28 26 } }
29 27
30 void favorites_add(char *newFavorite) {
28 void favorites_add(FavoriteItems *favorites, char *newFavorite) {
31 29 favorites->items=realloc(favorites->items, sizeof(char *)*favorites->count); favorites->items=realloc(favorites->items, sizeof(char *)*favorites->count);
32 30 favorites->items[favorites->count++]=newFavorite; favorites->items[favorites->count++]=newFavorite;
33 31 } }
34 32
35 void favorites_save() {
36 }
37
38 void favorites_close() {
39 if(favorites) {
40 if(favorites->count) {
41 int i;
42 for(i=0; i<favorites->count; i++) {
43 free(favorites->items[i]);
44 }
45 }
46 }
33 void favorites_save(FavoriteItems *favorites) {
47 34 } }
File src/hstr_history.c changed (mode: 100644) (index db01669..2d4b2ca)
11 11 #include <limits.h> #include <limits.h>
12 12 #include <readline/history.h> #include <readline/history.h>
13 13 #include "include/hstr_history.h" #include "include/hstr_history.h"
14 #include "include/hstr_favorites.h"
14 15
15 16 #define NDEBUG #define NDEBUG
16 17 #include <assert.h> #include <assert.h>
 
... ... HistoryItems *get_prioritized_history()
150 151
151 152 radixsort_destroy(&rs); radixsort_destroy(&rs);
152 153
154
155 FavoriteItems favoriteItems;
156 favorites_init(&favoriteItems);
157 favorites_load(&favoriteItems);
158 prioritizedHistory->favorites=favoriteItems.items;
159 prioritizedHistory->favoritesCount=favoriteItems.count;
160
153 161 return prioritizedHistory; return prioritizedHistory;
154 162 } else { } else {
155 163 return NULL; return NULL;
 
... ... HistoryItems *get_prioritized_history()
159 167 void free_prioritized_history() void free_prioritized_history()
160 168 { {
161 169 free(prioritizedHistory->items); free(prioritizedHistory->items);
170 if(prioritizedHistory->favorites) {
171 int i;
172 for(i=0; i<prioritizedHistory->favoritesCount; i++) {
173 free(prioritizedHistory->favorites[i]);
174 }
175 free(prioritizedHistory->favorites);
176 }
162 177 free(prioritizedHistory); free(prioritizedHistory);
163 178 } }
164 179
File src/include/hstr_favorites.h changed (mode: 100644) (index b60c9d9..d6aeb6e)
... ... typedef struct {
19 19 unsigned count; unsigned count;
20 20 } FavoriteItems; } FavoriteItems;
21 21
22 FavoriteItems *favorites_init();
23 FavoriteItems *favorites_load();
24 void favorites_add();
25 void favorites_save();
26 void favorites_close();
22 void favorites_init(FavoriteItems *favorites);
23 void favorites_load(FavoriteItems *favorites);
24 void favorites_add(FavoriteItems *favorites, char *favorite);
25 void favorites_save(FavoriteItems *favorites);
27 26
28 27 #endif #endif
File src/include/hstr_history.h changed (mode: 100644) (index 0c7ef55..b9451ae)
30 30 typedef struct { typedef struct {
31 31 char **items; char **items;
32 32 char **raw; char **raw;
33 char **favorites;
33 34 unsigned count; unsigned count;
34 35 unsigned rawCount; unsigned rawCount;
36 unsigned favoritesCount;
35 37 } HistoryItems; } HistoryItems;
36 38
37 39 HistoryItems *get_prioritized_history(); HistoryItems *get_prioritized_history();
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