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

Add update_tmux to PROMPT_COMMAND only once #21

Open
wants to merge 1 commit 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
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ Git branch in Tmux
==================
[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/drmad/tmux-git)

`tmux-git` shows information about the `git` repo of the current directory in
`tmux-git` shows information about the `git` repo of the current directory in
`tmux` status bar, like current branch, *dirtiness*, stash, etc.

## Overview

There are many solutions to integrate the current Git branch on your Linux terminal
prompt (like [this one][1] and [this one][2], from where I borrowed most parts of
this script), but I don't like to bloat my prompt.
this script), but I don't like to bloat my prompt.

So I was thinking where can I put that information, and then I remembered the
fabulous [Tmux][3]. `tmux` is a powerful and really cool client-server terminal
multiplexer, but it also provides a nice status bar.
fabulous [Tmux][3]. `tmux` is a powerful and really cool client-server terminal
multiplexer, but it also provides a nice status bar.

This script scans the current `bash` directory for a `git` repo. If it's found,
This script scans the current `bash` directory for a `git` repo. If it's found,
then puts information about it in the status bar, like:

* Project name (actually, is the `git` repo directory name)
* Active branch
* 'Dirty' status (i.e., there are pending modifications to be committed)
* Active branch
* 'Dirty' status (i.e., there are pending modifications to be committed)
* Stashed changes

## Installation
Expand All @@ -29,8 +29,8 @@ Clone this project in your home directory, renaming the folder with a preceding
dot for hiding it:

git clone git://github.com/drmad/tmux-git.git ~/.tmux-git
Then, execute this line in a shell to add the script in the Bash initialization

Then, execute this line in a shell to add the script in the Bash initialization
file (usually `.bashrc`, replace if needed):

echo "if [[ \$TMUX ]]; then source ~/.tmux-git/tmux-git.sh; fi" >> ~/.bashrc
Expand All @@ -41,20 +41,20 @@ Run `tmux`, `cd` to a Git repo, and enjoy :)

brew install coreutils

## Configurarion
## Configuration

The configuration is stored in the file `~/.tmux-git.conf`, created at the first
run of the script with default values. Just edit it, and reload `tmux`.

### Variables and functions

* `TMUX_STATUS_LOCATION`: Position of the status on Tmux bar: `left` or `right`
* `TMUX_OUTREPO_STATUS`: Tmux status for when you're out of a repo. Set by
default to your pre-existing status line.
* `TMUX_OUTREPO_STATUS`: Tmux status for when you're out of a repo. Set by
default to your pre-existing status line.
* `TMUX_STATUS_DEFINITION()`: This function sets the `TMUX_STATUS` variable, which
is shown in the `tmux` bar. You can use any variable used across the script for
creating this variable.

## En español

He realizado un post en español de las instrucciones de instalación en mi blog:
Expand Down
32 changes: 17 additions & 15 deletions tmux-git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ if [ ! -f $CONFIG_FILE ]; then
# Location of the status on tmux bar: left or right
TMUX_STATUS_LOCATION='right'

# Status for where you are out of a repo. Default is your pre-existing status
# line.
# Status for where you are out of a repo. Default is your pre-existing status
# line.
# Kudos to https://github.com/danarnold for the idea.
TMUX_OUTREPO_STATUS=`tmux show -vg status-$TMUX_STATUS_LOCATION`

# Function to build the status line. You need to define the $TMUX_STATUS
# Function to build the status line. You need to define the $TMUX_STATUS
# variable.
TMUX_STATUS_DEFINITION() {
# You can use any tmux status variables, and $GIT_BRANCH, $GIT_DIRTY,
# You can use any tmux status variables, and $GIT_BRANCH, $GIT_DIRTY,
# $GIT_FLAGS ( which is an array of flags ), and pretty much any variable
# used in this script :-)

GIT_BRANCH="`basename "$GIT_REPO"` | branch: $GIT_BRANCH"

TMUX_STATUS="$GIT_BRANCH$GIT_DIRTY"

if [ ${#GIT_FLAGS[@]} -gt 0 ]; then
TMUX_STATUS="$TMUX_STATUS [`IFS=,; echo "${GIT_FLAGS[*]}"`]"
fi
Expand Down Expand Up @@ -96,7 +96,7 @@ find_git_stash() {

update_tmux() {

# The trailing slash is for avoiding conflicts with repos with
# The trailing slash is for avoiding conflicts with repos with
# similar names. Kudos to https://github.com/tillt for the bug report
CWD=`$READLINK -e "$(pwd)"`/

Expand All @@ -111,20 +111,20 @@ update_tmux() {
find_git_dirty

GIT_FLAGS=($GIT_STASH)

TMUX_STATUS_DEFINITION
if [ "$GIT_DIRTY" ]; then

if [ "$GIT_DIRTY" ]; then
tmux set-window-option status-$TMUX_STATUS_LOCATION-style bright > /dev/null
else
tmux set-window-option status-$TMUX_STATUS_LOCATION-style none > /dev/null
fi
tmux set-window-option status-$TMUX_STATUS_LOCATION "$TMUX_STATUS" > /dev/null

tmux set-window-option status-$TMUX_STATUS_LOCATION "$TMUX_STATUS" > /dev/null

else
find_git_repo

if [[ $GIT_REPO ]]; then
export TMUX_GIT_LASTREPO="$GIT_REPO"
update_tmux
Expand All @@ -149,6 +149,8 @@ case $SHELL in
fi
;;
*)
PROMPT_COMMAND="update_tmux; $PROMPT_COMMAND"
if [[ $PROMPT_COMMAND != *update_tmux* ]]; then
PROMPT_COMMAND="update_tmux; $PROMPT_COMMAND"
fi
;;
esac