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 a9e5750b3018250e4a48335e4150dee7b2551ece

Adding favorites saving.
Author: Martin Dvorak
Author date (UTC): 2014-03-29 16:11
Committer name: Martin Dvorak
Committer date (UTC): 2014-03-29 16:11
Parent(s): 105d66c75d883b8da24364c0b1e3602d09c356ae
Signing key:
Tree: a8e2c96a3dcd9daf0660d589e145d2a11656c57d
File Lines added Lines deleted
src/hstr_favorites.c 34 8
File src/hstr_favorites.c changed (mode: 100644) (index ad30018..afe316a)
... ... void favorites_init(FavoriteItems *favorites)
22 22 favorites->loaded=false; favorites->loaded=false;
23 23 } }
24 24
25 char* favorites_get_filename()
26 {
27 char *home = getenv(ENV_VAR_HOME);
28 char *fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_RC) + 1);
29 strcpy(fileName, home);
30 strcat(fileName, "/");
31 strcat(fileName, FILE_HH_RC);
32 return fileName;
33 }
34
25 35 void favorites_get(FavoriteItems *favorites) void favorites_get(FavoriteItems *favorites)
26 36 { {
27 37 if(!favorites->loaded) { if(!favorites->loaded) {
28 char *home = getenv(ENV_VAR_HOME);
29 char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1);
30 strcpy(fileName,home);
31 strcat(fileName,"/");
32 strcat(fileName,FILE_HH_RC);
33
38 char* fileName = favorites_get_filename();
34 39 char *file_contents=NULL; char *file_contents=NULL;
35 40 if(access(fileName, F_OK) != -1) { if(access(fileName, F_OK) != -1) {
36 41 long input_file_size; long input_file_size;
 
... ... void favorites_get(FavoriteItems *favorites)
73 78 favorites->loaded=true; favorites->loaded=true;
74 79 return; return;
75 80 } }
81 free(fileName);
76 82 } }
77 83 } }
78 84
 
... ... void favorites_choose(FavoriteItems *favorites, char *choice)
114 120
115 121 void favorites_remove(FavoriteItems *favorites, char *almostDead) void favorites_remove(FavoriteItems *favorites, char *almostDead)
116 122 { {
117 // TODO: keep slot you have, just change count
123 // TODO: keep slot you have, just change count > ? by pointer or strstr?
118 124
119 125 favorites_save(favorites); favorites_save(favorites);
120 126 } }
121 127
122 128 void favorites_save(FavoriteItems *favorites) void favorites_save(FavoriteItems *favorites)
123 129 { {
130 char *fileName=favorites_get_filename();
131
124 132 if(favorites->count) { if(favorites->count) {
125 // TODO shrink file and rewrite it (can be shorter)
133 FILE *output_file = fopen(fileName, "wb");
134 rewind(output_file);
135 int i;
136 for(i=0; i<favorites->count; i++) {
137 if(fwrite(favorites->items[i], sizeof(char), strlen(favorites->items[i]), output_file)==-1) {
138 exit(EXIT_FAILURE);
139 }
140 if(fwrite("\n", sizeof(char), strlen("\n"), output_file)==-1) {
141 exit(EXIT_FAILURE);
142 }
143 }
144 fclose(output_file);
145 } else {
146 if(access(fileName, F_OK) != -1) {
147 FILE *output_file = fopen(fileName, "wb");
148 fclose(output_file);
149 }
126 150 } }
151
152 free(fileName);
127 153 } }
128 154
129 155 void favorites_destroy(FavoriteItems *favorites) void favorites_destroy(FavoriteItems *favorites)
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