ansi | Dmytro Shteflyuk's Home https://kpumuk.info In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming. Tue, 08 Sep 2015 00:15:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Customizing iTerm. Creating a display profile with pastel colors https://kpumuk.info/mac-os-x/customizing-iterm-creating-a-display-profile-with-pastel-colors/ https://kpumuk.info/mac-os-x/customizing-iterm-creating-a-display-profile-with-pastel-colors/#comments Mon, 04 May 2009 15:09:40 +0000 http://kpumuk.info/?p=645 Last week I have posted an article on how to write ssh host name on the iTerm’s background. I have been looking for something like this for a while, and this is that killer feature, which forced me to switch to iTerm from classic Terminal app. Here I will show what settings I have tuned […]

The post Customizing iTerm. Creating a display profile with pastel colors first appeared on Dmytro Shteflyuk's Home.]]>
iTerm Last week I have posted an article on how to write ssh host name on the iTerm’s background. I have been looking for something like this for a while, and this is that killer feature, which forced me to switch to iTerm from classic Terminal app. Here I will show what settings I have tuned to get iTerm more comfortable to use as for me.

First thing necessary to do is enabling UTF-8 support. If you create folders in Finder with extended characters (e.g. Russian, Japanese, etc.) iTerm will only show question marks instead of the real characters. To fix this you just have to set the LANG environment variable of the shell (put this into the ~/.bash_profile):

1
export LANG=ru_RU.UTF-8

The next time you will start an iTerm session the variable will be set and unicode characters will be shown correctly.

A list of all supported UTF-8 locales could be retrieved with the command:

1
locale -a | grep UTF-8

Next thing is to fix an input problem (from iTerm FAQ). If you can’t input Chinese/Japanese/Umlauts/Accents, put this into your ~/.bash_profile:

1
2
3
4
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off

Ok, everything works now and all I want is to migrate my color scheme options from Terminal. I love dark color scheme with pastel ANSI colors theme:

iTerm pastel display profile example

There is a great feature of iTerm called bookmarks. You can create display, keyboard, and terminal profiles, and then add bookmarks with these profiles associated to them. Each bookmark opens a terminal window with specified command in it. By default iTerm creates a default profile with something like login -fp kpumuk (for my machine).

I’ve created a new display profile called “Pastel” with nice pastel colors which replace default ANSI colors in terminal (click to enlarge):

Pastel display profile

I think I’m not alone in my pastel wishes, so here is the script, which will import Pastel display profile to your iTerm, and will assign it to the Default bookmark. Please note: you should run this script from Terminal.app, and do not forget to quit from iTerm before running, because settings will not be applied in this case!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash

PASTEL='{
    "Ansi 0 Color" =         {
        "Blue Component" = 0.3097887;
        "Green Component" = 0.3097887;
        "Red Component" = 0.3097887;
    };
    "Ansi 1 Color" =         {
        "Blue Component" = 0.3764706;
        "Green Component" = 0.4235294;
        "Red Component" = 1;
    };
    "Ansi 10 Color" =         {
        "Blue Component" = 0.6727703;
        "Green Component" = 1;
        "Red Component" = 0.8094148;
    };
    "Ansi 11 Color" =         {
        "Blue Component" = 0.7996491;
        "Green Component" = 1;
        "Red Component" = 1;
    };
    "Ansi 12 Color" =         {
        "Blue Component" = 0.9982605;
        "Green Component" = 0.8627756;
        "Red Component" = 0.7116503;
    };
    "Ansi 13 Color" =         {
        "Blue Component" = 0.9965209;
        "Green Component" = 0.6133059;
        "Red Component" = 1;
    };
    "Ansi 14 Color" =         {
        "Blue Component" = 0.9970397;
        "Green Component" = 0.8763103;
        "Red Component" = 0.8759136;
    };
    "Ansi 15 Color" =         {
        "Blue Component" = 1;
        "Green Component" = 1;
        "Red Component" = 1;
    };
    "Ansi 2 Color" =         {
        "Blue Component" = 0.3764706;
        "Green Component" = 1;
        "Red Component" = 0.6588235;
    };
    "Ansi 3 Color" =         {
        "Blue Component" = 0.7137255;
        "Green Component" = 1;
        "Red Component" = 1;
    };
    "Ansi 4 Color" =         {
        "Blue Component" = 0.9960784;
        "Green Component" = 0.7960784;
        "Red Component" = 0.5882353;
    };
    "Ansi 5 Color" =         {
        "Blue Component" = 0.9921569;
        "Green Component" = 0.4509804;
        "Red Component" = 1;
    };
    "Ansi 6 Color" =         {
        "Blue Component" = 0.9960784;
        "Green Component" = 0.772549;
        "Red Component" = 0.7764706;
    };
    "Ansi 7 Color" =         {
        "Blue Component" = 0.9335317;
        "Green Component" = 0.9335317;
        "Red Component" = 0.9335317;
    };
    "Ansi 8 Color" =         {
        "Blue Component" = 0.4862745;
        "Green Component" = 0.4862745;
        "Red Component" = 0.4862745;
    };
    "Ansi 9 Color" =         {
        "Blue Component" = 0.6901961;
        "Green Component" = 0.7137255;
        "Red Component" = 1;
    };
    "Anti Alias" = 1;
    "Background Color" =         {
        "Blue Component" = 0;
        "Green Component" = 0;
        "Red Component" = 0;
    };
    Blur = 1;
    "Bold Color" =         {
        "Blue Component" = 0.5067359;
        "Green Component" = 0.5067359;
        "Red Component" = 0.9909502;
    };
    Columns = 120;
    "Cursor Color" =         {
        "Blue Component" = 0.3764706;
        "Green Component" = 0.6470588;
        "Red Component" = 1;
    };
    "Cursor Text Color" =         {
        "Blue Component" = 1;
        "Green Component" = 1;
        "Red Component" = 1;
    };
    "Disable Bold" = 0;
    Font = "Monaco 14";
    "Foreground Color" =         {
        "Blue Component" = 1;
        "Green Component" = 1;
        "Red Component" = 1;
    };
    "Horizontal Character Spacing" = 1;
    NAFont = "Monaco 14";
    Rows = 24;
    "Selected Text Color" =         {
        "Blue Component" = 0.9476005;
        "Green Component" = 0.9476005;
        "Red Component" = 0.9476005;
    };
    "Selection Color" =         {
        "Blue Component" = 0.5153061;
        "Green Component" = 0.2224857;
        "Red Component" = 0.2099074;
    };
    Transparency = 0.1;
    "Vertical Character Spacing" = 1;
}'


