Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Print color highlighted strings to unix terminal

Sample of escape sequences needed to print in color to unix/linux terminal.

print x.replace('toostis', '\033[0;31mtoostis\033[m')


This will highlight all 'toostis' instances in x with red.

Bash title random Quote routine

#
#this is a script in my .bash_profile that gets a random
#quote .txt file and assigns it to the terminal title.
#

quote_path="/Users/glynch/Documents/quotes/*"
files=($quote_path)
N=${#files[@]}
((N=RANDOM%N))
randomfile=${files[$N]}
quote=""
while read line; do quote="$quote $line"; done < $randomfile
echo -n -e "\033]0;$quote\007"

Untill keypressed / conio.h

Turbo Pascal and conio.h allowed immediate character reading, without wainting for enter. Following code does the same under unix terminals:

#include <stdio.h>
#include <termios.h>

char getch(void) {
        char buf = 0;
        struct termios old = {0};
        if (tcgetattr(0, &old) < 0)
                perror("tcsetattr()");
        old.c_lflag &= ~ICANON;
        old.c_lflag &= ~ECHO;
        old.c_cc[VMIN] = 1;
        old.c_cc[VTIME] = 0;
        if (tcsetattr(0, TCSANOW, &old) < 0)
                perror("tcsetattr ICANON");
        if (read(0, &buf, 1) < 0)
                perror ("read()");
        old.c_lflag |= ICANON;
        old.c_lflag |= ECHO;
        if (tcsetattr(0, TCSADRAIN, &old) < 0)
                perror ("tcsetattr ~ICANON");
        return (buf);
}

int main() {
        char key;
        printf("Press 'x' to exit...\n");
        while((key=getch()) && (key != 'x'))
                printf ("you pressed %c\n", key);
        return(0);
}

Colorized text from php in unix terminals

Colorized text from php in unix terminals

# first define colors to use
$_colors = array(
        LIGHT_RED      => "[1;31m",
        LIGHT_GREEN     => "[1;32m",
        YELLOW         => "[1;33m",
        LIGHT_BLUE     => "[1;34m",
        MAGENTA     => "[1;35m",
        LIGHT_CYAN     => "[1;36m",
        WHITE         => "[1;37m",
        NORMAL         => "[0m",
        BLACK         => "[0;30m",
        RED         => "[0;31m",
        GREEN         => "[0;32m",
        BROWN         => "[0;33m",
        BLUE         => "[0;34m",
        CYAN         => "[0;36m",
        BOLD         => "[1m",
        UNDERSCORE     => "[4m",
        REVERSE     => "[7m",

);
##############################################
# Output colorized text to terminal run
# php scripts..
##############################################
function termcolored($text, $color="NORMAL", $back=1){
    global $_colors;
    $out = $_colors["$color"];
    if($out == ""){ $out = "[0m"; }
    if($back){
        return chr(27)."$out$text".chr(27)."[0m";#.chr(27);
    }else{
        echo chr(27)."$out$text".chr(27).chr(27)."[0m";#.chr(27);
    }//fi
}// end function
##############################################

Linux - XTerm font size

// Aumenta la dimensione del carattere sul terminale

xterm -geometry 640x480 -fn *-fixed-*-*-*-20-*

Run R from the command line

// This runs R from the command line in batch mode.
// The log file is output to track R's progress

R CMD BATCH --vanilla path/to/file.R file.log &

Fix Terminal spacing bug

defaults write com.apple.Terminal FontWidthSpacing 1.003
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS