Friday, January 18, 2013

ZSH autocompletion for tmuxinator

I have been using robbyrussell's Oh My Zsh distribution for a few months now and I feel that it's a very good starting point for ZSH newbies. It's a reasonably good set of plugins, themes and configuration files that makes terminal users' life easier.

Yet another handy tool is tmuxinator which makes using/configuring/starting/managing tmux sessions a cinch. However I found that zsh doesn't have autocompletion for tmuxinator command built-in. And all it'd do when I press 'tab' is list out the files and directory in the current directory. It would be cool if I can make it autocomplete command-line options and project names. And that's what I did this morning.

Here is the piece of code you can put it in file called tmuxinator.plugin.zsh inside custom/plugins/tmuxinator/ directory.

_tmux_cmd() {
  # Constants
  local tmuxinator_configs_path
  tmuxinator_configs_path="$HOME/.tmuxinator"

  local a
  read -l a

  num_args=$(echo "$a" | awk '{print NF}')
  last_arg=$(echo "$a" | awk '{print $NF}')

  if [[ $num_args -le 1 ]]; then
    reply=(start open copy delete implode list doctor help version)
  else
    # whether to autocomplete project names
    if [[ -n "$(echo "start open copy delete" | grep -E "\<$last_arg\>")" ]]; then
      reply=(`ls "$tmuxinator_configs_path" | sed 's/\.yml$//' | sed -e :a -e '$!N; s/\n/ /; ta'`)
    fi
  fi
}

compctl -K _tmux_cmd tmuxinator

Here's how the final outcome looks...

When I enter 'tmuxinator ' and press tab, I'll get this

When it is 'tmuxinator open ' (or start or delete or copy), it'd give

Great!

Thursday, January 3, 2013

Split/cut mp3 files using ffmpeg

FFmpeg is a very handy tool. It's hard to find a video/audio encoding/decoding task which it cannot do.  But, with the command-line version one would have to go through the manual page to know about it much better.

Splitting mp3 files is fairly straight forward process.

ffmpeg -i input-file.mp3 -acodec copy -t 00:00:30 -ss 00:02:20 output-file.mp3

This is what I do to extract a 30-second portion (2.20 secs to 2.50 secs) from an mp3 file. Simple!

Sunday, October 7, 2012

Ugly-looking gtk3 apps

Often times, using of themes which do not have GTK3 styling in KDE or lightweight desktop environments like Xfce, Openbox and Lxde results in the latest GTK applications like gedit, transmission and evince looking unstyled and ugly.

Here's how my gedit looks like in my Openbox when I use my 'xfce-4.0' gtk2 theme:


So, just copy the default Adwaita theme to your home directory with something like

cp -r /usr/share/themes/Adwaita/gtk-3.0 ~/.config/

After that, edit your ~/.config/gtk-3.0/settings.ini file and insert just the following contents.

[Settings]
gtk-theme-name = Adwaita
gtk-icon-theme-name = fs-icons-ubuntu
gtk-fallback-icon-theme = fs-icons-ubuntu

# next option is applicable only if selected theme supports it
gtk-application-prefer-dark-theme = false

# set font name and dimension
gtk-font-name = Sans 8
gtk-toolbar-style=GTK_TOOLBAR_ICONS
gtk-toolbar-icon-size=GTK_ICON_SIZE_MENU

Add voila, here's how it looks afterwards, (pardon the change in colorscheme of gedit)


Wednesday, August 22, 2012

Bypass proxy script for Pacman

A few months back I had written a small bash script to bypass the download limit (14MB) imposed by our proxy server. What it essentially does is, downloads files chunk-wise; for instance, in my case, it'd download 14MB chunks at a time and appends it to the file... all on-the-fly! It served me pretty well all these days. Moreover, I also wanted to use it as my Pacman's XferCommand to stay up-to-date.

Though the original version works, it needs a few tweaks. For bigger files, output doesn't look streamlined. Like this...


I removed all unwanted code like i) filename guessing from URLs and ii) output directory validations. And beautified/simplified it's output to look like the below screenshot. To use it with pacman, go to /etc/pacman.conf and add a XferCommand like shown in the top pane of the below screenshot. That's it, now it works well with pacman too...





