Skip to content

Commit

Permalink
Merge pull request #268 from xaizek/several-improvements
Browse files Browse the repository at this point in the history
Improvements: code improvements, bottom prompt fixes, last item deletion crash fix.
  • Loading branch information
dvorka authored May 6, 2018
2 parents a9f3681 + 88fbedb commit c9c992b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/autom4te.cache/
/aclocal.m4
/compile
/config.guess
/config.log
/config.status
/config.sub
/configure
/depcomp
/install-sh
/missing
/src/hh
.deps/
Makefile
Makefile.in
*.o
29 changes: 22 additions & 7 deletions src/hstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,15 @@ void add_to_selection(Hstr *hstr, char *line, unsigned int *index)

void print_help_label()
{
int cursorX=getcurx(stdscr);
int cursorY=getcury(stdscr);

char screenLine[CMDLINE_LNG];
snprintf(screenLine, getmaxx(stdscr), "%s", LABEL_HELP);
mvprintw(hstr->promptYHelp, 0, "%s", screenLine); clrtoeol();
refresh();

move(cursorY, cursorX);
}

void print_confirm_delete(const char *cmd, Hstr *hstr)
Expand All @@ -492,7 +497,7 @@ void print_confirm_delete(const char *cmd, Hstr *hstr)
mvprintw(hstr->promptYHelp, 0, "%s", screenLine);
if(hstr->theme & HH_THEME_COLOR) {
color_attr_off(A_BOLD);
color_attr_on(COLOR_PAIR(1));
color_attr_on(COLOR_PAIR(HH_COLOR_NORMAL));
}
clrtoeol();
refresh();
Expand All @@ -510,7 +515,7 @@ void print_cmd_deleted_label(const char *cmd, int occurences, Hstr *hstr)
mvprintw(hstr->promptYHelp, 0, "%s", screenLine);
if(hstr->theme & HH_THEME_COLOR) {
color_attr_off(A_BOLD);
color_attr_on(COLOR_PAIR(1));
color_attr_on(COLOR_PAIR(HH_COLOR_NORMAL));
}
clrtoeol();
refresh();
Expand All @@ -527,7 +532,7 @@ void print_regexp_error(const char *errorMessage)
mvprintw(hstr->promptYHelp, 0, "%s", screenLine);
if(hstr->theme & HH_THEME_COLOR) {
color_attr_off(A_BOLD);
color_attr_on(COLOR_PAIR(1));
color_attr_on(COLOR_PAIR(HH_COLOR_NORMAL));
}
clrtoeol();
refresh();
Expand All @@ -544,7 +549,7 @@ void print_cmd_added_favorite_label(const char *cmd, Hstr *hstr)
mvprintw(hstr->promptYHelp, 0, screenLine);
if(hstr->theme & HH_THEME_COLOR) {
color_attr_off(A_BOLD);
color_attr_on(COLOR_PAIR(1));
color_attr_on(COLOR_PAIR(HH_COLOR_NORMAL));
}
clrtoeol();
refresh();
Expand Down Expand Up @@ -820,7 +825,7 @@ void hstr_print_highlighted_selection_row(char *text, int y, int width, Hstr *hs
{
color_attr_on(A_BOLD);
if(hstr->theme & HH_THEME_COLOR) {
color_attr_on(COLOR_PAIR(2));
color_attr_on(COLOR_PAIR(HH_COLOR_HIROW));
} else {
color_attr_on(A_REVERSE);
}
Expand All @@ -830,7 +835,7 @@ void hstr_print_highlighted_selection_row(char *text, int y, int width, Hstr *hs
getmaxx(stdscr)-2, getmaxx(stdscr)-2, text);
mvprintw(y, 0, "%s", screenLine);
if(hstr->theme & HH_THEME_COLOR) {
color_attr_on(COLOR_PAIR(1));
color_attr_on(COLOR_PAIR(HH_COLOR_NORMAL));
} else {
color_attr_off(A_REVERSE);
}
Expand Down Expand Up @@ -1065,6 +1070,10 @@ void loop_to_select(Hstr *hstr)
printDefaultLabel=FALSE;
}

if(c == K_CTRL_R) {
c = (hstr->promptBottom ? K_CTRL_P : K_CTRL_N);
}

switch (c) {
case KEY_HOME:
// avoids printing of wild chars in search prompt
Expand Down Expand Up @@ -1094,6 +1103,12 @@ void loop_to_select(Hstr *hstr)
printDefaultLabel=TRUE;
print_history_label();

if(hstr->selectionSize == 0) {
// just update the cursor, there are no elements to select
move(hstr->promptY, basex+strlen(pattern));
break;
}

if(hstr->promptBottom) {
if(selectionCursorPosition <= hstr->promptYItemsEnd-hstr->selectionSize+1) {
selectionCursorPosition=hstr->promptYItemsEnd-hstr->selectionSize+1;
Expand Down Expand Up @@ -1168,6 +1183,7 @@ void loop_to_select(Hstr *hstr)
break;
case KEY_RESIZE:
print_history_label();
maxHistoryItems=recalculate_max_history_items();
result=hstr_print_selection(maxHistoryItems, pattern, hstr);
print_history_label();
selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
Expand Down Expand Up @@ -1237,7 +1253,6 @@ void loop_to_select(Hstr *hstr)
highlight_selection(selectionCursorPosition, previousSelectionCursorPosition, pattern, hstr);
move(hstr->promptY, basex+strlen(pattern));
break;
case K_CTRL_R:
case KEY_DOWN:
case K_CTRL_J:
case K_CTRL_N:
Expand Down
8 changes: 8 additions & 0 deletions src/hstr_curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ void hstr_curses_start()
start_color();
use_default_colors();
}

#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
// Use ncurses specific function to make delay after pressing escape key
// unnoticeable. Can be zero, but in some corner cases multiple bytes
// composing a functional key code might be handled to the application with
// a delay, so small delay is safer.
set_escdelay(5);
#endif
}

bool terminal_has_colors() {
Expand Down
36 changes: 21 additions & 15 deletions src/hstr_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
limitations under the License.
*/

#include <ctype.h>
#include <stdio.h>
#include <limits.h>
#include <readline/history.h>
Expand Down Expand Up @@ -89,6 +90,24 @@ int get_item_offset(void)
}
}

bool is_hist_timestamp(const char *line)
{
// HISTTIMEFORMAT defined > ^#1234567890$

if(line[0] != '#') {
return false;
}

int i;
for(i = 1; line[i] != '\0'; ++i) {
if(!isdigit(line[i])) {
return false;
}
}

return (i >= 11);
}

HistoryItems *get_prioritized_history(int optionBigKeys, HashSet *blacklist)
{
using_history();
Expand All @@ -112,19 +131,14 @@ HistoryItems *get_prioritized_history(int optionBigKeys, HashSet *blacklist)
radixsort_init(&rs, (radixMaxKeyEstimate<100000?100000:radixMaxKeyEstimate));
rs.optionBigKeys=optionBigKeys;

regex_t regexp;
// HISTTIMEFORMAT defined > ^#1234567890$
const char *histtimeformatTimestamp = "^#[[:digit:]]\\{10,\\}$";
regexp_compile(&regexp, histtimeformatTimestamp);

RankedHistoryItem *r;
RadixItem *radixItem;
HIST_ENTRY **historyList=history_list();
char **rawHistory=malloc(sizeof(char*) * historyState->length);
int rawOffset=historyState->length-1, rawTimestamps=0;
char *line;
for(i=0; i<historyState->length; i++, rawOffset--) {
if(!regexp_match(&regexp, historyList[i]->line)) {
if(is_hist_timestamp(historyList[i]->line)) {
rawHistory[rawOffset]=0;
rawTimestamps++;
continue;
Expand Down Expand Up @@ -171,8 +185,6 @@ HistoryItems *get_prioritized_history(int optionBigKeys, HashSet *blacklist)
}
}

regfree(&regexp);

DEBUG_RADIXSORT();

RadixItem **prioritizedRadix=radixsort_dump(&rs);
Expand Down Expand Up @@ -224,13 +236,7 @@ int history_mgmt_remove_from_system_history(char *cmd)
char *l;
HISTORY_STATE *historyState=history_get_history_state();

// TODO refactor this code to have this in source exactly once
regex_t tsRegexp;
regex_t zshRegexp;
// HISTTIMEFORMAT defined > ^#1234567890$
const char *histtimeformatTimestamp = "^#[[:digit:]]\\{10,\\}$";
regexp_compile(&tsRegexp, histtimeformatTimestamp);

const char *zshTimestamp = "^: [[:digit:]]\\{10,\\}:[[:digit:]];.*";
regexp_compile(&zshRegexp, zshTimestamp);

Expand All @@ -246,7 +252,7 @@ int history_mgmt_remove_from_system_history(char *cmd)
if(offset>0) {
l=historyState->entries[offset-1]->line;
if(l && strlen(l)) {
if(!regexp_match(&tsRegexp, l)) {
if(is_hist_timestamp(l)) {
// TODO check that this delete doesn't cause mismatch of searched cmd to be deleted
free_history_entry(remove_history(offset-1));
}
Expand Down

0 comments on commit c9c992b

Please sign in to comment.