• Git pretty log output

    This alias has been around the web for quite some time, but it does look fantastic indeed.

    An output of `git pretty-log` alias.

    To add the alias git pretty-log, execute the following command (join string prior to executing):

    git config alias.pretty-log 'log --graph --pretty=format:"%Cred%h%Creset
    -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset"'
    
  • Download gists from prompt

    I wrote a little script to download gists from the command prompt.

    Generate your Github API Token under Settings -> Applications, change it within a script, and then:

    chmod +x shgist.py
    mv shgist.py ~/bin/shgist
    

    Where ~/bin is a directory in your path. Now you can use it as shgist file to quickly download your gists (Gist on Github).

    #!/usr/bin/env python
    
    # Ruslan Osipov
    # Usage: shgist keywords
    # Description: Gists downloader
    
    import urllib
    import urllib2
    import sys
    import json
    
    token = 'Personal API Access Token'  # Github Settings -> Applications
    
    class Gist:
        def __init__(self, token):
            """
            token -- str, github token
            """
            self.token = token
            self.url = 'https://api.github.com'
    
        def find_by_name(self, keywords):
            """
            keywords -- list of strings
            """
            gists, urls = self._get_gists()
            for i, gist in enumerate(gists):
                for keyword in keywords:
                    if keyword not in gist:
                        del gists[i]
                        del urls[i]
                        break
            if len(gists) == 0:
                print "Sorry, no gists matching your description"
                return
            if len(gists) == 1:
                self._download_gist(gists[0], urls[0])
                return
            for i, gist in enumerate(gists):
                print i, gist
            while True:
                num = raw_input("Gist number, 'q' to quit: ")
                if num == 'q':
                    print "Quiting..."
                    return
                try:
                    num = int(num)
                    if 0 <= num < len(gists):
                        break
                    print "Number should be within specified range"
                except:
                    print "Only integers or 'q' are allowed"
            self._download_gist(gists[num], urls[num])
    
        def _download_gist(self, name, url):
            """
            name -- str, filename
            url -- str, raw gist url
            """
            print "Downloading %s..." % name
            gist = self._send_get_request(url)
            open(name, 'wb').write(gist)
    
        def _get_gists(self):
            """
            Returns 2 lists which should be treated as ordered dict
            """
            url = '/gists'
            response = self._send_get_request(self.url + url)
            response = json.loads(response)
            gists, urls = [], []
            for gist in response:
                for name, meta in gist['files'].items():
                    gists.append(name)
                    urls.append(meta['raw_url'])
            return gists, urls
    
        def _send_get_request(self, url):
            """
            url -- str
            """
            headers = {
                    'Authorization': 'token ' + self.token
                    }
            request = urllib2.Request(url, headers=headers)
            response = urllib2.urlopen(request)
            return response.read()
    
    argv = sys.argv[1:]
    if not len(argv):
        print "Usage: shgist keywords"
        sys.exit(0)
    
    gist = Gist(token)
    gist.find_by_name(argv)
    
  • My most used bash commands

    Shell history can tell a lot about its owner. What’s in your shell?

    history | awk '{CMD[$2]++;count++;}
    END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}'
    | grep -v "./" | column -c3 -s " " -t | sort -nr | nl |  head -n10
    
         1  580  38.0328%    git         # I keep everything under VCS
         2  202  13.2459%    cd          # Moving around a lot
         3  171  11.2131%    vi          # Favorite text editor
         4  127  8.32787%    ls          # I'm a curious person
         5  43   2.81967%    rm          # I also like when it's clean
         6  26   1.70492%    usrswitch   # https://gist.github.com/ruslanosipov/5453510
         7  25   1.63934%    exit        # I don't like hitting the red cross button
         8  18   1.18033%    source      # Reloading bash configuration files
         9  17   1.11475%    clear       # Like when it's *really* clean
        10  15   0.983607%   gitk        # Sometimes it is too messy for git log
    
  • Colorless week results

    A round-up of The Week Without Colorful Prompt.

    I worked with the colors disabled in bash, git, and vim for a week. So how did it go? It is definitely an interesting experience, but such a harsh change that it doesn’t really work out with everything.

    Bash

    Disabling colorful PS1 and removing color output for ls commands forced me to concentrate more on the actual text, changing the perception of the general bash workflow. I was more concentrated on the task, missed less details, and generally paid more attention to the output.

    Git

    Never repeat my mistake by disabling colors for git diff. Log and status are fairly easy to read, but the disabling of colors noticeably slows down the workflow.

    Vim

    Vim without code highlight forces you to remember your code structure more effectively, which is a great thing. Not having a need to rely on color can hint that a programmer has better understanding of the code he/she is writing.

    Now that the experiment is over I have mostly returned to using colorful prompt. But I do turn syntax highlight off once in a while - it allows you to see problems from new angle and work more efficiently at finding a solution. Try it and see for yourself!

  • A week without colorful prompt

    I noticed that I rely on colors in the bash terminal a lot, as in git output, diffs, directory and file listings… It gets worse when using vim - I feel lost without the cozy syntax highlight guidance.

    Time to stop using output colors for a week whether in shell, git, or vim, and use only plain text with no fancy colors. Set git config –global color.ui false and don’t use –color flags in shell. Also, set syntax off and set a simple color scheme for vim.

    What can I gain from all this? It will definitely reduce my productivity for a few days. However, I have a hint of an idea that changing the visual code representation will give me new insight on what I am currently writing.

    Link to related commit on GutHub.

    Check back in a week to see how it went!