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 a994222f43aafc309002326958432664419715fc

Added history mgmt functions.
Author: Martin Dvorak
Author date (UTC): 2013-12-15 14:49
Committer name: Martin Dvorak
Committer date (UTC): 2013-12-15 14:49
Parent(s): 0e564239a88347788296bfd1afbf41a6d7ba0c58
Signing key:
Tree: 0dce7e1fd906acb500fb1dac2a22b88f3ce84446
File Lines added Lines deleted
README.md 1 0
src/hstr_history.c 34 11
File README.md changed (mode: 100644) (index 0390b32..f86ccb6)
... ... INSTALLATION
20 20 * add * add
21 21
22 22 `shopt -s histappend` `shopt -s histappend`
23
23 24 `export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"` `export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"`
24 25
25 26 at the end of `.bashrc` in order to ensure that BASH history of commands at the end of `.bashrc` in order to ensure that BASH history of commands
File src/hstr_history.c changed (mode: 100644) (index 1d57aa3..c5b9d02)
11 11 #include "include/hashset.h" #include "include/hashset.h"
12 12 #include "include/hashmap.h" #include "include/hashmap.h"
13 13 #include "include/radixsort.h" #include "include/radixsort.h"
14 #include <stdbool.h>
15
16 #include "include/hstr_utils.h"
14 17
15 18 #define NDEBUG #define NDEBUG
16 19 #include <assert.h> #include <assert.h>
 
... ... typedef struct {
22 25
23 26 static HistoryItems *history; static HistoryItems *history;
24 27 static HistoryItems *prioritizedHistory; static HistoryItems *prioritizedHistory;
28 static bool dirty;
25 29
26 30 unsigned history_ranking_function(unsigned rank, unsigned newOccurenceOrder, unsigned lng) { unsigned history_ranking_function(unsigned rank, unsigned newOccurenceOrder, unsigned lng) {
27 31 rank+=newOccurenceOrder/10 + lng; rank+=newOccurenceOrder/10 + lng;
28 32 return rank; return rank;
29 33 } }
30 34
31 char *get_history_file() {
32 char *home = getenv(ENV_VAR_HOME);
33 char *fileName = (char*) malloc(
34 strlen(home) + 1 + strlen(FILE_HISTORY) + 1);
35 strcat(strcat(strcpy(fileName, home), "/"), FILE_HISTORY);
36 return fileName;
35 char *get_history_file_name() {
36 char *historyFile=getenv(ENV_VAR_HISTFILE);
37 if(!historyFile || strlen(historyFile)==0) {
38 char *home = getenv(ENV_VAR_HOME);
39 historyFile = (char*) malloc(strlen(home) + 1 + strlen(FILE_HISTORY) + 1);
40 strcat(strcat(strcpy(historyFile, home), "/"), FILE_HISTORY);
41 }
42 return historyFile;
37 43 } }
38 44
39 45 void dump_prioritized_history(HistoryItems *ph) { void dump_prioritized_history(HistoryItems *ph) {
 
... ... HistoryItems *get_history_items() {
120 126
121 127 using_history(); using_history();
122 128
123 char *historyFile = getenv(ENV_VAR_HISTFILE);
124 if(!historyFile || strlen(historyFile)==0) {
125 historyFile=get_history_file();
126 }
127
129 char *historyFile = get_history_file_name();
128 130 if(read_history(historyFile)!=0) { if(read_history(historyFile)!=0) {
129 131 fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile); fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile);
130 132 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
 
... ... HistoryItems *get_prioritized_history() {
167 169 return prioritize_history(fileItems); return prioritize_history(fileItems);
168 170 } }
169 171
172 void history_mgmt_open() {
173 dirty=false;
174 }
175
176 void history_mgmt_remove(char *cmd) {
177 get_history_items();
178
179 int offset=history_search_pos(cmd, 0, 0);
180 while(offset>=0) {
181 free_history_entry(remove_history(offset));
182 offset=history_search_pos(cmd, 0, ++offset);
183 }
170 184
185 write_history(get_history_file_name());
186 dirty=true;
187 }
188
189 void history_mgmt_close() {
190 if(dirty) {
191 fill_terminal_input("history -r\n");
192 }
193 }
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