mirror of
https://github.com/dvorka/hstr.git
synced 2025-02-24 23:08:36 +08:00
Merge pull request #268 from xaizek/several-improvements
Improvements: code improvements, bottom prompt fixes, last item deletion crash fix.
This commit is contained in:
commit
c9c992b6fb
4 changed files with 67 additions and 22 deletions
16
.gitignore
vendored
Normal file
16
.gitignore
vendored
Normal file
|
@ -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
src/hstr.c
29
src/hstr.c
|
@ -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)
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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:
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <readline/history.h>
|
||||
|
@ -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();
|
||||
|
@ -112,11 +131,6 @@ 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(®exp, histtimeformatTimestamp);
|
||||
|
||||
RankedHistoryItem *r;
|
||||
RadixItem *radixItem;
|
||||
HIST_ENTRY **historyList=history_list();
|
||||
|
@ -124,7 +138,7 @@ HistoryItems *get_prioritized_history(int optionBigKeys, HashSet *blacklist)
|
|||
int rawOffset=historyState->length-1, rawTimestamps=0;
|
||||
char *line;
|
||||
for(i=0; i<historyState->length; i++, rawOffset--) {
|
||||
if(!regexp_match(®exp, historyList[i]->line)) {
|
||||
if(is_hist_timestamp(historyList[i]->line)) {
|
||||
rawHistory[rawOffset]=0;
|
||||
rawTimestamps++;
|
||||
continue;
|
||||
|
@ -171,8 +185,6 @@ HistoryItems *get_prioritized_history(int optionBigKeys, HashSet *blacklist)
|
|||
}
|
||||
}
|
||||
|
||||
regfree(®exp);
|
||||
|
||||
DEBUG_RADIXSORT();
|
||||
|
||||
RadixItem **prioritizedRadix=radixsort_dump(&rs);
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue