diff --git a/src/hstr.c b/src/hstr.c index ce3336d..ecc59ee 100644 --- a/src/hstr.c +++ b/src/hstr.c @@ -76,8 +76,8 @@ #define HH_COLOR_DELETE 4 #define HH_COLOR_MATCH 5 -#define HH_ENV_VAR_CONFIG "HH_CONFIG" -#define HH_ENV_VAR_PROMPT "HH_PROMPT" +#define HH_ENV_VAR_CONFIG "HH_CONFIG" +#define HH_ENV_VAR_PROMPT "HH_PROMPT" #define HH_CONFIG_MONO "monochromatic" #define HH_CONFIG_HICOLOR "hicolor" @@ -156,7 +156,7 @@ static const char *HH_CASE_LABELS[]={ "sensitive" }; -static const char *INSTALL_STRING= +static const char *INSTALL_BASH_STRING= "\n# add this configuration to ~/.bashrc" "\nexport HH_CONFIG=hicolor # get more colors" "\nshopt -s histappend # append new history items to .bash_history" @@ -168,6 +168,12 @@ static const char *INSTALL_STRING= "\nif [[ $- =~ .*i.* ]]; then bind '\"\\C-r\": \"\\C-a hh \\C-j\"'; fi" "\n\n"; +static const char *INSTALL_ZSH_STRING= + "\n# add this configuration to ~/.zshrc" + "\nexport HISTFILE=~/.zsh_history # ensure history file visibility" + "\nexport HH_CONFIG=hicolor # get more colors" + "\n\n"; + static const char *HELP_STRING= "Usage: hh [option] [arg1] [arg2]..." "\nShell history suggest box:" @@ -1117,7 +1123,12 @@ void hstr_getopt(int argc, char **argv, Hstr *hstr) printf("%s", HELP_STRING); exit(EXIT_SUCCESS); case 's': - printf("%s", INSTALL_STRING); + // ZSH_VERSION is not exported by Zsh > detected by parent process + if(isZshParentShell()) { + printf("%s", INSTALL_ZSH_STRING); + } else { + printf("%s", INSTALL_BASH_STRING); + } exit(EXIT_SUCCESS); case '?': diff --git a/src/hstr_history.c b/src/hstr_history.c index f54d245..47ad8d7 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -68,9 +68,9 @@ void dump_prioritized_history(HistoryItems *ph) printf("\n"); fflush(stdout); } -int get_item_offset(char *historyFileName) +int get_item_offset() { - if(strstr(historyFileName,"zsh")) { + if(isZshParentShell()) { // In zsh history file, the format of item is // [:][blank][unix_timestamp][:][0][;][cmd] // Such as: @@ -94,7 +94,7 @@ HistoryItems *get_prioritized_history() } HISTORY_STATE *historyState=history_get_history_state(); - int itemOffset = get_item_offset(historyFile); + int itemOffset = get_item_offset(); if(historyState->length > 0) { HashSet rankmap; diff --git a/src/hstr_utils.c b/src/hstr_utils.c index cd02c4e..4c10f00 100644 --- a/src/hstr_utils.c +++ b/src/hstr_utils.c @@ -125,3 +125,29 @@ void toggle_case(char *str, bool lowercase) { } } } + +const char* get_process_name_by_pid(const int pid) +{ + char* name = (char*)calloc(256,sizeof(char)); + if(name){ + sprintf(name, "/proc/%d/cmdline",pid); + FILE* f = fopen(name,"r"); + if(f){ + size_t size = fread(name, sizeof(char), 256, f); + if(size>0){ + if('\n'==name[size-1]) + name[size-1]='\0'; + } + fclose(f); + } + } + return name; +} + +bool isZshParentShell() { + pid_t parentPid=getppid(); + const char* cmdline=get_process_name_by_pid(parentPid); + bool result=cmdline && strstr(cmdline, "zsh"); + free(cmdline); + return result; +} diff --git a/src/include/hstr_utils.h b/src/include/hstr_utils.h index 6315319..d2f3ce0 100644 --- a/src/include/hstr_utils.h +++ b/src/include/hstr_utils.h @@ -28,5 +28,6 @@ void fill_terminal_input(char* cmd, bool padding); void reverse_char_pointer_array(char **array, unsigned length); void get_hostname(int bufferSize, char *buffer); void toggle_case(char *str, bool lowercase); +bool isZshParentShell(); #endif