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 105d66c75d883b8da24364c0b1e3602d09c356ae

Loading of favorites from resource file added.
Author: Martin Dvorak
Author date (UTC): 2014-03-29 12:19
Committer name: Martin Dvorak
Committer date (UTC): 2014-03-29 12:19
Parent(s): 0f32fe0c657da22ef6215700dfbf3b2f52f87a63
Signing key:
Tree: 9e1c87a6732a3cdb28f92f3b2fc163a0819c629a
File Lines added Lines deleted
src/hstr_favorites.c 62 43
src/include/hstr_favorites.h 3 3
File src/hstr_favorites.c changed (mode: 100644) (index 83a2695..ad30018)
... ... void favorites_init(FavoriteItems *favorites)
19 19 { {
20 20 favorites->items=NULL; favorites->items=NULL;
21 21 favorites->count=0; favorites->count=0;
22 favorites->loaded=false;
22 23 } }
23 24
24 void favorites_load(FavoriteItems *favorites)
25 void favorites_get(FavoriteItems *favorites)
25 26 { {
26 // TODO fake initialization instead of .hhrc load
27 favorites->count=2;
28 favorites->items=malloc(sizeof(char *)*favorites->count);
29 favorites->items[0]=malloc(2);
30 strcpy(favorites->items[0],"a");
31 favorites->items[1]=malloc(2);
32 strcpy(favorites->items[1],"b");
33
34 lazy loading (boolean indicator to FavoriteItems)
35
36 // TODO load from file
37
38 char *home = getenv(ENV_VAR_HOME);
39 char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1);
40 strcpy(fileName,home);
41 strcat(fileName,"/");
42 strcat(fileName,FILE_HH_RC);
43
44 char *file_contents=NULL;
45 if(access(fileName, F_OK) != -1) {
46 long input_file_size;
27 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
34 char *file_contents=NULL;
35 if(access(fileName, F_OK) != -1) {
36 long input_file_size;
37
38 FILE *input_file = fopen(fileName, "rb");
39 fseek(input_file, 0, SEEK_END);
40 input_file_size = ftell(input_file);
41 rewind(input_file);
42 file_contents = malloc((input_file_size + 1) * (sizeof(char)));
43 if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) {
44 exit(EXIT_FAILURE);
45 }
46 fclose(input_file);
47 file_contents[input_file_size] = 0;
48
49 if(file_contents && strlen(file_contents)) {
50 favorites->count = 0;
51 char *p=strchr(file_contents,'\n');
52 while (p!=NULL) {
53 favorites->count++;
54 p=strchr(p+1,'\n');
55 }
47 56
48 FILE *input_file = fopen(fileName, "rb");
49 fseek(input_file, 0, SEEK_END);
50 input_file_size = ftell(input_file);
51 rewind(input_file);
52 file_contents = malloc((input_file_size + 1) * (sizeof(char)));
53 if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) {
54 exit(EXIT_FAILURE);
57 favorites->items = malloc(sizeof(char*) * favorites->count);
58 int i = 0;
59 char *pb=file_contents, *pe;
60 pe=strchr(file_contents, '\n');
61 while(pe!=NULL) {
62 favorites->items[i]=pb;
63 *pe=0;
64 favorites->items[i]=strdup(pb);
65 pb=pe+1;
66 pe=strchr(pb, '\n');
67 i++;
68 }
69 free(file_contents);
70 }
71 } else {
72 // favorites file not found > favorites don't exist yet
73 favorites->loaded=true;
74 return;
55 75 } }
56 fclose(input_file);
57 file_contents[input_file_size] = 0;
58 } else {
59 fprintf(stderr,"\nHistory file not found: %s\n",fileName);
60 }
61
62 if(file_contents) {
63 split & process & initilize favorites
64 76 } }
65 77 } }
66 78
67 79 void favorites_add(FavoriteItems *favorites, char *newFavorite) void favorites_add(FavoriteItems *favorites, char *newFavorite)
68 80 { {
69 favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count);
70 favorites->items[favorites->count-1]=newFavorite;
81 if(favorites->count) {
82 favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count);
83 favorites->items[favorites->count-1]=strdup(newFavorite);
84 favorites_choose(favorites, newFavorite);
85 } else {
86 favorites->items=malloc(sizeof(char*));
87 favorites->items[0]=strdup(newFavorite);
88 favorites->count=1;
89 }
71 90
72 favorites_choose(favorites, newFavorite);
73 91 favorites_save(favorites); favorites_save(favorites);
74 92 } }
75 93
 
... ... void favorites_choose(FavoriteItems *favorites, char *choice)
96 114
97 115 void favorites_remove(FavoriteItems *favorites, char *almostDead) void favorites_remove(FavoriteItems *favorites, char *almostDead)
98 116 { {
99 // TODO: keept slot you have, just change count
117 // TODO: keep slot you have, just change count
100 118
101 119 favorites_save(favorites); favorites_save(favorites);
102 120 } }
103 121
104 122 void favorites_save(FavoriteItems *favorites) void favorites_save(FavoriteItems *favorites)
105 123 { {
106 create/update history file
107
124 if(favorites->count) {
125 // TODO shrink file and rewrite it (can be shorter)
126 }
108 127 } }
109 128
110 129 void favorites_destroy(FavoriteItems *favorites) void favorites_destroy(FavoriteItems *favorites)
File src/include/hstr_favorites.h changed (mode: 100644) (index 45d4e91..5a91a86)
15 15 #define ENV_VAR_USER "USER" #define ENV_VAR_USER "USER"
16 16 #define ENV_VAR_HOME "HOME" #define ENV_VAR_HOME "HOME"
17 17
18 #define FILE_HH_RC ".hhrc"
18 #define FILE_HH_RC ".hh_favorites"
19 19
20 20 typedef struct { typedef struct {
21 21 char **items; char **items;
22 22 unsigned count; unsigned count;
23 bool loaded;
23 24 } FavoriteItems; } FavoriteItems;
24 25
25 26 void favorites_init(FavoriteItems *favorites); void favorites_init(FavoriteItems *favorites);
26 void favorites_load(FavoriteItems *favorites);
27 void favorites_get(FavoriteItems *favorites);
27 28 void favorites_add(FavoriteItems *favorites, char *favorite); void favorites_add(FavoriteItems *favorites, char *favorite);
28 29 void favorites_choose(FavoriteItems *favorites, char *choice); void favorites_choose(FavoriteItems *favorites, char *choice);
29 30 void favorites_remove(FavoriteItems *favorites, char *almostDead); void favorites_remove(FavoriteItems *favorites, char *almostDead);
30 void favorites_save(FavoriteItems *favorites);
31 31 void favorites_destroy(FavoriteItems *favorites); void favorites_destroy(FavoriteItems *favorites);
32 32
33 33 #endif #endif
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