git bash prompt

posted by alexis reigel on july 05, 2013

Hi.

I tried many different bash git prompt solutions, all of which were either too complicated, too buggy or too ugly. So…

I didn’t want to invent too much myself, so I used the git prompt provided by git as a basis. This script does all the magic already, so the only thing left to do was to make it look nicer. This is the whole section in my .bash_profile:

# bash git prompt
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="verbose"

git_current_branch_name="\$(__git_ps1 '%s' | sed 's/ .\+//' | sed -e 's/[\\\\/&]/\\\\\\\\&/g')"
git_status_substitutes=(
    "s/$git_current_branch_name//;" # remove branch temporarily
    "s/u//;" # upstream
    "s/+\([0-9]\+\)/▴\1/;" # outgoing
    "s/-\([0-9]\+\)/▾\1/;" # incoming
    "s/%/?/;" # untracked
    "s/+/✓/;" # staged
    "s/*/✕/;" # unstaged
    "s/\(.\+\)/($git_current_branch_name\1)/;" # insert branch again
)
git_status_command="\$(__git_ps1 '%s'| sed \"${git_status_substitutes[@]}\")"

if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+($debian_chroot)}\[\033[0;37m\] \w \[\033[34m\]$git_status_command\[\033[37m\]\$\[\033[00m\] "
else
PS1="${debian_chroot:+($debian_chroot)} \w $git_status_command\$ "
fi
unset git_status_substitutes git_status_command git_current_branch_name

And now, step by step:

GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWUPSTREAM="verbose"
git_current_branch_name="\$(__git_ps1 '%s' | sed 's/ .\+//;s/(//')"
git_status_substitutes=(
    "s/$git_current_branch_name //;" # remove branch temporarily
    "s/u//;" # upstream
    "s/+\([0-9]\+\)/▴\1/;" # outgoing
    "s/-\([0-9]\+\)/▾\1/;" # incoming
    "s/%/?/;" # untracked
    "s/+/✓/;" # staged
    "s/*/✕/;" # unstaged
    "s/\(.\+\)/($git_current_branch_name \1)/;" # insert branch again
)
git_status_command="\$(__git_ps1 '%s'| sed \"${git_status_substitutes[@]}\")"
if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[0;37m\] \w \[\033[34m\]$git_status_command\[\033[37m\]\$\[\033[00m\] "
else
    PS1="${debian_chroot:+($debian_chroot)} \w $git_status_command\$ "
fi

The final output looks like this:

~/src/dotfiles (master ✕✓? ▴1▾5)$

The original output of __git_ps1 looks like this, which is much more cryptic:

~/src/dotfiles (master *+% u+1-5)$

The meaning of the symbols:

Unstaged changes
Staged changes
? Untracked files
▴1 One changeset ahead of remote
▾5 Five changesets behind remote
= No difference to remote

You can see the whole thing in action in my .bash_profile.

how to generate a webfont kit with open source tools

posted by alexis reigel on august 06, 2012

There are some web based services that convert fonts to a webfont package, the most popular of which is the fontsquirrel font-face generator. For our metaflop project I was looking for a service api that i could call from within our application. Since i didn’t find one I was attempting to build the whole webfont generation by collecting tools that could do the job.

Font types

To create a webfont kit that works on all browsers we need to generate several different font types:

Tool chain

The following tools are required to generate all the needed font types. I assume that you have your font as otf. If you have a ttf, just switch ttf with otf in the following statements:

The very simple FontForge script (ttf-svg.pe) looks like this:
Open($1)
Generate($1:r + ".ttf")
Generate($1:r + ".svg")
# outputs font.ttf, font.svg
$ fontforge -script ttf-svg.pe font.otf
# outputs font.woff
$ sfnt2woff font.otf
$ ttf2eot font.ttf > font.eot

CSS

The css declaration is based on Fontspring’s bulletproof @font-face syntax and is the same syntax as used by fontsquirrel.

@font-face {
    font-family: 'YourFont';
    src: url('YourFont.eot'); /* IE 9 Compatibility Mode */
    src: url('YourFont.eot?#iefix') format('embedded-opentype'), /* IE < 9 */
         url('YourFont.woff') format('woff'), /* Firefox >= 3.6, any other modern browser */
         url('YourFont.ttf') format('truetype'), /* Safari, Android, iOS */
         url('YourFont.svg#YourFont') format('svg'); /* Chrome < 4, Legacy iOS */
}

That’s it. Remember though that you should only convert fonts are legally eligible for web embedding.

useful linux commands

posted by alexis reigel on january 16, 2004

This is a random aggregation of useful linux commands.
Useful for those who will be eventually stuck as I once was.
Or those who didn’t know that certain things can be done in such a smooth way…

Print a locked pdf

To be able to print a pdf file even though it has been print locked you can convert the pdf to postscrloipt and back again to a pdf.

$ pdf2ps locked.pdf unlocked.ps && ps2pdf unlocked.ps unlocked.pdf

Mount a cd-rom image

This will mount an image file to a certain mountpoint such that it can be used as a usual drive (like a virtual drive in windows).

$ mount -o loop -t iso9660 <isofilename> <mountpoint>

ssh with x-window support

With this you can launch an x-window application via an ssh connection (provided that the host computer does support the particular toolkit).

$ ssh -l <loginname> <remote-host> -X -C <path-to-application>

Print out manpage

Prints out a manpage in a nicely formatted way.

$ man <manpage> | col -b | ul -t dumb | lpr -P<printername>

$ man -Tps <manpage> | lpr -P<printername>

Find advanced

Find is a very powerful command, as it is extensible in a very convenient way.
Every find-result is passed to “command”.

$ find <parameters> -exec <command '{}' ';'>

example 1, copies all jpg images to the folder “img/”

$ find -name *.jpg -exec cp '{}' img/ ';'

example 2, searches all textfiles for “searchstring”

$ find -name *.txt -exec grep "searchstring" '{}' ';'

Disable beep

If it’s about time to disable the annoying beep in the shell. For the current user

$ echo "set bell-style none">> ~/.inputrc

For all users (you have to be root) in /etc/inputrc set the line set bell-style none

Convert a text file from DOS to Unix style

This tool converts dos formatted text files with end-of-line ^M^J to unix end-of-line ^J.

$ dos2unix <file>

Patch file

With the following command you create a patch file from any source file.

$ diff -u src.old src.new1 > file.patch0

To apply the patch

$ patch -p0 file < file.patch0

Convert a large file into small files

$ split -b 650m file # split file into 650MB chunks

$ cat x* > largefile # merge files into 1 large file

Web page dump

The following will save the contents of a web page to a textfile.

$ lynx -dump http://www.somesite.org/somepage.html > textfile

Clear file contents

In order to clear the contents of a file such as a logfile, do not use rm to delete the file and then create a new empty file, because the file may still be accessed in the interval between commands. The following is the safe way to clear the contents of the file.

$ :>file-to-be-cleared

Merge two PostScript or PDF files

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pswrite -sOutputFile=merge.ps -f file1.ps file2.ps

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merge.pdf -f file1.pdf file2.pdf

Samba client basics

Mount a windows (samba) share

$ mount -t smbfs -o username=myname,uid=my_uid,gid=my_gid //server/share /mnt/smb

$ smbmount //server/share /mnt/smb -o "username=myname,uid=my_uid,gid=my_gid"

List the shares on a computer

$ smbclient -L 192.168.1.2

Samba neighbors can be checked from Linux using the following command

$ smbclient -N -L ip_address_of_your_PC | less

$ nmblookup -T "*"

Make iso from cd

$ dd if=/dev/cdrom of=image.iso

Burn iso to cd

For an ide device

$ cdrecord -v speed=SPEED dev=ATAPI:1,0,0 -data image.iso

To find out the device number, use the following command

$ cdrecord -scanbus dev=ATAPI

Alternatively, you can specify the ide device directly

$ cdrecord -v speed=SPEED dev=ATAPI:/dev/hdc -data image.iso

For a scsi device

$ cdrecord -v speed=SPEED dev=1,0,0 -data image.iso

To find out the device number, use the following command

$ cdrecord -scanbus