Thursday, January 12, 2012

Enhance Coding Experience at Linux








Hi friends,

I am back again with a good utility for programmers.Are you bored of putting same header files again  and again while writing a new code every time ???. I was wondering if it is possible whenever i open a file with '.c' or '.cpp' extension my system open the file and put the standard headers (at least frequently used like stdio.h) in it automatically and i need not to waste time writing them.

I discussed the same with my good friend Utkarsh Srivastav , he then came with a very good script which served my purpose.I am Sharing the same script here and how you can make use of the script to the maximum.

Step 1:
Save this Script as coding.sh in your home directory ( in my case /home/amol)

file=$1
x=${file##*.}
if [ "$x" == "c" ]
then
 if cat $1 > /dev/null 2> /dev/null
 then echo ""
 else
 echo "#include   #YOUR C TEMPLETE HERE
#include

#include
#include
#include

int main()
{
        
 return 0;
}
" >> $1
 fi
elif [ "$x" == "cpp" ]
 then
 if cat $1 > /dev/null 2> /dev/null
 then echo ""
 else
 echo "#include  #YOUR C++ TEMPLETE HERE
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int UINT;
int gcd( int a, int b ) {  if( !b ) return a;  return gcd( b, a % b ); }

int main()
{
       
 
 return 0;
} 
"  >> $1
 fi
fi
gedit $1 &
#Ignore line below, it is coming as blogger is treating headers as HTML tags :P


Step 2:
Open the .bashrc file of your system located in home directory
Amol@Dreamer-PC:~$ gedit .bashrc&

Step 3:
Add the following line in your .bashrc file
alias code="bash /home/amol/coding.sh"
Now 'code' will now appear as a command for the users to open the files :)

now whenever you will type
Amol@Dreamer-PC:~$ code myfile.c
it will open the new file in gedit with the headers already written in it.

similarly for '.cpp' files .
It will work for the existing files as well by opening the in gedit without modifying anything in it.

Now few lines explaining how Script works,first we are finding the extension of the using Shell Parameter Expansion ,then we are checking if the extension is '.c', if it is then then we are adding our template only if it is empty. Similar logic for '.cpp' extension,and finally we are opening the file using gedit as the background process.

You can make the changes in the script as per your requirements like the template or the editor or you may even add some more languages to it.

Hope all the linux coders will find this utility useful.


Saturday, December 17, 2011

Fun with Pidgin


Hi all,

Fun with pidgin....how ?.After reading this post you'll be able to play with your gmail chat status programitically.we'll be exploiting the xmpp protocol which Gtalk uses.

For Ubuntu(Linux) Users here's one of the way to do this. I am assuming you have installed pidgin on your system and it's working fine ( if you haven't.....downloading and install it...for any problem google it). After Installing Pidgin you need to install libpurple-bin.

For installing libpurple-bin from terminal -
$sudo apt-get install libpurple-bin.

or Ubuntu users can alternatively download the same using synaptic packet manager..

After this we just need a simple script to change your status message.i am sharing with you an script which i wrote...it will change you status and message every second. Isn't that cool ?....your name in your friend's chat list will change color from green to red and then red to yellow and then back to green in a cyclic way.
sleep_time=1
while test 1 = 1; do
msg1="Green"
msg2="Red"
msg3="Yellow"
if test "`purple-remote getstatus`" = "available"; then
purple-remote "setstatus?status=unavailable&message=${msg2}"
oldmsg="$msg"
elif test "`purple-remote getstatus`" = "unavailable"; then
purple-remote "setstatus?status=away&message=${msg3}"
oldmsg="$msg2"
else 
purple-remote "setstatus?status=available&message=${msg1}"
oldmsg="$msg3"
fi
sleep "${sleep_time}s"
done
save this file as status.sh now run this script in the terminal and just see the magic :D.
Sometimes this may be annoying for other's so you may increase the sleep_time.

Another Script for displaying time updated every second
sleep_time=1
while test 1 = 1; do
msg=`date`
if test "`purple-remote getstatus`" = "available"; then
purple-remote "setstatus?status=available&message=${msg}"
oldmsg="$msg"
fi
sleep "${sleep_time}s"
done
You can as do as many thing with as you can think bash script can do.........

Another way of doing that is using pyhton script using xmpppy library it will also not require libpurple-bin....but i haven't worked much on it so can't discuss that at this moment.

Edit : For exploring the python thing.....you can see this post by my friend Saurabh Singh .

Friday, December 16, 2011

Customizing Bootloader


Hi all,

I was looking for some cool stuff for Ubuntu...and then i found about burg....and believe me it's really good.BURG means brand-new unified grub loader.

If you are bored with your same boot-loader screen and are looking for some change then you should definitely try this..

for details about installation you can visit the link below

http://www.howtogeek.com/howto/45164/how-to-customize-the-ubuntu-bootloader-screen/

Enjoy the new Bootloader !! :)