Here's the new code... It's also on github

#!/bin/bash
#
# Vikas Reddy @
# http://vikas-reddy.blogspot.in/2012/04/bypass-proxy-servers-file-size-download.html
#
#

# Erase the current line in stdout
erase_line() {
    echo -ne '\r\033[K'
}

# Asynchronously (as a different process) display the filesize continously
# (once per second).
# * Contains an infinite loop; runs as long as this script is active
# * Takes one argument, the total filepath
async_display_size() {
    PARENT_PID=$BASHPID
    {
        # Run until this script ends
        until [[ -z "$(ps x | grep -E "^\s*$PARENT_PID")" ]]; do
            # Redraw the `du` line every second
            erase_line
            echo -n "$(du -sh "$1") "
            sleep 1
        done
    }&
    updater_pid="$!"
}

# Defaults
fsize_limit=$((14*1024*1024)) # 14MB
user_agent="Firefox/20.0"


# Command-line options
while getopts 'f:d:u:y' opt "$@"; do
    case "$opt" in
        f) filepath="$OPTARG"    ;;
        u) user_agent="$OPTARG"  ;;
    esac
done
shift $((OPTIND - 1))

# Exit if no URL or filepath argument is provided
if [[ $# -eq 0 ]] || [[ -z "$filepath" ]]; then
    exit
fi

# Only one argument, please!
url="$1"

# Create/truncate the output file
truncate --size 0 "$filepath"


# Asynchronously (as a different process) start displaying the filesize
# even before the download is started!
async_display_size "$filepath"

# infinite loop, until the file is fully downloaded
for (( i=1; 1; i++ )); do

    # setting the range
    [ $i -eq 1 ] && start=0 || start=$(( $fsize_limit * ($i - 1) + 1))
    stop=$(( $fsize_limit * i ))

    # downloading
    curl --fail \
         --location \
         --user-agent "$user_agent" \
         --range "$start"-"$stop" \
         "$url" >> "$filepath" 2> /dev/null; # No progress bars and error msgs, please!

    # catching the exit status
    exit_status="$?"

    if [[ $exit_status -eq 22 ]] || [[ $exit_status -eq 36 ]]; then
        # Download finished
        erase_line
        echo "$(du -sh "$filepath")... done!"
        break
    elif [[ $exit_status -gt 0 ]]; then
        # Unknown exit status! Something has gone wrong
        erase_line
        echo "Unknown exit status: $exit_status. Aborting..."
        break
    fi

done


This can also be used as a standalone script to download files normally. Just that you need to mention the full filepath of where you want to download. Like this...

./pacman-curl.sh -f ~/downloads/video.mp4 'http://the-whole-url/'




Thursday, August 16, 2012

TMUX Cheatsheet

TMUX is a fitting competitor to GNU Screen. Here's a cheatsheet of most keybindings. I lifted it straight from its man page.

TMUX Cheatsheet.docx
TMUX Cheatsheet.pdf

Monday, August 13, 2012

VIM Molokai colorscheme

Here’s a screenshot of my gVim…
  • Colorscheme: molokai
  • Font: Monaco 10pt
  • OS: Archlinux
  • Resolution: 1280x1024

Thursday, June 28, 2012

Archlinux: Compile ffmpeg with nonfree codecs

The ffmpeg binaries that are present in the official repository of Archlinux are not compiled with nonfree codecs. Some excellent ones like libfaac, libx264 and lib-nonfree are not included by default. However, with AUR and its package building automation tool yaourt, one can easily accomplish this. Here are the steps...
  1. Install yaourt using pacman -S yaourt, if you haven't already.
  2. Key in yaourt -Sb ffmpeg, and answer 'Yes' when asked for confirmation. Remember, you'd have to edit PKGBUILD when asked for. Add --enable-nonfree and --enable-libfaac and other needed configuration options to the build() function, exit the editor, and proceed.
  3. That's it, get yourself a cup of tea while the compilation process to finishes, and you're good to go.
PKGBUILD in vim

For a list of available configuration options, see http://git.videolan.org/?p=ffmpeg.git;a=blob;f=configure;h=f30998b37c80ff83a81f4949ea8ab0cfb8043376;hb=HEAD