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 3230917c2a538297fc769ad95cabac8cc962c856

Kernel code style
Author: Martin Dvorak
Author date (UTC): 2014-01-03 00:50
Committer name: Martin Dvorak
Committer date (UTC): 2014-01-03 00:50
Parent(s): f3cde7963931f98d76efe808d2b33841455c3e0e
Signing key:
Tree: 1cfb456f06be135998326b377a60a06c45a5e14c
File Lines added Lines deleted
src/hashset.c 16 8
src/hstr.c 38 19
src/hstr_history.c 15 7
src/hstr_utils.c 6 3
src/radixsort.c 16 8
File src/hashset.c changed (mode: 100644) (index 20ba53c..5229601)
9 9
10 10 #include "include/hashset.h" #include "include/hashset.h"
11 11
12 unsigned int hashmap_hash(const char *str) {
12 unsigned int hashmap_hash(const char *str)
13 {
13 14 int i; int i;
14 15 unsigned int result = 5381; unsigned int result = 5381;
15 16
 
... ... unsigned int hashmap_hash(const char *str) {
21 22 return result % HASH_MAP_SIZE; return result % HASH_MAP_SIZE;
22 23 } }
23 24
24 void hashset_init(HashSet * hs) {
25 void hashset_init(HashSet * hs)
26 {
25 27 int i; int i;
26 28 hs->currentSize = 0; hs->currentSize = 0;
27 29 for( i = 0; i < HASH_MAP_SIZE; i++ ) { for( i = 0; i < HASH_MAP_SIZE; i++ ) {
 
... ... void hashset_init(HashSet * hs) {
29 31 } }
30 32 } }
31 33
32 void *hashset_get(const HashSet * hs, const char *key) {
34 void *hashset_get(const HashSet * hs, const char *key)
35 {
33 36 int listNum = hashmap_hash( key ); int listNum = hashmap_hash( key );
34 37 struct HashSetNode *ptr = hs->lists[ listNum ]; struct HashSetNode *ptr = hs->lists[ listNum ];
35 38
 
... ... void *hashset_get(const HashSet * hs, const char *key) {
39 42 return (ptr != NULL? ptr->value : NULL); return (ptr != NULL? ptr->value : NULL);
40 43 } }
41 44
42 int hashset_contains(const HashSet * hs, const char *key) {
45 int hashset_contains(const HashSet * hs, const char *key)
46 {
43 47 return (hashset_get(hs, key) != NULL); return (hashset_get(hs, key) != NULL);
44 48 } }
45 49
46 int hashset_put(HashSet * hs, const char *key, void *value) {
50 int hashset_put(HashSet * hs, const char *key, void *value)
51 {
47 52 struct HashSetNode *newNode; struct HashSetNode *newNode;
48 53 int listNum; int listNum;
49 54
 
... ... int hashset_put(HashSet * hs, const char *key, void *value) {
69 74 return 1; return 1;
70 75 } }
71 76
72 int hashset_add(HashSet * hs, const char *key) {
77 int hashset_add(HashSet * hs, const char *key)
78 {
73 79 return hashset_put(hs, key, "nil"); return hashset_put(hs, key, "nil");
74 80 } }
75 81
76 int hashset_size(const HashSet * hs) {
82 int hashset_size(const HashSet * hs)
83 {
77 84 return hs->currentSize; return hs->currentSize;
78 85 } }
79 86
80 void hashset_stat( const HashSet * hs ) {
87 void hashset_stat( const HashSet * hs )
88 {
81 89 int i; int i;
82 90 struct HashSetNode *ptr; struct HashSetNode *ptr;
83 91
File src/hstr.c changed (mode: 100644) (index 5c8194f..483b418)
... ... static unsigned selectionSize=0;
62 62 static bool terminalHasColors=FALSE; static bool terminalHasColors=FALSE;
63 63 static char screenLine[1000]; static char screenLine[1000];
64 64
65 int print_prompt(WINDOW *win) {
65 int print_prompt(WINDOW *win)
66 {
66 67 char *hostname = get_hostname(); char *hostname = get_hostname();
67 68 char *user = getenv(ENV_VAR_USER); char *user = getenv(ENV_VAR_USER);
68 69 int xoffset = 1; int xoffset = 1;
 
... ... int print_prompt(WINDOW *win) {
73 74 return xoffset+strlen(user)+1+strlen(hostname)+1; return xoffset+strlen(user)+1+strlen(hostname)+1;
74 75 } }
75 76
76 void print_help_label(WINDOW *win) {
77 void print_help_label(WINDOW *win)
78 {
77 79 snprintf(screenLine, getmaxx(win), "%s", LABEL_HELP); snprintf(screenLine, getmaxx(win), "%s", LABEL_HELP);
78 80 mvwprintw(win, Y_OFFSET_HELP, 0, screenLine); mvwprintw(win, Y_OFFSET_HELP, 0, screenLine);
79 81 refresh(); refresh();
80 82 } }
81 83
82 void print_cmd_deleted_label(WINDOW *win, char *cmd, int occurences) {
84 void print_cmd_deleted_label(WINDOW *win, char *cmd, int occurences)
85 {
83 86 snprintf(screenLine, getmaxx(win), "History item '%s' deleted (%d occurrence%s)", cmd, occurences, (occurences==1?"":"s")); snprintf(screenLine, getmaxx(win), "History item '%s' deleted (%d occurrence%s)", cmd, occurences, (occurences==1?"":"s"));
84 87 mvwprintw(win, Y_OFFSET_HELP, 0, screenLine); mvwprintw(win, Y_OFFSET_HELP, 0, screenLine);
85 88 clrtoeol(); clrtoeol();
86 89 refresh(); refresh();
87 90 } }
88 91
89 void print_history_label(WINDOW *win) {
92 void print_history_label(WINDOW *win)
93 {
90 94 char message[512]; char message[512];
91 95
92 96 int width=getmaxx(win); int width=getmaxx(win);
 
... ... void print_history_label(WINDOW *win) {
105 109 refresh(); refresh();
106 110 } }
107 111
108 unsigned get_max_history_items(WINDOW *win) {
112 unsigned get_max_history_items(WINDOW *win)
113 {
109 114 return (getmaxy(win)-Y_OFFSET_ITEMS); return (getmaxy(win)-Y_OFFSET_ITEMS);
110 115 } }
111 116
112 117
113 void alloc_selection(unsigned size) {
118 void alloc_selection(unsigned size)
119 {
114 120 selectionSize=size; selectionSize=size;
115 121 if(selection!=NULL) { if(selection!=NULL) {
116 122 free(selection); free(selection);
 
... ... void alloc_selection(unsigned size) {
121 127 } }
122 128 } }
123 129
124 unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) {
130 unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCount)
131 {
125 132 alloc_selection(sizeof(char*) * maxSelectionCount); // TODO realloc alloc_selection(sizeof(char*) * maxSelectionCount); // TODO realloc
126 133 unsigned i, selectionCount=0; unsigned i, selectionCount=0;
127 134
 
... ... unsigned make_selection(char *prefix, HistoryItems *history, int maxSelectionCou
150 157 return selectionCount; return selectionCount;
151 158 } }
152 159
153 char *print_selection(WINDOW *win, unsigned maxHistoryItems, char *prefix, HistoryItems *history) {
160 char *print_selection(WINDOW *win, unsigned maxHistoryItems, char *prefix, HistoryItems *history)
161 {
154 162 char *result=""; char *result="";
155 163 unsigned selectionCount=make_selection(prefix, history, maxHistoryItems); unsigned selectionCount=make_selection(prefix, history, maxHistoryItems);
156 164 if (selectionCount > 0) { if (selectionCount > 0) {
 
... ... char *print_selection(WINDOW *win, unsigned maxHistoryItems, char *prefix, Histo
184 192 return result; return result;
185 193 } }
186 194
187 void highlight_selection(int selectionCursorPosition, int previousSelectionCursorPosition) {
195 void highlight_selection(int selectionCursorPosition, int previousSelectionCursorPosition)
196 {
188 197 if(previousSelectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) { if(previousSelectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) {
189 198 mvprintw(Y_OFFSET_ITEMS+previousSelectionCursorPosition, 0, " "); mvprintw(Y_OFFSET_ITEMS+previousSelectionCursorPosition, 0, " ");
190 199 } }
 
... ... void highlight_selection(int selectionCursorPosition, int previousSelectionCurso
193 202 } }
194 203 } }
195 204
196 void color_start() {
205 void color_start()
206 {
197 207 terminalHasColors=has_colors(); terminalHasColors=has_colors();
198 208 if(terminalHasColors) { if(terminalHasColors) {
199 209 start_color(); start_color();
200 210 } }
201 211 } }
202 212
203 void color_init_pair(short int x, short int y, short int z) {
213 void color_init_pair(short int x, short int y, short int z)
214 {
204 215 if(terminalHasColors) { if(terminalHasColors) {
205 216 init_pair(x, y, z); init_pair(x, y, z);
206 217 } }
207 218 } }
208 219
209 void color_attr_on(int c) {
220 void color_attr_on(int c)
221 {
210 222 if(terminalHasColors) { if(terminalHasColors) {
211 223 attron(c); attron(c);
212 224 } }
213 225 } }
214 226
215 void color_attr_off(int c) {
227 void color_attr_off(int c)
228 {
216 229 if(terminalHasColors) { if(terminalHasColors) {
217 230 attroff(c); attroff(c);
218 231 } }
219 232 } }
220 233
221 void selection_remove(char *cmd, HistoryItems *history) {
234 void selection_remove(char *cmd, HistoryItems *history)
235 {
222 236 if(history->count) { if(history->count) {
223 237 int i, w; int i, w;
224 238 for(i=0, w=0; i<history->count; i++) { for(i=0, w=0; i<history->count; i++) {
 
... ... void selection_remove(char *cmd, HistoryItems *history) {
231 245 } }
232 246 } }
233 247
234 char *selection_loop(HistoryItems *history) {
248 char *selection_loop(HistoryItems *history)
249 {
235 250 initscr(); initscr();
236 251 color_start(); color_start();
237 252
 
... ... char *selection_loop(HistoryItems *history) {
364 379 return result; return result;
365 380 } }
366 381
367 void install_write() {
382 void install_write()
383 {
368 384 char *home = getenv(ENV_VAR_HOME); char *home = getenv(ENV_VAR_HOME);
369 385 sprintf(screenLine, "%s/%s", home, FILE_BASHRC); sprintf(screenLine, "%s/%s", home, FILE_BASHRC);
370 386 FILE *file = fopen(screenLine,"a"); FILE *file = fopen(screenLine,"a");
 
... ... void install_write() {
374 390 fclose(file); fclose(file);
375 391 } }
376 392
377 void install_show() {
393 void install_show()
394 {
378 395 printf("%s", INSTALL_STRING); printf("%s", INSTALL_STRING);
379 396 } }
380 397
381 void hstr() {
398 void hstr()
399 {
382 400 HistoryItems *history=get_prioritized_history(); HistoryItems *history=get_prioritized_history();
383 401 history_mgmt_open(); history_mgmt_open();
384 402 char *command = selection_loop(history); char *command = selection_loop(history);
 
... ... void hstr() {
387 405 free_prioritized_history(); free_prioritized_history();
388 406 } }
389 407
390 int main(int argc, char *argv[]) {
408 int main(int argc, char *argv[])
409 {
391 410 if(argc>1) { if(argc>1) {
392 411 install_show(); install_show();
393 412 } else { } else {
File src/hstr_history.c changed (mode: 100644) (index f472cc7..28b4537)
7 7 ============================================================================ ============================================================================
8 8 */ */
9 9
10 #include <readline/history.h>
10 11 #include "include/hstr_history.h" #include "include/hstr_history.h"
11 12
12 13 #define NDEBUG #define NDEBUG
 
... ... static const char *commandBlacklist[] = {"ls", "pwd", "cd", "hh", "mc"};
29 30
30 31 #define HISTORY_RANKING_FUNCTION(RANK,NEWOCCURENCEORDER,LNG) RANK+NEWOCCURENCEORDER/10+LNG #define HISTORY_RANKING_FUNCTION(RANK,NEWOCCURENCEORDER,LNG) RANK+NEWOCCURENCEORDER/10+LNG
31 32
32 char *get_history_file_name() {
33 char *get_history_file_name()
34 {
33 35 char *historyFile=getenv(ENV_VAR_HISTFILE); char *historyFile=getenv(ENV_VAR_HISTFILE);
34 36 if(!historyFile || strlen(historyFile)==0) { if(!historyFile || strlen(historyFile)==0) {
35 37 char *home = getenv(ENV_VAR_HOME); char *home = getenv(ENV_VAR_HOME);
 
... ... char *get_history_file_name() {
39 41 return historyFile; return historyFile;
40 42 } }
41 43
42 void dump_prioritized_history(HistoryItems *ph) {
44 void dump_prioritized_history(HistoryItems *ph)
45 {
43 46 printf("\n\nPrioritized history:"); printf("\n\nPrioritized history:");
44 47 int i; int i;
45 48 for(i=0; i<ph->count; i++) { for(i=0; i<ph->count; i++) {
 
... ... void dump_prioritized_history(HistoryItems *ph) {
52 55 printf("\n"); fflush(stdout); printf("\n"); fflush(stdout);
53 56 } }
54 57
55 HistoryItems *get_prioritized_history() {
58 HistoryItems *get_prioritized_history()
59 {
56 60 using_history(); using_history();
57 61
58 62 char *historyFile = get_history_file_name(); char *historyFile = get_history_file_name();
 
... ... HistoryItems *get_prioritized_history() {
132 136 } }
133 137 } }
134 138
135 void free_prioritized_history() {
139 void free_prioritized_history()
140 {
136 141 free(prioritizedHistory->items); free(prioritizedHistory->items);
137 142 free(prioritizedHistory); free(prioritizedHistory);
138 143 } }
139 144
140 void history_mgmt_open() {
145 void history_mgmt_open()
146 {
141 147 dirty=false; dirty=false;
142 148 } }
143 149
144 int history_mgmt_remove(char *cmd) {
150 int history_mgmt_remove(char *cmd)
151 {
145 152 int offset=history_search_pos(cmd, 0, 0), occurences=0; int offset=history_search_pos(cmd, 0, 0), occurences=0;
146 153 while(offset>=0) { while(offset>=0) {
147 154 occurences++; occurences++;
 
... ... int history_mgmt_remove(char *cmd) {
155 162 return occurences; return occurences;
156 163 } }
157 164
158 void history_mgmt_close() {
165 void history_mgmt_close()
166 {
159 167 if(dirty) { if(dirty) {
160 168 fill_terminal_input("history -r\n", false); fill_terminal_input("history -r\n", false);
161 169 } }
File src/hstr_utils.c changed (mode: 100644) (index 4aa45ee..d855e8e)
13 13 #define HOSTNAME_BUFFER 100 #define HOSTNAME_BUFFER 100
14 14 #define PROC_HOSTNAME "/proc/sys/kernel/hostname" #define PROC_HOSTNAME "/proc/sys/kernel/hostname"
15 15
16 void tiocsti() {
16 void tiocsti()
17 {
17 18 char buf[] = DEFAULT_COMMAND; char buf[] = DEFAULT_COMMAND;
18 19 int i; int i;
19 20 for (i = 0; i < sizeof buf - 1; i++) { for (i = 0; i < sizeof buf - 1; i++) {
 
... ... void tiocsti() {
21 22 } }
22 23 } }
23 24
24 void fill_terminal_input(char *cmd, bool padding){
25 void fill_terminal_input(char *cmd, bool padding)
26 {
25 27 if(cmd) { if(cmd) {
26 28 size_t size = strlen(cmd); size_t size = strlen(cmd);
27 29 unsigned i; unsigned i;
 
... ... void fill_terminal_input(char *cmd, bool padding){
35 37 } }
36 38 } }
37 39
38 void reverse_char_pointer_array(char **array, unsigned length) {
40 void reverse_char_pointer_array(char **array, unsigned length)
41 {
39 42 char *temp; char *temp;
40 43 unsigned i; unsigned i;
41 44 for (i=0; i<length/2; i++) { for (i=0; i<length/2; i++) {
File src/radixsort.c changed (mode: 100644) (index 78f6554..7bf6e09)
12 12 #define GET_TOP_INDEX(KEY) KEY/SLOT_SIZE #define GET_TOP_INDEX(KEY) KEY/SLOT_SIZE
13 13 #define GET_LOW_INDEX(KEY) KEY%SLOT_SIZE #define GET_LOW_INDEX(KEY) KEY%SLOT_SIZE
14 14
15 void radixsort_init(RadixSorter *rs, unsigned keyLimit) {
15 void radixsort_init(RadixSorter *rs, unsigned keyLimit)
16 {
16 17 unsigned topIndexLimit=GET_TOP_INDEX(keyLimit); unsigned topIndexLimit=GET_TOP_INDEX(keyLimit);
17 18 rs->size=0; rs->size=0;
18 19 rs->topDigits=malloc(topIndexLimit * sizeof(RadixItem ***)); rs->topDigits=malloc(topIndexLimit * sizeof(RadixItem ***));
 
... ... void radixsort_init(RadixSorter *rs, unsigned keyLimit) {
24 25 rs->_slotsCount=0; rs->_slotsCount=0;
25 26 } }
26 27
27 RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex) {
28 RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex)
29 {
28 30 RadixItem **slot=malloc(SLOT_SIZE * sizeof(RadixItem *)); RadixItem **slot=malloc(SLOT_SIZE * sizeof(RadixItem *));
29 31 memset(slot, 0, SLOT_SIZE * sizeof(RadixItem *)); memset(slot, 0, SLOT_SIZE * sizeof(RadixItem *));
30 32
 
... ... RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex) {
38 40 return slot; return slot;
39 41 } }
40 42
41 void radixsort_add(RadixSorter *rs, RadixItem *item) {
43 void radixsort_add(RadixSorter *rs, RadixItem *item)
44 {
42 45 unsigned topIndex = GET_TOP_INDEX(item->key); unsigned topIndex = GET_TOP_INDEX(item->key);
43 46 unsigned lowIndex = GET_LOW_INDEX(item->key); unsigned lowIndex = GET_LOW_INDEX(item->key);
44 47
 
... ... void radixsort_add(RadixSorter *rs, RadixItem *item) {
62 65 rs->_slotDescriptors[topIndex]->size++; rs->_slotDescriptors[topIndex]->size++;
63 66 } }
64 67
65 void radix_dec_slot_descriptor_size(RadixSorter *rs, RadixSlot *descriptor, unsigned key, unsigned topIndex) {
68 void radix_dec_slot_descriptor_size(RadixSorter *rs, RadixSlot *descriptor, unsigned key, unsigned topIndex)
69 {
66 70 descriptor->size--; descriptor->size--;
67 71 if(!descriptor->size) { if(!descriptor->size) {
68 72 descriptor->min=rs->keyLimit; descriptor->min=rs->keyLimit;
 
... ... void radix_dec_slot_descriptor_size(RadixSorter *rs, RadixSlot *descriptor, unsi
80 84 } }
81 85 } }
82 86
83 RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data) {
87 RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data)
88 {
84 89 // TODO optimization: fix min/max on cut of a value // TODO optimization: fix min/max on cut of a value
85 90 if(key<=rs->maxKey) { if(key<=rs->maxKey) {
86 91 unsigned topIndex = GET_TOP_INDEX(key); unsigned topIndex = GET_TOP_INDEX(key);
 
... ... RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data) {
113 118 return NULL; return NULL;
114 119 } }
115 120
116 RadixItem **radixsort_dump(RadixSorter *rs) {
121 RadixItem **radixsort_dump(RadixSorter *rs)
122 {
117 123 if(rs->size>0) { if(rs->size>0) {
118 124 RadixItem **result=malloc(rs->size * sizeof(RadixItem *)); RadixItem **result=malloc(rs->size * sizeof(RadixItem *));
119 125 int t = GET_TOP_INDEX(rs->maxKey); int t = GET_TOP_INDEX(rs->maxKey);
 
... ... RadixItem **radixsort_dump(RadixSorter *rs) {
146 152 return NULL; return NULL;
147 153 } }
148 154
149 void radixsort_stat(RadixSorter *rs) {
155 void radixsort_stat(RadixSorter *rs)
156 {
150 157 printf("\n Radixsort (size/max/limit): %u %u %u", rs->size, rs->maxKey, rs->keyLimit); printf("\n Radixsort (size/max/limit): %u %u %u", rs->size, rs->maxKey, rs->keyLimit);
151 158 if(rs->size>0) { if(rs->size>0) {
152 159 int t = GET_TOP_INDEX(rs->maxKey); int t = GET_TOP_INDEX(rs->maxKey);
 
... ... void radixsort_stat(RadixSorter *rs) {
182 189 fflush(stdout); fflush(stdout);
183 190 } }
184 191
185 void radixsort_destroy(RadixSorter *rs) {
192 void radixsort_destroy(RadixSorter *rs)
193 {
186 194 // radix items: DONE (passed on dump() by reference) // radix items: DONE (passed on dump() by reference)
187 195 // rs: DONE (created and destroyed by caller) // rs: DONE (created and destroyed by caller)
188 196 // slots: // slots:
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