mirror of
https://github.com/dvorka/hstr.git
synced 2025-01-01 20:51:45 +08:00
Fixed #120 by the reliable Linux detection of Zsh.
This commit is contained in:
parent
dc5df0dec0
commit
c69ee85593
4 changed files with 45 additions and 7 deletions
19
src/hstr.c
19
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 '?':
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue