thefourtheye's weblog

opinions are my own; try code/suggestions at your own risk

Ubuntu 13.04 - Execute Scripts on Double Click

| Comments

After upgrading to Ubuntu 13.04, I was facing this problem. I was not able to execute any script file just by double clicking on it, even though it has execute permission. Then I figured out, how to fix this problem.

I opened the file explorer Files. Pressed Alt+F10 and selected Preferences. It showed something like this.



In the Behavior tab, I selected Ask each time and closed it. By default, it was in View executable text files when they are opened. Now whenever I double click on a shell script, it will ask me what to do. If you want to straight away execute the script, select the first option Run executable text files when they are opened.

Gitclean - a Git Clean Wrapper

| Comments

Last week, I had to make some changes to the code. I was keep on making changes but forgot to add them to git and to take backup. When I am almost done I wanted to commit and before committing I checked the changed files with this.

git status

it listed all the files which have got changed and few other temporary files and compiled binaries, which I don't want to keep in the repository. So I did

git clean -qfxd

That's it. This cleared everything, including my newly added untracked files and all my hardwork went in vain. Then I read about git clean here and got to know what I could have done to avoid this problem. And then I was thinking, if there is a script which would remind me that, the following files will also be removed and only when I say `yes` it would go ahead and clean the repository. So I made this gitclean script. This would collect the status of the files with git status --porcelain and then compare it with the list of extensions specified in the config file. If they match, it would report to the user and ask his consent before beginning to clean.

Github


Github Repository : https://github.com/thefourtheye/gitclean.

Installation


To install this script, please follow these steps

wget https://raw.github.com/thefourtheye/gitclean/master/gitclean
chmod 544 gitclean
./gitclean install

  1. The first step will download the script from github
  2. The second step changes the permissions of the file to 544
  3. The third step installs the script. Basically, it copies itself to the user's home directory and creates the gitclean alias in the $HOME/.bashrc file

Configuration

Users can specify the extensions, gitclean has to report in the $HOME/.gitclean_extenstions file. By default, gitclean will report the extension-less files and directories as well. But, it does not report the empty directory. My .gitclean_extenstions file has the following items.
cpp
js

Sample Runs

1. With no files changed
~/gitclean$ gitclean

Interesting Extensions found in [/home/thefourtheye/.gitclean_extensions] are as follows
[cpp]
[js]

0 interesting files found, in total 0 files. Cleaning was not done. Exiting...
~/gitclean$
2. With one interesting file [Test.js]
~/gitclean$ gitclean

Interesting Extensions found in [/home/thefourtheye/.gitclean_extensions] are as follows
[cpp]
[js]

Test.js

1 interesting files found, in total 1 files. Are you sure want to clean? [y/Y - Yes, Any other Key - No] : n
Cleaning was not done. Exiting...
~/gitclean$
3. With one interesting file [Test.js] and a non-empty directory
~/gitclean$ gitclean

Interesting Extensions found in [/home/thefourtheye/.gitclean_extensions] are as follows
[cpp]
[js]

Test.js
Test/

2 interesting files found, in total 2 files. Are you sure want to clean? [y/Y - Yes, Any other Key - No] : y
Cleaning...

~/gitclean$
4. With one interesting file [Test.js] and a non-empty directory and a non-interesting file [Test.txt]
~/gitclean$ gitclean

Interesting Extensions found in [/home/thefourtheye/.gitclean_extensions] are as follows
[cpp]
[js]

Test.js
Test/

2 interesting files found, in total 3 files. Are you sure want to clean? [y/Y - Yes, Any other Key - No] : y
Cleaning...

~/gitclean$

Sublime Configuring CodeIntel Auto Completion

| Comments

Unarguably Sublime is one of the best text editors and with its ability to extend with python scripts, gifts itself a lot of power. Here lets see one of those extensions which allow code completion. Normally, sublime provides code completion for the words in the current file being edited. Codeintel allows the user to get code completion feature of the respective languages.

Installation

Lets install CodeIntel, through Package Control plugin. To install Package Control plugin, do the following steps. Here is the author's page. Quoting from the source website