# Add display profile
defaults write net.sourceforge.iTerm Displays -dict-add Pastel "$PASTEL"
echo "Pastel display profile added"

# Set the default display profile
BOOKMARKS=`defaults read net.sourceforge.iTerm Bookmarks | sed 's/\("Display Profile" = \)"[^"]*";/\1"Pastel";/'`
defaults write net.sourceforge.iTerm Bookmarks "$BOOKMARKS"
echo "Pastel display profile installed as default"

You can download this script from GitHub here.

Hope, you will find this post useful. Fill free to post your comments and suggestions!

Changelog

2011-08-09 — updated gist URL and install command.

2010-07-13 — fixed bug which was making iTerm totally unusable in some cases.

The post Customizing iTerm. Creating a display profile with pastel colors first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/mac-os-x/customizing-iterm-creating-a-display-profile-with-pastel-colors/feed/ 14
Colorizing console Ruby-script output https://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/ https://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/#comments Fri, 23 Mar 2007 16:28:06 +0000 http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/ Very often I have to implement console scripts (because of my laziness, for boring processes optimization). Many of them write some information to the output, show process status or display results of work. Anyway, it’s very wearisome action to read script output, and I want to highlight most important things: errors in red, successfully finished […]

The post Colorizing console Ruby-script output first appeared on Dmytro Shteflyuk's Home.]]>
Very often I have to implement console scripts (because of my laziness, for boring processes optimization). Many of them write some information to the output, show process status or display results of work. Anyway, it’s very wearisome action to read script output, and I want to highlight most important things: errors in red, successfully finished steps in green color, etc. And it is a case when ANSI escape sequences could help. They are supported by the most terminals, including VT100 (btw, Windows NT family console does not support it, but I will back to this issue later).

For the beginning, let’s examine ANSI escape sequence structure. It starts with ESC symbol (ASCII-code 27), following by left square bracket [. There are one or more numbers separated by semicolon ; with a letter at the end appear after it.

I will not describe all possible codes, anybody who wishes could find them in Wikipedia. The sequence with m letter at the end is used to change foreground and background colors. In general situation it looks like: ESC[31m, where 31 sets red color as foreground. Here the table with codes which supported by most terminals:

Code Effect
0 Turn off all attributes
1 Set bright mode
4 Set underline mode
5 Set blink mode
7 Exchange foreground and background colors
8 Hide text (foreground color would be the same as background)
30 Black text
31 Red text
32 Green text
33 Yellow text
34 Blue text
35 Magenta text
36 Cyan text
37 White text
39 Default text color
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Magenta background
46 Cyan background
47 White background
49 Default background color

As you could see from the table, you are able to set foreground and background colors separately, also you could combine them into one sequence (for example, ESC[1;33;44m – bright yellow text on blue background).

Attention: Don’t forget to turn off all attributes before exit, otherwise all following text would be displayed with your attributes.

That’s enough of the theory, let’s examine example:

1
2
3
4
# Actual work
puts "Importing categories [ e[32mDONEe[0m ]"
# Actual work
puts "Importing tags       [e[31mFAILEDe[0m]"

As the result you will see something like following:

VT100 Example 1

All my life I used codes just like shown in the previous example, but not so long ago I found simple helpers when delving in the RSpec sources:

1
2
3
4
5
6
7
8
9
10
11
def colorize(text, color_code)
  "#{color_code}#{text}e[0m"
end

def red(text); colorize(text, "e[31m"); end
def green(text); colorize(text, "e[32m"); end

# Actual work
puts 'Importing categories [ ' + green('DONE') + ' ]'
# Actual work
puts 'Importing tags       [' + red('FAILED') + ']'

It’s a good idea, and now I’m using it. And recommend it to you :-)

Now about sorrowful things. Windows XP (and as far as I remember, Windows 2000 too) does not support ANSI escape sequences. If you are love perversions, look at the Command Interpreter Ansi Support article. Others could stay here and look, how to solve problem using Ruby facilities.

You should install win32console first:

1
gem install win32console

Now add following lines at the beginning of your script (and again, I found them in RSpec):

1
2
3
4
5
begin
  require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
rescue LoadError
  raise 'You must gem install win32console to use color on Windows'
end

Script output will be colorized both on windows and Unix systems.

And in the end I will show full table of different codes, which you could use in your scripts:

VT100 Terminal

It has been obtained using following script:

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/ruby

[0, 1, 4, 5, 7].each do |attr|
  puts '----------------------------------------------------------------'
  puts "ESC[#{attr};Foreground;Background"
  30.upto(37) do |fg|
    40.upto(47) do |bg|
      print "\033[#{attr};#{fg};#{bg}m #{fg};#{bg}  "
    end
  puts "\033[0m"
  end
end

Updated 06/10/2010: Replaced PLATFORM constant with the RUBY_PLATFORM (thanks to Ian Alexander Wood).

The post Colorizing console Ruby-script output first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/feed/ 10