How to change color of pry prompt string - ruby

I'm using different bash prompt string color in my server and local computer, so that I can easily be aware of where I am.
But when I use pry in server there is only white and black.
Is there a way to colorize prompt string of pry?

As an easy way, you can just:
Pry.config.prompt_name = "\e[0;31m#{Rails.env} \e[0m" # 31 means red
Pry.config.prompt_name = "\e[0;32m#{Rails.env} \e[0m" # 32 means green
You will see something like:
[2] production (main)>
^ red or green

There's this gem called pry-theme. Have a look.
Install using gem install pry-theme

Related

Cmder wrong colors using Windows Terminal

I am trying to use Cmder in Windows Terminal. I tried following this guide, and I did everything as it says.
However, there is a small issue. No matter what I do, the prompt background colour does not change, it stays black.
I couldn't figure out the issue. Any suggestions?
In the comments section of the same article
I ran into this issue as well and was able to get it working. In your
"%cmder_root%\config" directory, create a file called "my_prompt.lua"
and add the following to it:
function my_prompt_filter()
cwd = clink.get_cwd()
prompt = "\x1b[1;32;49m{cwd} {git}{hg}{svn} \n\x1b[1;39;49m{lamb} \x1b[0m"
new_value = string.gsub(prompt, "{cwd}", cwd)
clink.prompt.value = string.gsub(new_value, "{lamb}", "λ")
end
clink.prompt.register_filter(my_prompt_filter, 1)
Kudos to Eric Grandt
#AMagyar 's answer is great, except that if you use conda, pyenv or other virtual environments, that information would be omitted (when it should be (base) λ, you will get λ). Instead, you can create my_prompt.lua as something like:
function my_prompt_filter()
local prompt = clink.prompt.value
prompt = string.gsub(prompt, '^\x1b%[1;32;40m', '\x1b[1;32;49m')
prompt = string.gsub(prompt, '\n\x1b%[1;39;40m', '\n\x1b[1;39;49m')
clink.prompt.value = prompt
end
clink.prompt.register_filter(my_prompt_filter, 1)
Everything is settled.
What controls the terminal text & background color?
In function set_prompt_filter in %cmder_root%\vender\clink.lua, you may read lines like:
local cmder_prompt = "\x1b[1;32;40m{cwd} {git}{hg}{svn} \n\x1b[1;39;40m{lamb} \x1b[0m"
This is the prototype of cmder prompts. The {cwd}, {git}, {lamb}, etc, are to be substituted with the actual content later on. The \x1b[1;32;40m is the ANSI escape sequence that controls the color of following text. 32 means green text color, 40 means black background color, 39 means default text color, and 49 means default background color.
Why was pyenv/conda environment omitted?
Also in function set_prompt_filter in %cmder_root%\vender\clink.lua, you may find how cmder added the information of virtual environments into {lamb} (or more specifically, prompts with () or []). So either you have to retrieve that information from the original prompt, or simply just replace the color codes, as in this answer.

Change Bash terminal output color while running a Ruby script

I'd like to briefly change my terminal output color, run a Ruby script so that standard output prints in that changed color, 'sleep' for a second, and then change it back. I know how to set colors, like for the prompt:
PS1="\e[0;36m[\w] \e[m "
I imagine I need to write a Bash function to do this. What would that look like?
Here is a Ruby script to show all the terminal colors. Download it or run the code below.
def color(index)
normal = "\e[#{index}m#{index}\e[0m"
bold = "\e[#{index}m\e[1m#{index}\e[0m"
"#{normal} #{bold} "
end
8.times do|index|
line = color(index + 1)
line += color(index + 30)
line += color(index + 90)
line += color(index + 40)
line += color(index + 100)
puts line
end
You can do it within Ruby (assuming you're on Linux; Windows requires a library/gem whose name I can't remember at the moment) using the normal codes you would use in bash, e.g.
puts "\e[31m etc Your text here."
To reset to normal display:
puts "\e[0m"
Adjust to taste.
You can also use the Term Ansicolor gem to change it from inside a running script.
http://flori.github.io/term-ansicolor/
One may also use the Colorize gem.
Installation:
sudo gem install colorize
Usage:
require 'colorize'
puts "I am now red.".red
puts "I am now blue.".green
puts "I am a super coder".yellow
This answer is copied from How can I use Ruby to colorize the text output to a terminal?.

Vim option value contains error message when editing ruby files, Gem.all_load_paths

When editing ruby, some files but not all are garbled when editing in vim.
Inspecting the options with :set, I discovered that there is some magic done to produce one of the option values, and something went wrong with the magic and there is an error or warning message where the option value should be. That may be causing side effects.
The method throwing the error is Gem.all_load_paths, and it happens regardless of whether I use ruby 1.8.7, 1.9.2, whether I use rvm or system ruby. Using Ubuntu 11.10
I've tried setting the omnifunc option to nil, but that doesn't solve the problem; it seems to be a different option producing this value. The plugins I'm using can be seen below.
:set
--- Options ---
autoindent comments=:# history=50 keywordprg=ri scroll=29 suffixesadd=.rb ttyfast
backup filetype=ruby hlsearch mouse=a shiftwidth=2 syntax=ruby ttymouse=xterm2
backupdir=~/.tmp helplang=en incsearch ruler showcmd tabstop=2
backspace=indent,eol,start
balloonexpr=RubyBalloonexpr()
commentstring=# %s
fileencodings=ucs-bom,utf-8,default,latin1
formatoptions=croql
include=^\s*\<\(load\|w*require\)\>
includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
indentexpr=GetRubyIndent()
indentkeys=0{,0},0),0],!^F,o,O,e,=end,=elsif,=when,=ensure,=rescue,==begin,==end
omnifunc=rubycomplete#Complete f
rom ~/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/rubygems/deprecate.rb:62:in `all_load_paths'^#^Ifrom -e:1^#1.8/rubygems/deprecate.rb:62:in `send'^#^I
printoptions=paper:letter /
after,/var/lib/vim/addons/after,~/.vim/afterm,~/.vim/bundle/vim-rails,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim73,/usr/share/vim/vimfiles
suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
I got the same thing today after updating from Snapshot 63 to Snapshot 64 of MacVim. It's an ugly hack, but I found the offending line (79) in MacVim.app/Contents/Resources/vim/runtime/ftplugin/ruby.vim and removed the reference to Gem.all_load_paths, which is deprecated with no replacement.
I've posted a diff of my gist that gets me going. It's not clear to me at this point if this is a bug to be reported against MacVim or Vim.
I built on #pbyme a little bit and actually found the new way to do it. With my current setup, it's even pulling in some local gems brought in by bundler.
The key is replacing all_load_paths with Specification.map(&:lib_dirs_glob)
MacVim.app/Contents/Resources/vim/runtime/ftplugin/ruby.vim
79c79
< let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
---
> let s:code = "print ($: + begin; require %q{rubygems}; Gem::Specification.map(&:lib_dirs_glob).sort.uniq; rescue LoadError; []; end).join(%q{,})"

