Ensure 'skip-favorites-comments' does not strip comments when adding a

new favorite.
This commit is contained in:
Thibault Charbonnier 2020-12-10 11:59:29 -08:00
parent 516a46e1d3
commit f8cf3aa795
3 changed files with 24 additions and 8 deletions

View file

@ -832,8 +832,8 @@ unsigned hstr_make_selection(char* prefix, HistoryItems* history, unsigned maxSe
count=history->rawCount;
break;
case HSTR_VIEW_FAVORITES:
source=hstr->favorites->items;
count=hstr->favorites->count;
source=hstr->favorites->itemsView;
count=hstr->favorites->countView;
break;
case HSTR_VIEW_RANKING:
default:

View file

@ -22,6 +22,8 @@
void favorites_init(FavoriteItems* favorites)
{
favorites->itemsView=NULL;
favorites->countView=0;
favorites->items=NULL;
favorites->count=0;
favorites->loaded=false;
@ -83,18 +85,26 @@ void favorites_get(FavoriteItems* favorites)
}
favorites->items = malloc(sizeof(char*) * favorites->count);
favorites->itemsView = malloc(sizeof(char*) * favorites->count);
favorites->count = 0;
char* pb=fileContent, *pe, *s;
pe=strchr(fileContent, '\n');
while(pe!=NULL) {
*pe=0;
if(!hashset_contains(favorites->set,pb)) {
if(!favorites->skipComments || !(strlen(pb) && pb[0]=='#')) {
s=hstr_strdup(pb);
favorites->items[favorites->count++]=s;
hashset_add(favorites->set,s);
}
if(!strlen(pb)) {
goto next;
}
s=hstr_strdup(pb);
favorites->items[favorites->count++]=s;
if(pb[0]=='#' && favorites->skipComments) {
goto next;
}
if(!hashset_contains(favorites->set,pb)) {
s=hstr_strdup(pb);
favorites->itemsView[favorites->countView++]=s;
hashset_add(favorites->set,s);
}
next:
pb=pe+1;
pe=strchr(pb, '\n');
}
@ -206,7 +216,11 @@ void favorites_destroy(FavoriteItems* favorites)
for(i=0; i<favorites->count; i++) {
free(favorites->items[i]);
}
for(i=0; i<favorites->countView; i++) {
free(favorites->itemsView[i]);
}
free(favorites->items);
free(favorites->itemsView);
hashset_destroy(favorites->set, false);
free(favorites->set);
free(favorites);

View file

@ -27,7 +27,9 @@
typedef struct {
char** items;
char** itemsView;
unsigned count;
unsigned countView;
bool loaded;
bool reorderOnChoice;
bool skipComments;