Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 123 lines (106 sloc) 3.025 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#!/bin/bash

set -o allexport
function prompt_command
{
  TERMWIDTH=${COLUMNS}

  hostnam=$(echo -n $HOSTNAME | sed -e "s/[\.].*//")

  usernam=$(whoami)
  cur_tty=$(tty | sed -e "s/.*tty\(.*\)/\1/")
  newPWD="${PWD}"
  gitBranch=$(git branch 2>/dev/null | grep '^\*' | cut -c3-)
  if [[ -z "$gitBranch" ]]; then
    gitBranch=" "
  else
    gitBranch="$gitBranch"
  fi
  let promptsize=$(echo -n "--(${usernam}@${hostnam})-[$gitBranch]-(${PWD})--" | wc -c | tr -d " ")
  let fillsize=${TERMWIDTH}-${promptsize}
  fill=""
  while [ "$fillsize" -gt "0" ]
  do
    fill="${fill}-"
    let fillsize=${fillsize}-1
  done

  if [ "$fillsize" -lt "0" ]
  then
    let cut=3-${fillsize}
    newPWD="...$(echo -n $PWD | sed -e "s/\(^.\{$cut\}\)\(.*\)/\2/")"
  fi
}

PROMPT_COMMAND=prompt_command

function setXtermTitle
{
  export XTERM_TITLE=
  if [ $TERM == xterm ]; then
    if [ -n "$PERFORCE_PROJECT" ]; then
      typeset proj="[$PERFORCE_PROJECT]"
    else
      typeset proj=
    fi
    typeset title="$proj"
    if [ $# != 0 ]; then
      title="$*${proj:+ $proj}"
    fi
    if [ -n "$title" ]; then
      title="$title: "
    fi
    typeset h=
    if [ "$HOSTNAME" != "dqw-linux" ]; then
      h="$HOSTNAME:"
    fi
    XTERM_TITLE="\[\033]0;$h${title}\w\007\]"
  else
    XTERM_TITLE=
  fi
  twtty
}

alias st=setXtermTitle

function twtty
{
  local GRAY="\[\033[1;30m\]"
  local LIGHT_GRAY="\[\033[0;37m\]"
  local WHITE="\[\033[1;37m\]"
  local NO_COLOUR="\[\033[0m\]"

  local LIGHT_BLUE="\[\033[1;34m\]"

  local RED="\[\033[0;31m\]"
  local BRIGHT_RED="\[\033[1;31m\]"

  local CYAN="\[\033[0;36m\]"
  local BRIGHT_CYAN="\[\033[1;36m\]"

  local YELLOW="\[\033[0;33m\]"
  local BRIGHT_YELLOW="\[\033[1;33m\]"

  local GREEN="\[\033[0;32m\]"
  local BRIGHT_GREEN="\[\033[1;32m\]"

  if [ "$(hostname)" == "lambda.local" ]; then
    ((SHLVL=SHLVL+1))
  fi
  if [ $SHLVL -lt 2 ]; then
    local BASIC_TEXT="$GREEN"
    local BRIGHT_TEXT="$BRIGHT_GREEN"
    local DELIM_TEXT="$LIGHT_BLUE"
    local BRANCH_TEXT="$BRIGHT_RED"
  elif [ $SHLVL -lt 3 ]; then
    local BASIC_TEXT="$YELLOW"
    local BRIGHT_TEXT="$BRIGHT_YELLOW"
    local DELIM_TEXT="$LIGHT_BLUE"
    local BRANCH_TEXT="$BRIGHT_GREEN"
  elif [ $SHLVL -lt 4 ]; then
    local BASIC_TEXT="$CYAN"
    local BRIGHT_TEXT="$BRIGHT_CYAN"
    local DELIM_TEXT="$LIGHT_BLUE"
    local BRANCH_TEXT="$BRIGHT_GREEN"
  else
    local BASIC_TEXT="$RED"
    local BRIGHT_TEXT="$BRIGHT_RED"
    local DELIM_TEXT="$LIGHT_BLUE"
    local BRANCH_TEXT="$YELLOW"
  fi
  if [ "$(whoami)" = "root" ]; then
    local USER_TEXT="\[\033[0;41m\]\[\033[1;37m\]"
  else
    local USER_TEXT=$BASIC_TEXT
  fi

    PS1="$XTERM_TITLE$BASIC_TEXT-$DELIM_TEXT-($USER_TEXT\$usernam$NO_COLOUR$DELIM_TEXT@$BASIC_TEXT\$hostnam${DELIM_TEXT})-${DELIM_TEXT}[${BRANCH_TEXT}\$gitBranch${DELIM_TEXT}]-($BRIGHT_TEXT\${newPWD}$DELIM_TEXT)-$BASIC_TEXT\${fill}$BASIC_TEXT-\n$BASIC_TEXT-$DELIM_TEXT->$NO_COLOUR "
    PS2="$DELIM_TEXT-$BASIC_TEXT-$BASIC_TEXT-$NO_COLOUR "
}

setXtermTitle
set +o allexport
Something went wrong with that request. Please try again.