Show Git branch & untracked/modified files in terminal

Show Git branch & untracked/modified files in terminal

Bash Git Resources

Many people know that you can add the Git branch name you are currently working in to the end of your command prompt but did you know that you can also have the command prompt notify you if you have any untracked of modified files?

Screen clip of the final result

Clip of the final result showing the Git branch name and one or more untracked files represented by the “U” after the branch name

I have created this post with linux in mind but I’m sure you can Google what to do if you have a different OS

First place the following in ~/.bashrc so that .bashrc can read my script.

# Include my very own git parse type function 🙂
source ~/.GitParseStatus.sh

Then I update the PS1 variable (the command prompt text) to output the result of the script

export PS1=”\n${debian_chroot:+($debian_chroot)}${RED}\u@\H ${YELLOW}\A ${BLUE}\w \$(gitParseStatus)${NORMAL}\n$ ”

It’s the \$(gitParseStatus) bit that works the magic we want.

and then of course I have the script which I save as ~/.GitParseStatus.sh (don’t forget to make the script executable with

chmod +x ~/.GitParseStatus.sh

#!/bin/bash
#function get_out() {
# log $1
# return
#}
#
#function log() {
# echo '* '$1
#}
#
#
#if [ $1 = "--debug" ]; then
# DEBUG=true
# echo
# echo "==========="
# echo "Debug is on"
# echo "==========="
# echo
#fi
#
#=========================
# Set Variables
#=========================

# What do you want to show when everything is up to date
# Your options are…
TICK=$’\u2714′
OK=’OK’
CUSTOM_OK=”

# Now set the all ok message
ALL_OK=$TICK

# What do you want to display when there git needs updating in some way?
# I have used “M” for files have been modified
UPDATE_TEXT=’ M’

# What do you want to display when there are untracked files in the repo?
# I have used “U” but you can use whatever you want
UNTRACKED_TEXT=” U”

# How do you want to display the repo branch
# I have used square brackets
BRANCH_PRE_TEXT=”[”
BRANCH_POST_TEXT=”]”

# What colour do you want the git section of you command line to be when
# everything is ok?
# I have it as green
# Set some colours #
# Some examples are…
GIT_RESET=”17″
GIT_NORMAL=”33[0m”
GIT_RED_1=”33[31;1m”
GIT_YELLOW_1=”33[33;1m”
GIT_WHITE=”33[37;1m”
GIT_RED=”33[0;31m”
GIT_YELLOW=”33[01;33m”
GIT_GREEN=”33[0;32m”
GIT_BLUE=”33[01;034m”
BRANCH_COLOUR_ALL_OK=$GIT_GREEN
BRANCH_COLOUR_NOT_OK=$GIT_GREEN

TEXT_CODES_COLOUR_ALL_OK=$GIT_GREEN
TEXT_CODES_COLOUR_NOT_OK=$GIT_RED

function gitParseStatus {

# If we fail to get a branch name then simply return empty handed 🙁
#IN=$(git symbolic-ref HEAD 2> /dev/null) || echo “You are not in a Git repo”
IN=$(git symbolic-ref HEAD 2> /dev/null) || return
#echo $IN

BRANCH_NAME=`echo $IN | sed ‘s/refs\/heads\///’`
#log ‘Branch Name=”‘$BRANCH_NAME'”‘

# get a list of files to run through
FILES=$(git status -z 2> /dev/null)
#log ‘Git Status=”‘”$FILES”‘”‘

#echo
if [[ -z “$FILES” ]]; then

#log “All the files in your Git repo are up to date”
UPDATES=`echo -e ${TEXT_CODES_COLOUR_ALL_OK}`${ALL_OK}

BRANCH=`echo -e BRANCH_COLOUR_ALL_OK}`”$BRANCH_PRE_TEXT””$BRANCH_NAME””$BRANCH_POST_TEXT”

OUTPUT=”$BRANCH”

else

#log “You have discrepancies in your Git Repo”
BRANCH=`echo -e ${BRANCH_COLOUR_NOT_OK}`”$BRANCH_PRE_TEXT””$BRANCH_NAME””$BRANCH_POST_TEXT”

#log ‘Clearing the $UPDATES variable’
UPDATES=””

#echo ‘About to start testing on “‘${FILES}'”‘
#echo
#log ‘Testing for untracked files (ones beginning with ??)’
UNTRACKED=$(echo ${FILES} | grep $’\x000?{2}’)
#echo ‘UNTRACKED=”‘$UNTRACKED'”‘

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”
UPDATES+=”$UNTRACKED_TEXT”

else

UNTRACKED=$(echo ${FILES} | grep ^?{2})
#echo ‘UNTRACKED=”‘$UNTRACKED'”‘

if [ -n “$UNTRACKED” ] ; then

#log “Untracked files were found”
UPDATES+=”$UNTRACKED_TEXT”

fi

#log “No Untracked files were found”

fi
#echo
#log “Testing for modified files”

modified=$(echo ${files} | grep $’\x000[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then]

#log “modified files were found”
updates+=”$update_text”

else

modified=$(echo ${files} | grep ‘^[rmdc ]{2}’)
#echo ‘modified=”‘$modified'”‘
if [ -n “$modified” ] ; then

#log “modified files were found”
updates+=”$update_text”

fi

#log “No Modified files were found”

fi

UPDATE_OUTPUT=`echo -e ${TEXT_CODES_COLOUR_NOT_OK}`$UPDATES
OUTPUT=$BRANCH$UPDATE_OUTPUT
fi
echo $OUTPUT
}

I hope it is of use to some of you and if you have any problems you know where I am.

John/Grandad

Run XBMC Fullscreen on second monitor in Ubuntu

Bash Resources

After trying to find a way to run XBMC fullscreen in second monitor I decided a bespoke solution was the way forward.

UPDATE: This post was written back in the times of Ubuntu 12 (I think). As such as as you can see from the comments at the bottom of the post there are now better ways of accomplishing this task.

Although this tutorial focuses on the XBMC Media Center it can be applied to many other programs.

I know there have been work-arounds for previous versions of Ubuntu but over the years they have fallen by the wayside.

What I’m going to show you now will allow you to run XBMC fullscreen in second monitor.

Points to Note

  1. If you want to copy the code I advise you to click on the word “bash” above the code, this will open up a new box for you to copy the correct code from
  2. This tutorial is to get XBMC running in fullscreen but the code can be used to get many other programs running in fullscreen
  3. I have structured the tutorial so that you can run XBMC fullscreen in second monitor but you can easily alter the code to have it fullscreen in your primary monitor
  4. Quiet a bit of this tutorial uses the terminal, you can open a Terminal window by pressing control (or command for MacOS), Alt & T at the same time

 

Step 1: Run XMBC and get the title

Run XBMC up so that we can find the title it uses.

To find the title type

wmctrl -l

into the terminal. This will bring up a list of windows that the window controller is in charge of.

The chances are that the XBMC window is called “XBMC Media Center” but it may be different on yours so look in the last column of the output and copy the XBMC title.

 

Step 2: compizConfig settings manager

See if you have compizConfig installed by typing

which ccsm

If you get a path to the executable such as

/usr/bin/ccsm

then you have it installed. If not type

sudo apt-get install compizconfig-settings-manager

Now you can start the program by entering

ccsm

in the terminal window.

 

Once the program has loaded look on the right hand side and under “Effects” click “Window Decoration

Now find the field titled “Decoration windows” and click on the green plus icon on the right hand side.

The CompizConfig Settings Manger

Look for the “Window Decoration” option

In the first drop down box select “Window Title

In the second box enter the title that the previous step displayed

Click the “Invert” checkbox and click Add

Once that box disappears you can exit that program.

What this does is hide the title bar from from the XBMC window

 

Step 3: Get your display information

Now we need to get the display details

Run xrandr from the terminal

xrandr | grep " connected" | sed -e 's/\(.*\) connected \(.*\) (.*/\nDisplay Name: \1\nDisplay Sizes: \2 /'

You will be presented with an output similar to the following

Display Name: VGA-0
Display Sizes: 1440x900+0+0

Display Name: DVI-I-1
Display Sizes: 1680×1050+1440+0

Assuming you’re wanting the program in your second monitor then we the information from the latter of the two monitors

The display name is straight forward – take a note of this.
The next line not to straight forward. The value is made up of

display-width x display-height + start-x-pxs-from-the-left + start-x-pxs-from-the-top

The value we need here is the third value as this will be where the XBMC window will start (from the left)

 

Step 3: Create out actual script file

Create a file called….well, what ever you want, I called mine XBMC_fullscreen.sh

In this file type or paste the following:

#!/bin/bash
# This script will run a program in fullscreen mode (no borders or
# title bar etc) on a second monitor. It can used for many programs
# and on either the primary or secondary monitor
#This is the executable file or path
PROGRAM='xbmc'
# This is the output of wmctrl -l for the program that we are using
NAME='XBMC Media Center'
# This is the device name of the monitor we want to display the program on
DEVICE='DVI-I-1'
# If you want the program to be fullscreen on your second monitor
# this variable should be the width of your primary monitor
PRIMARYWIDTH=1440
# Run the program , then wait a bit before carrying on
# If your program doesn't load in time then try increasing this value
$PROGRAM > /dev/null 2> /dev/null & disown && sleep 3
## Set the Open GL environment variables
# Set the vertical blanking to on
__GL_SYNC_TO_VBLANK=1
# And make sure OpenGL syncs the correct monitor
__GL_SYNC_DISPLAY_DEVICE="$DEVICE"
# Do we want a screensaver
SDL_VIDEO_ALLOW_SCREENSAVER=0
# Set the dimensions of the program in the monitor
wmctrl -r "$NAME" -e '0,'$PRIMARYWIDTH',-1,-1,-1'
# Maximize it
wmctrl -r "$NAME" -b toggle,fullscreen,maximized_vert

 

Now you need to change the NAME variable to something other than the default value **IF** your version of XBMC has a different title in the window (see last step).

You also need to change the DEVICE variable to the one returned as Display Name above.
Lastly you need to change the PRIMARYWIDTH variable to the third number in the returned Display Sizes above.

Now let’s take a look at what this does (just the important lines though)

#!/bin/bash
This tells the system to use bash to execute the script
PROGRAM='xbmc'
Set the program to the path we found earlier
NAME='XBMC Media Center'
This sets the title of the the window that we want to control
DEVICE='DVI-I-1'
This is the name of the display device that we noted down earlier
PRIMARYWIDTH='1440'
This is the width of your primary monitor
$PROGRAM > /dev/null 2> /dev/null & disown && sleep 3
Ok, this line will:

  1. Start the program
  2. Redirect the output
  3. Disown the process so that the the script can carry on
  4. Wait 3 seconds so the program has a chance to start
__GL_SYNC_TO_VBLANK=1
Set the vertical line blanking to on
__GL_SYNC_DISPLAY_DEVICE="$DEVICE"
Make sure that we tie our XBMC window to the device we want
SDL_VIDEO_ALLOW_SCREENSAVER=0
Make sure we don’t have the screensaver
wmctrl -r "$\$NAME" -e '0,"$PRIMARYWIDTH",-1,-1,-1'
This will set the window to start at the end of the primary monitor
wmctrl -r "$\$NAME" -b toggle,fullscreen,maximized_vert
This will put the window in full screen but we also need to maximize the window vertically

 

Step 5: Save the file

Now we need to save the file and make it executable.

I’m sure you know how to save a file but then once it’s saved go back into the terminal and type

chmod +x

So for me it was

chmod +x ~/xbmc_fullscreen.sh

Step 6: Create a shortcut

If you haven’t already got it, install gnome-panel

sudo apt-get install --no-install-recommends gnome-panel

Now type

gnome-desktop-item-edit ~/Desktop/ --create-new

This will bring up a box that will enable us to add a shortcut to the desktop. We can then drag and drop it on the launcher.

Attaching this shortcut to the Launcher will allow you to run XBMC fullscreen in second monitor

The Create a shortcut dialogue box

Ok, so

In the “Type” box leave the default value (Application)

In Name type anything you like, I typed “Run XBMC in Full Screen

In the 3rd box type the path to our shell file in the form of /home/user/xbmc_fullscreen.sh

If you want to type any comments in the last box then feel free.

Take a look on your Desktop and you should now have a shortcut to XBMC that works as default in full screen.

If you want, simply drag this shortcut over the launcher to the side of the screen.

That about does it for this script.

That’s it, thanks for looking, I hope you enjoyed the post and don’t forget to leave your comments below.

Update

At some point further down the line the above script stopped working for me. It appears as if it needed a bit of a timeout so I often had to run a script containing these commands. This basically goes through the previous proceedure but as the XBMC program is already running it just sets the GUI co-ords etc.


PROGRAM='XBMC'
NAME='XBMC Media Center'
DEVICE='DVI-I-1'
PRIMARYWIDTH=1440
__GL_SYNC_TO_VBLANK=1
__GL_SYNC_DISPLAY_DEVICE="$DEVICE"
SDL_VIDEO_ALLOW_SCREENSAVER=0
wmctrl -r "$NAME" -e '0, '$PRIMARYWIDTH',-1,-1,-1'
wmctrl -r "$NAME" -b toggle,fullscreen,maximized_vert

If you have run the first part of the post but XBMC is still in your primary monitor then try this part. Hopefully your system just needed a bit of a timeout before the wmctrl commands were set.

Vimtip #1: start gvim from terminal and still use terminal

Resources Vim-tips

This tip will allow you to start gvim from the terminal and still use it after Gvim has opened.

Step 1:
Add the following to your ~/.bashrc or mac equivalent


function gvim {
/usr/bin/gvim -f $* > /dev/null 2> /dev/null & disown
}

Step 2:
Type gvim in your terminal and it will not hog it until you close gvim i.e

john@John-Tosh-Lin 10:31 ~
$ gvim
[1] 10381

john@John-Tosh-Lin 10:31 ~
$

As a bonus, if you start gvim in the directory that you are working in then NERDTree’s root will be the directory you are in. That in itself saves a lot of time 🙂
Tada!
Enjoy your newly reclaimed terminal people 🙂

Find string in Files

Web Development

Well tonight I was supposed to be pulling the new project to bits that I’m working on but I ran into a problem – let me explain.

Every time I have wanted to find some text in a directory I’ve had to either search the internet or my bash history for the right command. With this in mind I thought I may as well take a little time to create a function that would just search for the string that I want. After all while I’m learning the layout etc of this project I’m going to need it. Not knowing where all the files are as yet and what they all do it’s going to be a lot easier to run the function and see what files the string turns up in.

The function to put in your ~/.bashrc or similar file

function find-string() {

# re-iterate what we're looking for
echo 'looking for "'$1'"'

# perform a search on all files
find ./ -xdev -type f -print0 | \

# process each file through the grep,
# --colour=always = always show the coloured version
# --ignore-case = search is not case-sesitive
# --line-number = show the line numbers after the filename
# --context=1 = this will print 1 line both above and below the line where the string occurs
# "${1}" = finally search for the passed variable
xargs -0 grep --colour=always --ignore-case --line-number --context=1 "${1}" | \

# replace each tab with 2 spaces to cut down on the amount of indentation in
# the search results from files with large areas of indentation
sed -re 's/\t/ /g'

}

And to get it up and running just run

source ~/.bashrc
to enable your system to see the function and then when you want to use it you can simply enter something like:
find-string viewer-thumbnails-wrapper
find-string being the function and in this case we are searching for file containing viewer-thumbnails-wrapper which will bring results like these:
Looking for "viewer-thumbnails-wrapper"
./css/viewer.css-418-/* Thumbnails */
./css/viewer.css:419:.viewer-thumbnails-wrapper{
./css/viewer.css-420- background: #FFF;
--
./js/mediagallery.js-61- min_thumbs: 5,
./js/mediagallery.js:62: thumbwrapper: 'div.viewer-thumbnails-wrapper',
./js/mediagallery.js-63- btn_thumbs_show: 'a.viewer-thumbs',

Ok, that’s it, if you can make use of the this function then go ahead and copy and paste.”
Enjoy!

if there’s any help I can offer then let me know in the comments section below.

A rant to the Web Development community

A rant to the Web Development community

Web Development

Contents

  1. Ok, let’s get started
  2. A bit of background
  3. So what did I create previously?
  4. What am I complaining about?
  5. But Wait! (a glimmer of hope maybe?)
  6. So how should it be done?
  7. So what do I want?
  8. Rant over

Ok, let’s start

Let me start by saying that I know I’m using an existing theme but I needed something to put a blog on so I used an existing theme….so what.

A bit of background

I am about to become a freelance PHP Web Developer and so I thought I’d get around a few places in my local are a and meet some guys (& girls) that I’s been following on Twitter for a while.
I decided that I would visit Genuis Division in Barnsley as I had been following Craig Burgess on there for a while. The visit itself was very fruitful. I was made to feel very welcome by 4 very nice gentleman. What would have surprised my wife is that although the 4 of them were self confessed ‘Geeks’ not one of them had a Star Trek uniform on…anyway, I digress.

By the end of the visit I had a much firmer idea of my route into the webdev community & I knew that my months of trying to get my head around around the ins and outs of the design process were waisted. As Craig and Richard (their Techincal Director) pointed out – I should concentrate on what I am good at & what I enjoy doing. Common sense stuff of course but one that I possibly just needed to hear from an external source.
I knew when I got home that I had to clear all of this design nonsense out of my head and concentrate on the PHP side of things as I had now been focusing on design for a few months.

Kinda how I felt

Kinda how I felt

I posted a thread on a popular webdev forum about what project I could undertake in order to brush up my OOP skills. The majority of the replies that I got advised me to create my own framework. I took this advice & quickly set about Googling a few tutorials for how to create a framework.

Until this point I had not been a fan of frameworks per say as I found them overkill for the projects I had previously created but when I did start looking into it I was shocked.
The more I looked into frameworks the more I thought to myself “Hang on! I’ve already done this and been criticised for it”

So what did I create previously?

On the same popular forum around a year ago I posted my own files for review. I now know that this was the basis of a framework. Admittedly, looking back on the code it could do with a lot of improving and more abstraction but the basics were there. The framework had 5 or 6 classes that I used and covered everything that I needed including security, database handling, HTML output, templating facilities and more. Had I got a few words of encouragement, I may have continued to develop this framework and stick with OOP.

So what am I complaining about

I’m complaining about the critique process that many employed. The tone was a very negative one with comments made such as “Not quite sure why you’ve put it in a class to be honest”. Not one single person included a positive comment. This in my opinion leads to much dis-heartening and people giving up. This included me as a justified comment could have gone a long way to encouraging me to continue with the project. If someone had written something along the line of “You have the start of a framework there, if you continue along those lines & study the architecture of some of the popular frameworks you could have a good framework there”.

Success or Failure?

Photo credit: Cobrasoft

Another example

On a different forum I posted a general question on whether or not developers kept their files (not the files directly accessible via HTTP requests) in a non-accessible directory (i.e. sub root). The comments were generally good & polite but there were a few that told me in no uncertain terms that I was an idiot to keep files sub-root and there was no benefit in it. I didn’t dignify them with a response but I did know the benefits of this practice thanks to Chris Shiflett‘s Essential PHP Security book (non-affiliated link) which I bought a few years ago.

Yet Another Example

On the 13th December, 2012 Harry Roberts posted a tweet which I believe was also trying to get to the same thing as I am complaining about here. He tweeted:

 

Hacker News in a nutshell: Dev 1: ‘Hey I made this, it took me a year and you can have it for free!’ Dev 2: ‘Your screencasts sound bad.’

In this example. a little encouragement would have been a good thing – I haven’t seen the post to which Harry was referring to but it seems that the same theme of comment keeps popping it’s ugly head up.

But wait!

There was also a direct message waiting for me to look at. On reading it I as quite impressed. It was commenting on my profile & the fact that I used an outdated IDE (the profile hasn’t been updated in a while) but the sender framed it in such a way as to not have a negative tone. He then went on to suggest good IDEs etc.

So how should it be done?

In my opinion critique is an art form in itself but a lot of it is often down the attitude of the person critiquing. Instead of just offering negative comments why not frame a comment constructively. Image…We could even give its own name: we could call it something like “Constructive criticism” instead of ‘being an arsehole’.

Simply typing “How to criticise” in Google shows that the top article has several suggestions. As an ex-military instructor I use the 1st type of critiquing: one that is referred to by many including @CarriganGlen, a good friend of mine and former army buddy as a “Shit Sandwich Motivational Technique”. It allows critique to be given but not in a wholly negative manner.

Craig Burgess commented on a recent podcast saying that


As somebody who’s spoken in a public capacity a few times, … the pressure is massive. And 1 negative comment can crush you.
.

This shows what I have mentioned previously: that negativity can have an effect on one’s attitude and ambition to carry on with a project.

So what do I want?

It would be nice if people learn to critique properly – doing otherwise can only result in self-perpetuation negativity. I suggest that people think before they critique and carefully frame their words as to not be offending or be overly negative.

Rant over

Well that’s it folks. If you got to this point in the post I want you to do 3 things for me.

  1. Give yourself a pat on the back, you did well.
  2. Leave a comment if you have anything to add
  3. Make yourself a brew, you deserve it

By the way, please feel free to Tweet about this post to the wider webdev community.
Thanks again.
🙂