Better ruby terminal coloring library

There are plenty of coloring libraries: colored, term-ansicolor.
But is there any which can do this:
puts "#{'hello'.red} world!".bold
And world! should be bold.
To make it clear, I want to get this:
"\e[1m\e[31mhello\e[0m\e[1m world!\e[0m"
or better even this (just shorter):
"\e[1;31mhello\e[0;1m world!\e[0m"
instead of this:
"\e[1m\e[31mhello\e[0m world!\e[0m"
As there is none, I wrote my own, with blackjack and hookers smart one — smart_colored
gem install smart_colored
and run
require 'smart_colored/extend'
# without extend you'll need to use 'string'.colored.red
puts "#{'hello'.red} world!".bold
The lib is called Highline
It has a color method: say("This should be <%= color('bold', BOLD) %>!")
Which can be easily used to implement String#bold.
In case you are using highline I made a gem that extends colors and provides helpers such as:
say_bold 'this is bold text'
https://github.com/bonzofenix/highline-color

Why does Ruby's Win32Console not work for me with Ramaze?

I have installed Ramaze (on Windows XP) and it suggested I also install win32console to get coloured log output when it is running.
However, after doing so I get escape codes rather than colours as shown below:
W [2009-04-29 09:02:55 $5064] WARN | : ←[33mNo explicit root folder found, assuming it is C:/Projects/Ruby/Ramaze/Conferences←[0m
D [2009-04-29 09:02:55 $5064] DEBUG | : ←[34mUsing webrick←[0m
I [2009-04-29 09:02:55 $5064] INFO | : ←[37mWEBrick 1.3.1←[0m
I [2009-04-29 09:02:55 $5064] INFO | : ←[37mruby 1.8.6 (2008-08-11) [i386-mswin32]←[0m
D [2009-04-29 09:02:55 $5064] DEBUG | : ←[34mTCPServer.new(0.0.0.0, 7000)←[0m
D [2009-04-29 09:02:55 $5064] DEBUG | : ←[34mRack::Handler::WEBrick is mounted on /.←[0m
I [2009-04-29 09:02:55 $5064] INFO | : ←[37mWEBrick::HTTPServer#start: pid=5064 port=7000←[0m
This happens even on a clean install of Ruby/Ramaze/win32console
My setup is:
Windows XP with SP#3
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
rubygems version 1.3.1
win32console gem version 1.2.0
Incidentally, the following test program seems to work so I am wondering if it is a Ramaze/win32console issue on my machine.
#!/usr/bin/ruby
require 'rubygems'
require 'win32console'
[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
Maybe it's not an answer you are looking for, but I use MSYS/MinGW on Windows and it's bash displays colors properly without win32console gem.
Try
require 'rubygems'
require 'win32console'
include Win32::Console::ANSI
include Term::ANSIColor
I think this is caused by the difference in the formatting string between your code and the code in Ramaze (or Innate).
"\e[#{COLOR_CODE[LEVEL_COLOR[severity]]}m#{string}\e[0m"
That expands to (for red, and "Hello" being the String):
"\e[31mHello\e[0m"
\e, in Ruby, is converted to \033, so that shouldn't give us trouble:
of course, is equivalent being equivalent with \033
"\033" == "\e" # => true
The major difference is that Ramaze doesn't specify a background-color, as that may result in ugly output if it contrasts too much with the default background of the terminal. But as it may just as well be completely unreadable if your background is blue, red, white, or any of the other foreground colors that Ramaze uses for logging, I think we should define the background explicitly, and hope that this will also fix your windows issue.
try adding this in your app.rb
require "win32console"
require "Win32/Console/ANSI"
win32console needs to be required before Ramaze start.
This is because Ramaze and its logging mechanism are keeping references to stdout and stderr prior win32console replace them.
I just tried the suggestion in How to load ANSI escape codes or get coloured file listing in WinXP cmd shell and this worked nicely.
Basically, I grabbed ANSICon and then ran:
ansicon.exe -I (that's a capital 'i' there)
from the x86 sub-folder it was extracted to. This installed the ANSI colour code DLL as hook to all cmd.exe windows opened after that. Yo and behold - colours in cmd.exe

Resources