Installation is through the Sublime Text 2 console. This is accessed via the ctrl+` shortcut. Once open, paste the following command into the console.

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print('Please restart Sublime Text to finish installation')

After installing it, Press Shift+Ctrl+P or click on Tools->Command Palette. In the window that opens up, type Install Package. In the text box which you see after that, type SublimeCodeIntel and click on it to install it. You are done with the installation of CodeIntel.

Configuration

The following information is taken from the README.rst file. Open ~/.codeintel/config file in your favourite text editor, most likely Sublime. Pick your language from the list below and copy paste only the blocks for the languages of your choice.

    {
"PHP": {
"php": '/usr/bin/php',
"phpExtraPaths": [],
"phpConfigFile": 'php.ini'
},
"JavaScript": {
"javascriptExtraPaths": []
},
"Perl": {
"perl": "/usr/bin/perl",
"perlExtraPaths": []
},
"Ruby": {
"ruby": "/usr/bin/ruby",
"rubyExtraPaths": []
},
"Python": {
"python": '/usr/bin/python',
"pythonExtraPaths": []
},
"Python3": {
"python": '/usr/bin/python3',
"pythonExtraPaths": []
}
}

For example, I like to use python and my config file looks like this
{
"Python": {
"python": '/usr/bin/python',
"pythonExtraPaths": []
},
}

Thats it. Restart your sublime, type code and press Alt + / to see the Auto completion at work.

Ubuntu Scaling the Display (Zooming)

| Comments


Today I came across this useful trick, which allows me to have lot of space on my screen, even though my screen supports only very low resolution.

First, lets see how to get to know the list of displays attached to Ubuntu

~$ xrandr
Screen 0: minimum 8 x 8, current 2881 x 1280, maximum 16384 x 16384
VGA-0 connected 1600x1280+1281+0 (normal left inverted right x axis y axis) 376mm x 301mm
1280x1024 60.0*+ 75.0
1152x864 75.0
1024x768 75.0 60.0
800x600 75.0 60.3
640x480 75.0 59.9
LVDS-0 connected (normal left inverted right x axis y axis)
1600x900 60.0 + 40.0
DP-0 connected 1280x1024+0+0 (normal left inverted right x axis y axis) 376mm x 301mm
1280x1024 60.0*+ 75.0
1152x864 75.0
1024x768 75.0 60.0
800x600 75.0 60.3
640x480 75.0 59.9
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-0 disconnected (normal left inverted right x axis y axis)
DP-2 disconnected (normal left inverted right x axis y axis)
DP-3 disconnected (normal left inverted right x axis y axis)
~$

This is what I got on my machine. And following are the display names as per Ubuntu.
VGA-0 
LVDS-0
DP-0

This command has listed out other useful information as well. I could only understand the resolutions supported. Following are the resolutions supported by VGA-0 and DP-0.
1280x1024
1152x864
1024x768
800x600
640x480
So the maximum resolution allowed is 1280x1024. But that doesnot allow me to have more space on screen. After long searching, I got this.
xrandr --output VGA-0 --mode 1280x1024 --scale 1.25x1.25
This command sets the resolution of VGA-0, to 1280x1024 but it scales it by the factor of 1.25x1.25. Voila :) Now I have much bigger screen.


Google Code Jam 2013 - Round 1B - Osmos

| Comments

Today, I would like to share my solution to the Google Code Jam 2013's Round 1B's Osmos problem. These are the statistics for that problem
Small Input : 4668/7250 users correct (64%)
Large Input : 3537/4578 users correct (77%)
I believe it was tricky enough for an easy problem. Many people have got atleast one wrong try. Here is how I solved it (I couldn't solve it during the contest).

# include <iostream>
# include <cstdio>
# include <algorithm>

using namespace std;
int Sizes[101];
int Total, A, N, Temp, Counter;
int getDiff (int A, int B, int & C)
{
while (A <= B)
{
C++;
A += (A-1);
}
return A;
}
int Solve (int idx, int CurrentScore, int Changes)
{
if (idx == N) return Changes;
if (CurrentScore == 1)
return Solve (idx + 1, CurrentScore, Changes + 1);
else if (Sizes[idx] < CurrentScore)
return Solve (idx + 1, CurrentScore + Sizes[idx], Changes);
else
{
int tChanges = 0;
int Diff = getDiff (CurrentScore, Sizes[idx], tChanges) + Sizes[idx];
int Min1 = Solve (idx + 1, Diff, Changes + tChanges);
if (Sizes[idx] > CurrentScore)
return min (Solve (idx + 1, CurrentScore, Changes+1), Min1);
else return Min1;
}
}
int main()
{
//freopen ("Input.txt", "r", stdin);
//freopen ("Scratch.txt", "w", stdout);
scanf ("%d", &Total);
for (int i = 1; i <= Total; i++)
{
scanf ("%d%d", &A, &N);
Temp = A;
Counter = 0;
for (int j = 0; j < N; j++) scanf ("%d", &Sizes[j]);
sort (Sizes, Sizes + N);
printf ("Case #%d: %d\n", i, Solve (0, A, 0));
}
}
Here is the ideone URL http://ideone.com/g5qxYd where I tested the program with Google's inputs.