Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detect light terminal on initialization. Implements #150. #151

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mcfly.bash
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function mcfly_prompt_command {
# Set $PROMPT_COMMAND run mcfly_prompt_command and then any existing $PROMPT_COMMAND.
PROMPT_COMMAND="mcfly_prompt_command;$PROMPT_COMMAND"

# If this is an interactive shell, take ownership of ctrl-r.
# If this is an interactive shell, take ownership of ctrl-r and set terminal color.
if [[ $- =~ .*i.* ]]; then
if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
# shellcheck disable=SC2016
Expand All @@ -78,4 +78,10 @@ if [[ $- =~ .*i.* ]]; then
bind "'\C-r': '\C-amcfly: \e# mcfly search\C-m'"
fi
fi

if [[ "$COLORFGBG" =~ \;15$ ]]; then
export MCFLY_LIGHT=TRUE
elif [[ "$TERM_PROGRAM" == "Apple_Terminal" && $(defaults read -g AppleInterfaceStyle 2> /dev/null) != 'Dark' ]]; then
export MCFLY_LIGHT=TRUE
fi
fi
22 changes: 17 additions & 5 deletions mcfly.fish
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function __mcfly_add_command -d 'Add run commands to McFly database' -e fish_pos
eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]'
end

# If this is an interactive shell, set up key binding functions.
# If this is an interactive shell, set up key binding functions and terminal color.
if status is-interactive
function __mcfly-history-widget -d "Search command history with McFly"
set -l mcfly_output (mktemp -t mcfly.output.XXXXXXXX)
Expand All @@ -67,11 +67,23 @@ if status is-interactive
end

function mcfly_key_bindings -d "Default key bindings for McFly"
bind \cr __mcfly-history-widget
if bind -M insert >/dev/null 2>&1
bind -M insert \cr __mcfly-history-widget
end
bind \cr __mcfly-history-widget
if bind -M insert >/dev/null 2>&1
bind -M insert \cr __mcfly-history-widget
end
end

function mcfly_detect_terminal_color -d 'Attempt to determine if running in light mode'
if string match -r '\;15$' $COLORFGBG &> /dev/null
set -gx MCFLY_LIGHT TRUE
else if test "$TERM_PROGRAM" = "Apple_Terminal"
set apple_interface (defaults read -g AppleInterfaceStyle 2> /dev/null)
if [ "$apple_interface" != 'Dark' ]
set -gx MCFLY_LIGHT TRUE
end
end
end

mcfly_key_bindings
mcfly_detect_terminal_color
end
8 changes: 7 additions & 1 deletion mcfly.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ exit_logger() {
}
zshexit_functions+=(exit_logger)

# If this is an interactive shell, take ownership of ctrl-r.
# If this is an interactive shell, take ownership of ctrl-r and set terminal color.
if [[ $- =~ .*i.* ]]; then
mcfly-history-widget() {
() {
Expand Down Expand Up @@ -90,4 +90,10 @@ if [[ $- =~ .*i.* ]]; then
}
zle -N mcfly-history-widget
bindkey '^R' mcfly-history-widget

if [[ "$COLORFGBG" =~ \;15$ ]]; then
export MCFLY_LIGHT=TRUE
elif [[ "$TERM_PROGRAM" == "Apple_Terminal" && $(defaults read -g AppleInterfaceStyle 2> /dev/null) != 'Dark' ]]; then
export MCFLY_LIGHT=TRUE
fi
fi