From e3938d53c79d25886bb2718a5206e85accb42d13 Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sat, 28 Feb 2015 08:45:46 +0100 Subject: [PATCH] Fixing #107 by making big keys handling in radix sort an option. --- man/hh.1 | 22 +++++++++++++++++----- src/hstr.c | 18 ++++++++++++++++-- src/hstr_history.c | 4 ++-- src/include/hstr_history.h | 2 +- src/include/radixsort.h | 8 ++++++-- src/radixsort.c | 7 +++---- 6 files changed, 45 insertions(+), 16 deletions(-) diff --git a/man/hh.1 b/man/hh.1 index 6d4fccd..ec1917c 100644 --- a/man/hh.1 +++ b/man/hh.1 @@ -41,7 +41,7 @@ Toggle regular expression and substring search. Toggle case sensitive search. .TP \fBCtrl\-/\fR, \fBCtrl\-7\fR -Rotate view of history as provided by BASH, ranked history ordered by the number of occurences/length/timestamp and favorites. +Rotate view of history as provided by Bash, ranked history ordered by the number of occurences/length/timestamp and favorites. .TP \fBCtrl\-f\fR Add currently selected command to favorites. @@ -56,7 +56,7 @@ Navigate in the history list. Choose currently selected item for completion and let user to edit it on the command prompt. .TP \fBLEFT\fR arrow -Choose currently selected item for completion and let user to edit it editor (fix command). +Choose currently selected item for completion and let user to edit it in editor (fix command). .TP \fBENTER\fR Choose currently selected item for completion and execute it. @@ -85,7 +85,7 @@ Configuration options: Get more colors with this option (default is monochromatic). \fImonochromatic\fR - Ensure black and white view with this option. + Ensure black and white view. \fIregexp\fR Filter command history using regular expressions (substring match is default) @@ -105,6 +105,15 @@ Configuration options: \fIfavorites\fR Show favorites as a default view (metric-based view is shown otherwise). +\fIbigkeysskip\fR + Skip big history entries i.e. very long lines (default). + +\fIbigkeysfloor\fR + Use different sorting slot for big keys when building metrics-based view (big keys are skipped by default). + +\fIbigkeysexit\fR + Exit (fail) on presence of a big key in history (big keys are skipped by default). + \fIwarning\fR Show warning. @@ -151,11 +160,14 @@ bindkey -s "\eC\-r" "\eeqhh\en" # bind hh to Ctrl-r (for Vi mode check doc) .fi .SH EXAMPLES .TP +\fBhh git\fR + Start `hh` and show only history items containing 'git'. +.TP \fBhh --non-interactive git\fR -Print history items containing 'git' to standard output and exit. + Print history items containing 'git' to standard output and exit. .TP \fBhh --show-configuration >> ~/.bashrc\fR -Append default \fBhh\fR configuration to your Bash profile. + Append default \fBhh\fR configuration to your Bash profile. .SH AUTHOR Written by Martin Dvorak .SH BUGS diff --git a/src/hstr.c b/src/hstr.c index bf15069..6f1e864 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -93,6 +93,9 @@ #define HH_CONFIG_FAVORITES "favorites" #define HH_CONFIG_DEBUG "debug" #define HH_CONFIG_WARN "warning" +#define HH_CONFIG_BIG_KEYS_SKIP "bigkeysskip" +#define HH_CONFIG_BIG_KEYS_FLOOR "bigkeysfloor" +#define HH_CONFIG_BIG_KEYS_EXIT "bigkeysexit" #define HH_DEBUG_LEVEL_NONE 0 #define HH_DEBUG_LEVEL_WARN 1 @@ -239,6 +242,7 @@ typedef struct { bool interactive; bool hicolor; + int bigKeys; int debugLevel; HstrRegexp regexp; @@ -261,8 +265,9 @@ void hstr_init(Hstr *hstr) hstr->interactive=true; hstr->hicolor=FALSE; - + hstr->bigKeys=RADIX_BIG_KEYS_SKIP; hstr->debugLevel=HH_DEBUG_LEVEL_NONE; + hstr->cmdline[0]=0; hstr_regexp_init(&hstr->regexp); } @@ -295,6 +300,15 @@ void hstr_get_env_configuration(Hstr *hstr) hstr->historyView=HH_VIEW_FAVORITES; } } + if(strstr(hstr_config,HH_CONFIG_BIG_KEYS_EXIT)) { + hstr->bigKeys=RADIX_BIG_KEYS_EXIT; + } else { + if(strstr(hstr_config,HH_CONFIG_BIG_KEYS_FLOOR)) { + hstr->bigKeys=RADIX_BIG_KEYS_FLOOR; + } else { + hstr->bigKeys=RADIX_BIG_KEYS_SKIP; + } + } if(strstr(hstr_config,HH_CONFIG_DEBUG)) { hstr->debugLevel=HH_DEBUG_LEVEL_DEBUG; @@ -1103,7 +1117,7 @@ void hstr_init_favorites(Hstr *hstr) void hstr_main(Hstr *hstr) { - hstr->history=get_prioritized_history(); + hstr->history=get_prioritized_history(hstr->bigKeys); if(hstr->history) { history_mgmt_open(); if(hstr->interactive) { diff --git a/src/hstr_history.c b/src/hstr_history.c index acae663..c24be88 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -86,7 +86,7 @@ int get_item_offset() } } -HistoryItems *get_prioritized_history() +HistoryItems *get_prioritized_history(int optionBigKeys) { using_history(); @@ -114,7 +114,7 @@ HistoryItems *get_prioritized_history() RadixSorter rs; unsigned radixMaxKeyEstimate=historyState->size*1000; radixsort_init(&rs, (radixMaxKeyEstimate<100000?100000:radixMaxKeyEstimate)); - rs.optFloorAndInsertBigKeys=true; + rs.optionBigKeys=optionBigKeys; regex_t regexp; // HISTTIMEFORMAT defined > ^#1234567890$ diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index a2e2528..8b99074 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -40,7 +40,7 @@ typedef struct { unsigned rawCount; } HistoryItems; -HistoryItems *get_prioritized_history(); +HistoryItems *get_prioritized_history(int optionBigKeys); HistoryItems *get_history_items(); void free_history_items(); diff --git a/src/include/radixsort.h b/src/include/radixsort.h index cbb5cfc..0742983 100644 --- a/src/include/radixsort.h +++ b/src/include/radixsort.h @@ -10,6 +10,7 @@ #ifndef RADIXSORT_H_ #define RADIXSORT_H_ +#include #include #include #include @@ -19,6 +20,10 @@ #define RADIX_SLOT_SIZE 1000 +#define RADIX_BIG_KEYS_SKIP 0 +#define RADIX_BIG_KEYS_FLOOR 1 +#define RADIX_BIG_KEYS_EXIT 2 + #define RADIX_DEBUG_LEVEL_NONE 0 #define RADIX_DEBUG_LEVEL_WARN 1 #define RADIX_DEBUG_LEVEL_DEBUG 2 @@ -41,8 +46,7 @@ typedef struct { unsigned keyLimit; RadixItem ***topDigits; - bool optFloorAndInsertBigKeys; - bool optIgnoreBigKeys; + int optionBigKeys; RadixSlot **_slotDescriptors; unsigned _slotsCount; diff --git a/src/radixsort.c b/src/radixsort.c index 6ab0d4e..fd91338 100644 --- a/src/radixsort.c +++ b/src/radixsort.c @@ -14,8 +14,7 @@ void radixsort_init(RadixSorter *rs, unsigned keyLimit) { - rs->optFloorAndInsertBigKeys=false; - rs->optIgnoreBigKeys=false; + rs->optionBigKeys=RADIX_BIG_KEYS_SKIP; rs->_topIndexLimit=GET_TOP_INDEX(keyLimit); rs->size=0; @@ -54,10 +53,10 @@ void radixsort_add(RadixSorter *rs, RadixItem *item) if(rs->_debug > RADIX_DEBUG_LEVEL_NONE) { fprintf(stderr, "WARNING: Radix sort overflow - inserted key is bigger than limit (%u): %u\n", rs->keyLimit, item->key); } - if(rs->optFloorAndInsertBigKeys) { + if(rs->optionBigKeys==RADIX_BIG_KEYS_FLOOR) { item->key = rs->keyLimit-1; } else { - if(rs->optIgnoreBigKeys) { + if(rs->optionBigKeys==RADIX_BIG_KEYS_SKIP) { return; } else { exit(0);