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 1aab89187f1e15a6ba39275cc17f1ec80fd42bd6

Favorites: in memory adding sketched.
Author: Martin Dvorak
Author date (UTC): 2014-03-23 05:05
Committer name: Martin Dvorak
Committer date (UTC): 2014-03-23 05:05
Parent(s): a8337ad952515dedd560d0cd15895ba3321a9148
Signing key:
Tree: 3fef250cddf234442793355421b6803ee7ef5401
File Lines added Lines deleted
src/hstr.c 16 2
src/hstr_favorites.c 43 2
src/hstr_history.c 5 13
src/include/hstr_favorites.h 3 0
src/include/hstr_history.h 2 2
File src/hstr.c changed (mode: 100644) (index 26e21e2..374c4d5)
38 38
39 39 #define K_CTRL_A 1 #define K_CTRL_A 1
40 40 #define K_CTRL_E 5 #define K_CTRL_E 5
41 #define K_CTRL_F 6
41 42 #define K_CTRL_G 7 #define K_CTRL_G 7
42 43 #define K_CTRL_H 8 #define K_CTRL_H 8
43 44 #define K_CTRL_L 12 #define K_CTRL_L 12
 
... ... unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCou
258 259 count=history->rawCount; count=history->rawCount;
259 260 break; break;
260 261 case HH_VIEW_FAVORITES: case HH_VIEW_FAVORITES:
261 source=history->favorites;
262 count=history->favoritesCount;
262 source=history->favorites->items;
263 count=history->favorites->count;
263 264 break; break;
264 265 } }
265 266
 
... ... void loop_to_select(HistoryItems *history)
497 498 print_history_label(history); print_history_label(history);
498 499 selectionCursorPosition=0; selectionCursorPosition=0;
499 500 break; break;
501 case K_CTRL_F:
502 if(selectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) {
503 result=selection[selectionCursorPosition];
504
505 if(historyView==HH_VIEW_FAVORITES) {
506 favorites_choose(history->favorites, result);
507 } else {
508 favorites_add(history->favorites, result);
509 }
510 result=print_selection(maxHistoryItems, pattern, history);
511 selectionCursorPosition=0;
512 }
513 break;
500 514 case KEY_RESIZE: case KEY_RESIZE:
501 515 print_history_label(history); print_history_label(history);
502 516 result=print_selection(maxHistoryItems, pattern, history); result=print_selection(maxHistoryItems, pattern, history);
File src/hstr_favorites.c changed (mode: 100644) (index 1dbfd81..905ee57)
7 7 ============================================================================ ============================================================================
8 8 */ */
9 9
10 #include <stdbool.h>
10 11 #include <string.h> #include <string.h>
11 12 #include "include/hstr_favorites.h" #include "include/hstr_favorites.h"
12 13
 
... ... void favorites_load(FavoriteItems *favorites)
31 32
32 33 void favorites_add(FavoriteItems *favorites, char *newFavorite) void favorites_add(FavoriteItems *favorites, char *newFavorite)
33 34 { {
34 favorites->items=realloc(favorites->items, sizeof(char *)*favorites->count);
35 favorites->items[favorites->count++]=newFavorite;
35 favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count);
36 favorites->items[favorites->count-1]=newFavorite;
37
38 favorites_choose(favorites, newFavorite);
39 favorites_save(favorites);
40 }
41
42 void favorites_choose(FavoriteItems *favorites, char *choice)
43 {
44 if(favorites->count && choice) {
45 int i;
46 char *b=0, *next;
47 for(i=0; i<favorites->count; i++) {
48 if(!strcmp(favorites->items[i],choice)) {
49 favorites->items[0]=favorites->items[i];
50 if(b) {
51 favorites->items[i]=b;
52 }
53 return;
54 }
55 next=favorites->items[i];
56 favorites->items[i]=b;
57 b=next;
58 }
59 }
60 favorites_save(favorites);
61 }
62
63 void favorites_remove(FavoriteItems *favorites, char *almostDead)
64 {
65 // TODO: keept slot you have, just change count
36 66
37 67 favorites_save(favorites); favorites_save(favorites);
38 68 } }
 
... ... void favorites_add(FavoriteItems *favorites, char *newFavorite)
40 70 void favorites_save(FavoriteItems *favorites) void favorites_save(FavoriteItems *favorites)
41 71 { {
42 72 } }
73
74 void favorites_destroy(FavoriteItems *favorites)
75 {
76 if(favorites) {
77 int i;
78 for(i=0; i<favorites->count; i++) {
79 free(favorites->items[i]);
80 }
81 free(favorites);
82 }
83 }
File src/hstr_history.c changed (mode: 100644) (index 2d4b2ca..536292f)
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"
15 14
16 15 #define NDEBUG #define NDEBUG
17 16 #include <assert.h> #include <assert.h>
 
... ... HistoryItems *get_prioritized_history()
152 151 radixsort_destroy(&rs); radixsort_destroy(&rs);
153 152
154 153
155 FavoriteItems favoriteItems;
156 favorites_init(&favoriteItems);
157 favorites_load(&favoriteItems);
158 prioritizedHistory->favorites=favoriteItems.items;
159 prioritizedHistory->favoritesCount=favoriteItems.count;
154 FavoriteItems *favoriteItems=malloc(sizeof(FavoriteItems));
155 favorites_init(favoriteItems);
156 favorites_load(favoriteItems);
157 prioritizedHistory->favorites=favoriteItems;
160 158
161 159 return prioritizedHistory; return prioritizedHistory;
162 160 } else { } else {
 
... ... HistoryItems *get_prioritized_history()
167 165 void free_prioritized_history() void free_prioritized_history()
168 166 { {
169 167 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 }
168 favorites_destroy(prioritizedHistory->favorites);
177 169 free(prioritizedHistory); free(prioritizedHistory);
178 170 } }
179 171
File src/include/hstr_favorites.h changed (mode: 100644) (index d6aeb6e..b134f9f)
... ... typedef struct {
22 22 void favorites_init(FavoriteItems *favorites); void favorites_init(FavoriteItems *favorites);
23 23 void favorites_load(FavoriteItems *favorites); void favorites_load(FavoriteItems *favorites);
24 24 void favorites_add(FavoriteItems *favorites, char *favorite); void favorites_add(FavoriteItems *favorites, char *favorite);
25 void favorites_choose(FavoriteItems *favorites, char *choice);
26 void favorites_remove(FavoriteItems *favorites, char *almostDead);
25 27 void favorites_save(FavoriteItems *favorites); void favorites_save(FavoriteItems *favorites);
28 void favorites_destroy(FavoriteItems *favorites);
26 29
27 30 #endif #endif
File src/include/hstr_history.h changed (mode: 100644) (index b9451ae..e38fb68)
17 17 #include <readline/history.h> #include <readline/history.h>
18 18 #include <unistd.h> #include <unistd.h>
19 19 #include <stdbool.h> #include <stdbool.h>
20 #include "hstr_favorites.h"
20 21 #include "hstr_utils.h" #include "hstr_utils.h"
21 22 #include "hashset.h" #include "hashset.h"
22 23 #include "radixsort.h" #include "radixsort.h"
 
30 31 typedef struct { typedef struct {
31 32 char **items; char **items;
32 33 char **raw; char **raw;
33 char **favorites;
34 FavoriteItems *favorites;
34 35 unsigned count; unsigned count;
35 36 unsigned rawCount; unsigned rawCount;
36 unsigned favoritesCount;
37 37 } HistoryItems; } HistoryItems;
38 38
39 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