mirror of
https://github.com/dvorka/hstr.git
synced 2025-01-14 10:57:41 +08:00
Added CLI parameter to delete the last command from the history and thus fixed #251.
This commit is contained in:
parent
71e20a17b8
commit
069ad24822
4 changed files with 46 additions and 13 deletions
3
man/hh.1
3
man/hh.1
|
@ -23,6 +23,9 @@ Show help
|
|||
\fB-n --non-interactive\fR
|
||||
Print filtered history on standard output and exit
|
||||
.TP
|
||||
\fB-k --kill-last-command\fR
|
||||
Delete the last command from history and exit
|
||||
.TP
|
||||
\fB-f --favorites\fR
|
||||
Show favorites view immediately
|
||||
.TP
|
||||
|
|
10
src/hstr.c
10
src/hstr.c
|
@ -234,6 +234,7 @@ static const char *HELP_STRING=
|
|||
"\nShell history suggest box:"
|
||||
"\n"
|
||||
"\n --favorites -f ... show favorites view"
|
||||
"\n --kill-last-command -k ... delete last command in history"
|
||||
"\n --non-interactive -n ... print filtered history and exit"
|
||||
"\n --show-configuration -s ... show configuration to be added to ~/.bashrc"
|
||||
"\n --show-zsh-configuration -z ... show Zsh configuration to be added to ~/.zshrc"
|
||||
|
@ -259,6 +260,7 @@ static const char *LABEL_HELP=
|
|||
|
||||
static const struct option long_options[] = {
|
||||
{"favorites", GETOPT_NO_ARGUMENT, NULL, 'f'},
|
||||
{"kill-last-command", GETOPT_NO_ARGUMENT, NULL, 'k'},
|
||||
{"version", GETOPT_NO_ARGUMENT, NULL, 'V'},
|
||||
{"help", GETOPT_NO_ARGUMENT, NULL, 'h'},
|
||||
{"non-interactive", GETOPT_NO_ARGUMENT, NULL, 'n'},
|
||||
|
@ -1417,7 +1419,7 @@ void hstr_main(Hstr *hstr)
|
|||
void hstr_getopt(int argc, char **argv, Hstr *hstr)
|
||||
{
|
||||
int option_index = 0;
|
||||
int option = getopt_long(argc, argv, "fVhnszb", long_options, &option_index);
|
||||
int option = getopt_long(argc, argv, "fkVhnszb", long_options, &option_index);
|
||||
if(option != -1) {
|
||||
switch(option) {
|
||||
case 'f':
|
||||
|
@ -1426,6 +1428,12 @@ void hstr_getopt(int argc, char **argv, Hstr *hstr)
|
|||
case 'n':
|
||||
hstr->interactive=false;
|
||||
break;
|
||||
case 'k':
|
||||
if(history_mgmt_remove_last_history_entry()) {
|
||||
exit(EXIT_SUCCESS);
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
case 'b':
|
||||
blacklist_load(&hstr->blacklist);
|
||||
blacklist_dump(&hstr->blacklist);
|
||||
|
|
|
@ -49,7 +49,7 @@ unsigned history_ranking_function(unsigned rank, int newOccurenceOrder, size_t l
|
|||
return metrics;
|
||||
}
|
||||
|
||||
char *get_history_file_name()
|
||||
char *get_history_file_name(void)
|
||||
{
|
||||
char *historyFile=getenv(ENV_VAR_HISTFILE);
|
||||
if(!historyFile || strlen(historyFile)==0) {
|
||||
|
@ -74,7 +74,7 @@ void dump_prioritized_history(HistoryItems *historyItems)
|
|||
printf("\n"); fflush(stdout);
|
||||
}
|
||||
|
||||
int get_item_offset()
|
||||
int get_item_offset(void)
|
||||
{
|
||||
if(isZshParentShell()) {
|
||||
// In zsh history file, the format of item is
|
||||
|
@ -202,18 +202,18 @@ HistoryItems *get_prioritized_history(int optionBigKeys, HashSet *blacklist)
|
|||
}
|
||||
}
|
||||
|
||||
void free_prioritized_history()
|
||||
void free_prioritized_history(void)
|
||||
{
|
||||
free(prioritizedHistory->items);
|
||||
free(prioritizedHistory);
|
||||
}
|
||||
|
||||
void history_mgmt_open()
|
||||
void history_mgmt_open(void)
|
||||
{
|
||||
dirty=false;
|
||||
}
|
||||
|
||||
void history_clear_dirty()
|
||||
void history_clear_dirty(void)
|
||||
{
|
||||
dirty=false;
|
||||
}
|
||||
|
@ -263,6 +263,27 @@ int history_mgmt_remove_from_system_history(char *cmd)
|
|||
return occurences;
|
||||
}
|
||||
|
||||
bool history_mgmt_remove_last_history_entry()
|
||||
{
|
||||
using_history();
|
||||
|
||||
char *historyFile = get_history_file_name();
|
||||
if(read_history(historyFile)!=0) {
|
||||
fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
HISTORY_STATE *historyState=history_get_history_state();
|
||||
// delete the last command + the command that was used to run HSTR
|
||||
if(historyState->length > 1) {
|
||||
// length is NOT updated on history entry removal
|
||||
free_history_entry(remove_history(historyState->length-1));
|
||||
free_history_entry(remove_history(historyState->length-2));
|
||||
write_history(get_history_file_name());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int history_mgmt_remove_from_raw(char *cmd, HistoryItems *history) {
|
||||
int occurences=history->rawCount;
|
||||
if(history->rawCount) {
|
||||
|
@ -291,7 +312,7 @@ int history_mgmt_remove_from_ranked(char *cmd, HistoryItems *history) {
|
|||
return occurences-history->count;
|
||||
}
|
||||
|
||||
void history_mgmt_flush()
|
||||
void history_mgmt_flush(void)
|
||||
{
|
||||
if(dirty) {
|
||||
fill_terminal_input("history -r\n", false);
|
||||
|
|
|
@ -51,17 +51,18 @@ typedef struct {
|
|||
|
||||
HistoryItems *get_prioritized_history(int optionBigKeys, HashSet *blacklist);
|
||||
|
||||
HistoryItems *get_history_items();
|
||||
void free_history_items();
|
||||
HistoryItems *get_history_items(void);
|
||||
void free_history_items(void);
|
||||
|
||||
HistoryItems *prioritize_history(HistoryItems *historyFileItems);
|
||||
void free_prioritized_history();
|
||||
void free_prioritized_history(void);
|
||||
|
||||
void history_mgmt_open();
|
||||
void history_clear_dirty();
|
||||
void history_mgmt_open(void);
|
||||
void history_clear_dirty(void);
|
||||
int history_mgmt_remove_from_system_history(char *cmd);
|
||||
bool history_mgmt_remove_last_history_entry();
|
||||
int history_mgmt_remove_from_raw(char *cmd, HistoryItems *history);
|
||||
int history_mgmt_remove_from_ranked(char *cmd, HistoryItems *history);
|
||||
void history_mgmt_flush();
|
||||
void history_mgmt_flush(void);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue