Removing hstr as param - it's static, resolves #291.

This commit is contained in:
Martin Dvorak 2018-08-13 10:14:50 +02:00
parent 64d39992a5
commit ae05fdae83

View file

@ -362,8 +362,7 @@ unsigned recalculate_max_history_items(void)
return hstr->promptItems; return hstr->promptItems;
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static void hstr_get_env_configuration()
void hstr_get_env_configuration(Hstr* hstr)
{ {
char *hstr_config=getenv(HH_ENV_VAR_CONFIG); char *hstr_config=getenv(HH_ENV_VAR_CONFIG);
if(hstr_config && strlen(hstr_config)>0) { if(hstr_config && strlen(hstr_config)>0) {
@ -849,8 +848,8 @@ void print_selection_row(char* text, int y, int width, char* pattern)
} }
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static // IMPROVE elide text
void hstr_print_highlighted_selection_row(char *text, int y, int width, Hstr *hstr) void hstr_print_highlighted_selection_row(char *text, int y, int width)
{ {
UNUSED_ARG(width); UNUSED_ARG(width);
@ -873,8 +872,7 @@ void hstr_print_highlighted_selection_row(char *text, int y, int width, Hstr *hs
color_attr_off(A_BOLD); color_attr_off(A_BOLD);
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static char* hstr_print_selection(unsigned maxHistoryItems, char* pattern)
char *hstr_print_selection(unsigned maxHistoryItems, char* pattern, Hstr* hstr)
{ {
char *result=NULL; char *result=NULL;
unsigned selectionCount=hstr_make_selection(pattern, hstr->history, maxHistoryItems); unsigned selectionCount=hstr_make_selection(pattern, hstr->history, maxHistoryItems);
@ -936,8 +934,7 @@ char *hstr_print_selection(unsigned maxHistoryItems, char* pattern, Hstr* hstr)
return result; return result;
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static void highlight_selection(int selectionCursorPosition, int previousSelectionCursorPosition, char* pattern)
void highlight_selection(int selectionCursorPosition, int previousSelectionCursorPosition, char* pattern, Hstr* hstr)
{ {
if(previousSelectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) { if(previousSelectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) {
// TODO make this function // TODO make this function
@ -976,11 +973,7 @@ void highlight_selection(int selectionCursorPosition, int previousSelectionCurso
text=selectionCursorPosition; text=selectionCursorPosition;
y=hstr->promptYItemsStart+selectionCursorPosition; y=hstr->promptYItemsStart+selectionCursorPosition;
} }
hstr_print_highlighted_selection_row( hstr_print_highlighted_selection_row(hstr->selection[text], y, getmaxx(stdscr));
hstr->selection[text],
y,
getmaxx(stdscr),
hstr);
} }
} }
@ -999,18 +992,16 @@ void signal_callback_handler_ctrl_c(int signum)
} }
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static int remove_from_history_model(char* almostDead)
// IMPROVE replace delete as it's reserved C++ word (IDEs)
int remove_from_history_model(char* delete, Hstr* hstr)
{ {
if(hstr->historyView==HH_VIEW_FAVORITES) { if(hstr->historyView==HH_VIEW_FAVORITES) {
return favorites_remove(hstr->favorites, delete); return favorites_remove(hstr->favorites, almostDead);
} else { } else {
// raw & ranked history is pruned first as its items point to system history lines // raw & ranked history is pruned first as its items point to system history lines
int systemOccurences=0, rawOccurences=history_mgmt_remove_from_raw(delete, hstr->history); int systemOccurences=0, rawOccurences=history_mgmt_remove_from_raw(almostDead, hstr->history);
history_mgmt_remove_from_ranked(delete, hstr->history); history_mgmt_remove_from_ranked(almostDead, hstr->history);
if(rawOccurences) { if(rawOccurences) {
systemOccurences=history_mgmt_remove_from_system_history(delete); systemOccurences=history_mgmt_remove_from_system_history(almostDead);
} }
if(systemOccurences!=rawOccurences && hstr->debugLevel>HH_DEBUG_LEVEL_NONE) { if(systemOccurences!=rawOccurences && hstr->debugLevel>HH_DEBUG_LEVEL_NONE) {
fprintf(stderr, "WARNING: system and raw items deletion mismatch %d / %d\n", systemOccurences, rawOccurences); fprintf(stderr, "WARNING: system and raw items deletion mismatch %d / %d\n", systemOccurences, rawOccurences);
@ -1019,15 +1010,14 @@ int remove_from_history_model(char* delete, Hstr* hstr)
} }
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static void hstr_next_view(void)
void hstr_next_view(Hstr* hstr)
{ {
hstr->historyView++; hstr->historyView++;
hstr->historyView=hstr->historyView%3; hstr->historyView=hstr->historyView%3;
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static void stdout_history_and_return(void)
void stdout_history_and_return(Hstr* hstr) { {
unsigned selectionCount=hstr_make_selection(hstr->cmdline, hstr->history, hstr->history->rawCount); unsigned selectionCount=hstr_make_selection(hstr->cmdline, hstr->history, hstr->history->rawCount);
if (selectionCount > 0) { if (selectionCount > 0) {
unsigned i; unsigned i;
@ -1047,8 +1037,7 @@ char* getResultFromSelection(int selectionCursorPosition, Hstr* hstr, char* resu
return result; return result;
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static void loop_to_select(void)
void loop_to_select(Hstr* hstr)
{ {
signal(SIGINT, signal_callback_handler_ctrl_c); signal(SIGINT, signal_callback_handler_ctrl_c);
@ -1064,7 +1053,7 @@ void loop_to_select(Hstr* hstr)
color_attr_on(COLOR_PAIR(HH_COLOR_NORMAL)); color_attr_on(COLOR_PAIR(HH_COLOR_NORMAL));
// TODO why do I print non-filtered selection when on command line there is a pattern? // TODO why do I print non-filtered selection when on command line there is a pattern?
hstr_print_selection(recalculate_max_history_items(), NULL, hstr); hstr_print_selection(recalculate_max_history_items(), NULL);
color_attr_off(COLOR_PAIR(HH_COLOR_NORMAL)); color_attr_off(COLOR_PAIR(HH_COLOR_NORMAL));
if(!hstr->promptBottom) { if(!hstr->promptBottom) {
print_help_label(); print_help_label();
@ -1078,7 +1067,7 @@ void loop_to_select(Hstr* hstr)
int width=getmaxx(stdscr); int width=getmaxx(stdscr);
int selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; int selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
int previousSelectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; int previousSelectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
char *result="", *msg, *delete; char *result="", *msg, *almostDead;
char pattern[SELECTION_PREFIX_MAX_LNG]; char pattern[SELECTION_PREFIX_MAX_LNG];
pattern[0]=0; pattern[0]=0;
// TODO this is too late! > don't render twice // TODO this is too late! > don't render twice
@ -1097,7 +1086,7 @@ void loop_to_select(Hstr* hstr)
color_attr_off(A_BOLD); color_attr_off(A_BOLD);
cursorX=getcurx(stdscr); cursorX=getcurx(stdscr);
cursorY=getcury(stdscr); cursorY=getcury(stdscr);
result=hstr_print_selection(maxHistoryItems, pattern, hstr); result=hstr_print_selection(maxHistoryItems, pattern);
move(cursorY, cursorX); move(cursorY, cursorX);
} }
skip=FALSE; skip=FALSE;
@ -1122,17 +1111,17 @@ void loop_to_select(Hstr* hstr)
break; break;
case KEY_DC: // DEL case KEY_DC: // DEL
if(selectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) { if(selectionCursorPosition!=SELECTION_CURSOR_IN_PROMPT) {
delete=getResultFromSelection(selectionCursorPosition, hstr, result); almostDead=getResultFromSelection(selectionCursorPosition, hstr, result);
msg=malloc(strlen(delete)+1); msg=malloc(strlen(almostDead)+1);
strcpy(msg,delete); strcpy(msg, almostDead);
if(!hstr->noConfirm) { if(!hstr->noConfirm) {
print_confirm_delete(msg); print_confirm_delete(msg);
cc = wgetch(stdscr); cc = wgetch(stdscr);
} }
if(hstr->noConfirm || cc == 'y') { if(hstr->noConfirm || cc == 'y') {
deletedOccurences=remove_from_history_model(msg, hstr); deletedOccurences=remove_from_history_model(msg);
result=hstr_print_selection(maxHistoryItems, pattern, hstr); result=hstr_print_selection(maxHistoryItems, pattern);
print_cmd_deleted_label(msg, deletedOccurences); print_cmd_deleted_label(msg, deletedOccurences);
} else { } else {
print_help_label(); print_help_label();
@ -1157,7 +1146,7 @@ void loop_to_select(Hstr* hstr)
selectionCursorPosition = hstr->selectionSize - 1; selectionCursorPosition = hstr->selectionSize - 1;
} }
} }
highlight_selection(selectionCursorPosition, SELECTION_CURSOR_IN_PROMPT, pattern, hstr); highlight_selection(selectionCursorPosition, SELECTION_CURSOR_IN_PROMPT, pattern);
move(hstr->promptY, basex+strlen(pattern)); move(hstr->promptY, basex+strlen(pattern));
} }
break; break;
@ -1165,7 +1154,7 @@ void loop_to_select(Hstr* hstr)
hstr->historyMatch++; hstr->historyMatch++;
hstr->historyMatch=hstr->historyMatch%HH_NUM_HISTORY_MATCH; hstr->historyMatch=hstr->historyMatch%HH_NUM_HISTORY_MATCH;
// TODO make this a function // TODO make this a function
result=hstr_print_selection(maxHistoryItems, pattern, hstr); result=hstr_print_selection(maxHistoryItems, pattern);
print_history_label(); print_history_label();
selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
if(strlen(pattern)<(width-basex-1)) { if(strlen(pattern)<(width-basex-1)) {
@ -1177,7 +1166,7 @@ void loop_to_select(Hstr* hstr)
case K_CTRL_T: case K_CTRL_T:
hstr->caseSensitive=!hstr->caseSensitive; hstr->caseSensitive=!hstr->caseSensitive;
hstr->regexp.caseSensitive=hstr->caseSensitive; hstr->regexp.caseSensitive=hstr->caseSensitive;
result=hstr_print_selection(maxHistoryItems, pattern, hstr); result=hstr_print_selection(maxHistoryItems, pattern);
print_history_label(); print_history_label();
selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
if(strlen(pattern)<(width-basex-1)) { if(strlen(pattern)<(width-basex-1)) {
@ -1191,8 +1180,8 @@ void loop_to_select(Hstr* hstr)
case K_CTRL_W: case K_CTRL_W:
#endif #endif
case K_CTRL_SLASH: case K_CTRL_SLASH:
hstr_next_view(hstr); hstr_next_view();
result=hstr_print_selection(maxHistoryItems, pattern, hstr); result=hstr_print_selection(maxHistoryItems, pattern);
print_history_label(); print_history_label();
// TODO function // TODO function
selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
@ -1210,7 +1199,7 @@ void loop_to_select(Hstr* hstr)
} else { } else {
favorites_add(hstr->favorites, result); favorites_add(hstr->favorites, result);
} }
hstr_print_selection(maxHistoryItems, pattern, hstr); hstr_print_selection(maxHistoryItems, pattern);
selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
if(hstr->historyView!=HH_VIEW_FAVORITES) { if(hstr->historyView!=HH_VIEW_FAVORITES) {
print_cmd_added_favorite_label(result); print_cmd_added_favorite_label(result);
@ -1227,7 +1216,7 @@ void loop_to_select(Hstr* hstr)
case KEY_RESIZE: case KEY_RESIZE:
print_history_label(); print_history_label();
maxHistoryItems=recalculate_max_history_items(); maxHistoryItems=recalculate_max_history_items();
result=hstr_print_selection(maxHistoryItems, pattern, hstr); result=hstr_print_selection(maxHistoryItems, pattern);
print_history_label(); print_history_label();
selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT; selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
move(hstr->promptY, basex+strlen(pattern)); move(hstr->promptY, basex+strlen(pattern));
@ -1260,7 +1249,7 @@ void loop_to_select(Hstr* hstr)
} else { } else {
hstr_make_selection(NULL, hstr->history, maxHistoryItems); hstr_make_selection(NULL, hstr->history, maxHistoryItems);
} }
result=hstr_print_selection(maxHistoryItems, pattern, hstr); result=hstr_print_selection(maxHistoryItems, pattern);
move(hstr->promptY, basex+hstr_strlen(pattern)); move(hstr->promptY, basex+hstr_strlen(pattern));
break; break;
@ -1285,7 +1274,7 @@ void loop_to_select(Hstr* hstr)
selectionCursorPosition=hstr->selectionSize-1; selectionCursorPosition=hstr->selectionSize-1;
} }
} }
highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern, hstr); highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern);
move(hstr->promptY, basex+strlen(pattern)); move(hstr->promptY, basex+strlen(pattern));
break; break;
case KEY_PPAGE: case KEY_PPAGE:
@ -1295,7 +1284,7 @@ void loop_to_select(Hstr* hstr)
} else { } else {
selectionCursorPosition=0; selectionCursorPosition=0;
} }
highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern, hstr); highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern);
move(hstr->promptY, basex+strlen(pattern)); move(hstr->promptY, basex+strlen(pattern));
break; break;
case KEY_DOWN: case KEY_DOWN:
@ -1324,7 +1313,7 @@ void loop_to_select(Hstr* hstr)
} }
} }
if(hstr->selectionSize) { if(hstr->selectionSize) {
highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern, hstr); highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern);
} }
move(hstr->promptY, basex+strlen(pattern)); move(hstr->promptY, basex+strlen(pattern));
break; break;
@ -1340,7 +1329,7 @@ void loop_to_select(Hstr* hstr)
} }
} }
if(hstr->selectionSize) { if(hstr->selectionSize) {
highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern, hstr); highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern);
} }
move(hstr->promptY, basex+strlen(pattern)); move(hstr->promptY, basex+strlen(pattern));
break; break;
@ -1412,7 +1401,7 @@ void loop_to_select(Hstr* hstr)
cursorY=getcury(stdscr); cursorY=getcury(stdscr);
} }
result = hstr_print_selection(maxHistoryItems, pattern, hstr); result = hstr_print_selection(maxHistoryItems, pattern);
move(cursorY, cursorX); move(cursorY, cursorX);
refresh(); refresh();
} }
@ -1435,9 +1424,8 @@ void loop_to_select(Hstr* hstr)
} }
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static
// TODO protection from line overflow (snprinf) // TODO protection from line overflow (snprinf)
void hstr_assemble_cmdline_pattern(int argc, char* argv[], Hstr* hstr, int startIndex) void hstr_assemble_cmdline_pattern(int argc, char* argv[], int startIndex)
{ {
if(argc>0) { if(argc>0) {
int i; int i;
@ -1457,25 +1445,23 @@ void hstr_assemble_cmdline_pattern(int argc, char* argv[], Hstr* hstr, int start
} }
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static
// TODO make favorites loading lazy (load on the first opening of favorites view) // TODO make favorites loading lazy (load on the first opening of favorites view)
void hstr_init_favorites(Hstr* hstr) void hstr_init_favorites(void)
{ {
hstr->favorites=malloc(sizeof(FavoriteItems)); hstr->favorites=malloc(sizeof(FavoriteItems));
favorites_init(hstr->favorites); favorites_init(hstr->favorites);
favorites_get(hstr->favorites); favorites_get(hstr->favorites);
} }
// IMPROVE hstr doesn't have to be passed as parameter - it's global static void hstr_interactive(void)
void hstr_interactive(Hstr* hstr)
{ {
hstr->history=get_prioritized_history(hstr->bigKeys, hstr->blacklist.set); hstr->history=get_prioritized_history(hstr->bigKeys, hstr->blacklist.set);
if(hstr->history) { if(hstr->history) {
history_mgmt_open(); history_mgmt_open();
if(hstr->interactive) { if(hstr->interactive) {
loop_to_select(hstr); loop_to_select();
} else { } else {
stdout_history_and_return(hstr); stdout_history_and_return();
} }
hstr_on_exit(); hstr_on_exit();
} else { } else {
@ -1483,7 +1469,7 @@ void hstr_interactive(Hstr* hstr)
} }
} }
void hstr_getopt(int argc, char **argv, Hstr *hstr) void hstr_getopt(int argc, char **argv)
{ {
int option_index = 0; int option_index = 0;
int option = getopt_long(argc, argv, "fkVhnszb", long_options, &option_index); int option = getopt_long(argc, argv, "fkVhnszb", long_options, &option_index);
@ -1531,11 +1517,10 @@ void hstr_getopt(int argc, char **argv, Hstr *hstr)
} }
if(optind < argc) { if(optind < argc) {
hstr_assemble_cmdline_pattern(argc, argv, hstr, optind); hstr_assemble_cmdline_pattern(argc, argv, optind);
} }
} }
// IMPROVE rename this method - is NOT c convention + clash w/ existing name
int hstr_main(int argc, char* argv[]) int hstr_main(int argc, char* argv[])
{ {
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@ -1543,11 +1528,11 @@ int hstr_main(int argc, char* argv[])
hstr=malloc(sizeof(Hstr)); hstr=malloc(sizeof(Hstr));
hstr_init(); hstr_init();
hstr_get_env_configuration(hstr); hstr_get_env_configuration();
hstr_getopt(argc, argv, hstr); hstr_getopt(argc, argv);
hstr_init_favorites(hstr); hstr_init_favorites();
blacklist_load(&hstr->blacklist); blacklist_load(&hstr->blacklist);
hstr_interactive(hstr); hstr_interactive();
favorites_destroy(hstr->favorites); favorites_destroy(hstr->favorites);
free(hstr); free(hstr);