Skip to content

Instantly share code, notes, and snippets.

@PapaNappa
Last active August 8, 2016 11:10
Show Gist options
  • Save PapaNappa/b2b03fa7d32fff3242e95ad5a5a9c8c7 to your computer and use it in GitHub Desktop.
Save PapaNappa/b2b03fa7d32fff3242e95ad5a5a9c8c7 to your computer and use it in GitHub Desktop.
Run make in a seperate window
#!/bin/bash
# runs `make` in a separate window (using screen)
# I use this to compile out of vim,
# * without pausing vim while make executes
# * with the ability to easily scroll the output in a true terminal
# * with the ability to see coloured output
# in vim, run with
# :silent !code/make-session.sh <make options…>
# I prefer to map this to a key:
# :imap <F2> <ESC>:w<CR>:silent !make-session.sh<CR>
# :map <F2> :w<CR>:silent !make-session.sh<CR>
# Requires: screen, xdotool, wmctrl
# Replace your terminal emulator as needed ...
# The basic operation of this script is as follows:
# if no session is running then
# create new session
# run `make` in session
# We customise the session a bit, thus some shell code must be run inside the session during set up.
# In order to avoid a separate file (or have this code as a single string), execute this initialisation only when $MAKESESSION_SETUP is set.
# Thus, creating a session involves setting $MAKESSESION_SETUP and sourcing this file
if [ -n "$MAKESESSION_SETUP" ]; then
# run inside the session: initialise
# modify prompt to indicate we are an a session, and signal non-zero exit code
function makesession_prompt_command() {
local EXIT=$?
PS1="["
if [ $EXIT -ne 0 ]; then
PS1+="\e[31m"
else
PS1+="\e[32m"
fi
PS1+="make-session\e[0m \W]\$ "
}
PROMPT_COMMAND=makesession_prompt_command
# run make, and activate this window on error
function makesession_make() {
# give a visual hint where make starts
echo -e "\n\n\n\n\n\e[1;96m"\
" MAKE\n"\
"====================\e[0m\n\n"
# run make and save exit code (so that exit code is the exit-code of make)
make "$@"
local EXIT=$?
if [ $EXIT -eq 0 ]; then
# draw attention when make finished
wmctrl -i -r $WINDOWID -b add,demands_attention
else
# on error, draw more attention by activating the window
xdotool windowactivate $WINDOWID
fi
# return exit code of make
return $EXIT
}
else
# run from outside, thus initiate a new run of make
# ID of screen session
screen_session=make-session-${PWD//\//_}
# create screen session if necessary
if ! screen -list "$screen_session" >&/dev/null; then
xfce4-terminal -x screen -S "$screen_session" &
# wait for screen to initialise
while ! screen -list "$screen_session" >&/dev/null; do
sleep 0.1
done
# source this script inside the screen session to set up the make session
screen -S "$screen_session" -X stuff 'MAKESESSION_SETUP=1; source "'$0'"\n'
fi
# run make
screen -S "$screen_session" -X stuff 'makesession_make '"$@"'\n'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment