Adding Bash shell script #478

This commit is contained in:
Martin Dvorak 2023-04-06 00:08:17 +02:00
parent 611eecbae8
commit 83e7d6ce1a
4 changed files with 78 additions and 33 deletions

View file

@ -137,7 +137,7 @@
// major.minor.revision
static const char* VERSION_STRING=
"hstr version \"3.0.0\" (2023-04-02T18:50:00)"
"hstr version \"3.0.0\" (2023-04-05T08:50:00)"
"\n";
static const char* HSTR_VIEW_LABELS[]={
@ -366,6 +366,7 @@ void print_bash_install_code(void)
"\n READLINE_LINE=\"$(hstr ${READLINE_LINE})\""
"\n READLINE_POINT=${#READLINE_LINE}"
"\n}"
#if defined(__MS_WSL__)
"\nif [[ $- =~ .*i.* ]]; then bind -x '\"\\C-r\": \"hstrwsl\"'; fi"

View file

@ -157,21 +157,21 @@ void tiocsti(void)
void fill_terminal_input(char* cmd, bool padding)
{
if(cmd && strlen(cmd)>0) {
#if defined(__MS_WSL__) || defined(__CYGWIN__)
fprintf(stderr, "%s", cmd);
if(padding) fprintf(stderr, "%s", "\n");
#else
size_t size = strlen(cmd);
unsigned i;
char *c;
for (i = 0; i < size; i++) {
// terminal I/O control, simulate terminal input
c=(cmd+i);
ioctl(0, TIOCSTI, c);
if(is_tiocsti) {
size_t size = strlen(cmd);
unsigned i;
char *c;
for (i = 0; i < size; i++) {
// terminal I/O control, simulate terminal input
c=(cmd+i);
ioctl(0, TIOCSTI, c);
}
// echo, but don't flush to terminal
if(padding) printf("\n");
} else {
fprintf(stderr, "%s", cmd);
if(padding) fprintf(stderr, "%s", "\n");
}
// echo, but don't flush to terminal
if(padding) printf("\n");
#endif
}
}

View file

@ -177,15 +177,22 @@ bind -x '"\C-r": "hstrdebug"'
#
# EXAMPLE: WORKING minimal production version w/ foo HSTR
function foohstr {
echo "CMD_BY_HSTR: >>>${@}<<<"
}
# HSTR configuration - add this to ~/.bashrc
alias hh=hstr # hh to be alias for hstr
export HSTR_CONFIG=hicolor # get more colors
shopt -s histappend # append new history items to .bash_history
export HISTCONTROL=ignorespace # leading space hides commands from history
export HISTFILESIZE=10000 # increase history file size (default is 500)
export HISTSIZE=${HISTFILESIZE} # increase history size (default is 500)
# ensure synchronization between bash memory and history file
export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)
function hstrnotiocsti {
READLINE_LINE="$(foohstr ${READLINE_LINE})"
{ HSTR_OUT="$( { </dev/tty hstr ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;
READLINE_LINE="${HSTR_OUT}"
READLINE_POINT=${#READLINE_LINE}
}
bind -x '"\C-r": "hstrnotiocsti"'
if [[ $- =~ .*i.* ]]; then bind -x '"\C-r": "hstrnotiocsti"'; fi
export HSTR_TIOCSTI=n
# eof

View file

@ -41,21 +41,58 @@ bindkey '\C-r' hstrnotiocsti
# ##################################################################
#
# EXAMPLE: WORKING minimal production version w/ foo HSTR
# PROBLEM!
# - active ZLE takes over the terminal, therefore HSTR cannot be run
#
# PROBLEM:
# - active ZLE takes over the terminal input & output streams
# - attempt to run HSTR using $(), ``, ... BLOCKS active ZLE progress
# - w/o ZLE it is not possible to insert text into the terminal
#
# SOLUTION:
# - HSTR input: can be enable by reading from </dev/tty
# - HSTR output: is sent to stderr (as stdout is occupied by Curses)
foohstr() {
echo "command-by-hstr-${1}"
hstr_notiocsti() {
zle -I
TMPFILE=$(mktemp)
</dev/tty hstr ${BUFFER} 2> ${TMPFILE}
BUFFER="$(cat ${TMPFILE})"
CURSOR=${#BUFFER}
zle redisplay
rm TMPFILE > /dev/null 2>&1
}
zle -N hstr_notiocsti
bindkey '\C-r' hstr_notiocsti
hstrnotiocsti() {
BUFFER="$(foohstr ${BUFFER})"
CURSOR=${#BUFFER}
zle redisplay
export HSTR_TIOCSTI=n
# ##################################################################
#
# EXAMPLE: WORKING minimal production version w/ foo HSTR
#
# PROBLEM:
# - active ZLE takes over the terminal input & output streams
# - attempt to run HSTR using $(), ``, ... BLOCKS active ZLE progress
# - w/o ZLE it is not possible to insert text into the terminal
#
# SOLUTION:
# - HSTR input: can be enable by reading from </dev/tty
# - HSTR output: we use CUSTOM file handle to get stderr output ONLY
# 1. 2>&1 ... redirect stderr to stdout
# 2. 1>&3 ... redirect stdout to custom file handle
# (thus ONLY stderr is sent to stdout)
# 3. 3>&- ... close custom file handle
# (thus close stdout i.e. stdout won't be sent anywhere)
# { ... } ... to execute in the current shell & set HSTR_OUT
# 3>&1 ... restore stdout to custom file handle
hstr_no_tiocsti() {
zle -I
{ HSTR_OUT="$( { </dev/tty hstr ${BUFFER}; } 2>&1 1>&3 3>&- )"; } 3>&1;
BUFFER="${HSTR_OUT}"
CURSOR=${#BUFFER}
zle redisplay
}
zle -N hstrnotiocsti
bindkey '\C-r' hstrnotiocsti
zle -N hstr_no_tiocsti
bindkey '\C-r' hstr_no_tiocsti
# eof