Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure 'skip-favorites-comments' does not strip comments when adding a new favorite. #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/hstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 19 additions & 5 deletions src/hstr_favorites.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

void favorites_init(FavoriteItems* favorites)
{
favorites->itemsView=NULL;
favorites->countView=0;
favorites->items=NULL;
favorites->count=0;
favorites->loaded=false;
Expand Down Expand Up @@ -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(!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)) {
if(!favorites->skipComments || !(strlen(pb) && pb[0]=='#')) {
s=hstr_strdup(pb);
favorites->items[favorites->count++]=s;
hashset_add(favorites->set,s);
}
s=hstr_strdup(pb);
favorites->itemsView[favorites->countView++]=s;
hashset_add(favorites->set,s);
}
next:
pb=pe+1;
pe=strchr(pb, '\n');
}
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/include/hstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@

#include <getopt.h>
#include <locale.h>
#ifdef __APPLE__
#include <curses.h>
#elif defined(__FreeBSD__)
#include <ncurses.h>
#else
#include <ncursesw/curses.h>
#endif
#include <readline/chardefs.h>
#include <signal.h>
#include <termios.h>
Expand Down
2 changes: 2 additions & 0 deletions src/include/hstr_favorites.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

typedef struct {
char** items;
char** itemsView;
unsigned count;
unsigned countView;
bool loaded;
bool reorderOnChoice;
bool skipComments;
Expand Down