如何在 Ruby 中轻松设置 curses window 背景颜色?
How can I easily set the curses window background color in Ruby?
下面的 ruby 代码通过 Curses
打印两个 windows(重叠)。第一个 "border" window 在 black/cyan 中打印,"content" window 在青色上以蓝色打印。
内容window只显示打印文本的背景颜色。其余内容 window 保持黑色。 ruby dox 描述了使用 color_set
、bkgd
或 bkgdset
方法操纵 window 背景的方法。然而,我只能让 color_set()
工作,并且只能用于正在打印的文本:
如何用适当的背景颜色填充内容 window 的重置?我找到了 Set a window's background color in Ruby curses 的一些代码,但它似乎不起作用并且很旧。我唯一的另一个想法是用空格右填充字符串以用背景字符填充整个 window 但这看起来很老套。
编辑:添加代码
EDIT2:添加了"hacky padding"解决方法
#!/usr/bin/ruby
require 'curses'
Curses.init_screen
Curses.start_color
Curses.noecho
Curses.cbreak
Curses.refresh # Refresh the screen
xulc = 10
yulc = 10
width = 30
height = 8
# text color for border window
Curses.init_pair(1, Curses::COLOR_BLACK, Curses::COLOR_CYAN)
Curses.attrset(Curses.color_pair(1) | Curses::A_BOLD)
# Text color for content window
Curses.init_pair(2, Curses::COLOR_BLUE, Curses::COLOR_CYAN)
Curses.attrset(Curses.color_pair(2) | Curses::A_NORMAL)
# border window
win1 = Curses::Window.new(height, width, yulc, xulc)
win1.color_set(1)
win1.box("|", "-")
# content window
win2 = Curses::Window.new(height - 2, width - 2, yulc + 1, xulc + 1)
win2.color_set(2)
win2.setpos(0, 0)
# only prints in background color where there is text!
# add hacky padding to fill background then go back and print message
bg_padding = " " * ((width - 2) * (height - 2));
win2.addstr(bg_padding);
win2.setpos(0, 0)
win2.addstr("blah")
# prints without the color_set() attributes
#win2.bkgd ('.'.ord)
# make content visisble
win1.refresh
win2.refresh
# hit a key to exit curses
Curses.getch
Curses.close_screen
好的,所以我找到了this,实际代码在这里:
It's been a while, but maybe my examples are still useful:
It is the same "diamonds" for me when using
window.bkgd(COLOR_RED) This seems to appear, because the bkgd method
takes a char and prints it to all free spaces of the window (see old
doc).
However, then you can use a color pair with the wanted background
color and apply it to all screen positions before writing oher stuff.
Here is how I solved it:
require 'curses'
init_screen
start_color
init_pair(COLOR_RED, COLOR_WHITE, COLOR_RED)
window = Curses::Window.new(0, 0, 0, 0)
window.attron(color_pair(COLOR_RED)) do
lines.times do |line|
window.setpos(line, 0)
window << ' ' * cols
end
end
还发现this:
# color_set(col)
# Sets the current color of the given window to the foreground/background
# combination described by the Fixnum col.
main_window.color_set(1)
tutorial.html#颜色初始化
我猜我会使用 hacky padding 解决方法。似乎是我目前所发现的全部
下面的 ruby 代码通过 Curses
打印两个 windows(重叠)。第一个 "border" window 在 black/cyan 中打印,"content" window 在青色上以蓝色打印。
内容window只显示打印文本的背景颜色。其余内容 window 保持黑色。 ruby dox 描述了使用 color_set
、bkgd
或 bkgdset
方法操纵 window 背景的方法。然而,我只能让 color_set()
工作,并且只能用于正在打印的文本:
如何用适当的背景颜色填充内容 window 的重置?我找到了 Set a window's background color in Ruby curses 的一些代码,但它似乎不起作用并且很旧。我唯一的另一个想法是用空格右填充字符串以用背景字符填充整个 window 但这看起来很老套。
编辑:添加代码
EDIT2:添加了"hacky padding"解决方法
#!/usr/bin/ruby
require 'curses'
Curses.init_screen
Curses.start_color
Curses.noecho
Curses.cbreak
Curses.refresh # Refresh the screen
xulc = 10
yulc = 10
width = 30
height = 8
# text color for border window
Curses.init_pair(1, Curses::COLOR_BLACK, Curses::COLOR_CYAN)
Curses.attrset(Curses.color_pair(1) | Curses::A_BOLD)
# Text color for content window
Curses.init_pair(2, Curses::COLOR_BLUE, Curses::COLOR_CYAN)
Curses.attrset(Curses.color_pair(2) | Curses::A_NORMAL)
# border window
win1 = Curses::Window.new(height, width, yulc, xulc)
win1.color_set(1)
win1.box("|", "-")
# content window
win2 = Curses::Window.new(height - 2, width - 2, yulc + 1, xulc + 1)
win2.color_set(2)
win2.setpos(0, 0)
# only prints in background color where there is text!
# add hacky padding to fill background then go back and print message
bg_padding = " " * ((width - 2) * (height - 2));
win2.addstr(bg_padding);
win2.setpos(0, 0)
win2.addstr("blah")
# prints without the color_set() attributes
#win2.bkgd ('.'.ord)
# make content visisble
win1.refresh
win2.refresh
# hit a key to exit curses
Curses.getch
Curses.close_screen
好的,所以我找到了this,实际代码在这里:
It's been a while, but maybe my examples are still useful:
It is the same "diamonds" for me when using
window.bkgd(COLOR_RED) This seems to appear, because the bkgd method takes a char and prints it to all free spaces of the window (see old doc).
However, then you can use a color pair with the wanted background color and apply it to all screen positions before writing oher stuff.
Here is how I solved it:
require 'curses'
init_screen
start_color
init_pair(COLOR_RED, COLOR_WHITE, COLOR_RED)
window = Curses::Window.new(0, 0, 0, 0)
window.attron(color_pair(COLOR_RED)) do
lines.times do |line|
window.setpos(line, 0)
window << ' ' * cols
end
end
还发现this:
# color_set(col)
# Sets the current color of the given window to the foreground/background
# combination described by the Fixnum col.
main_window.color_set(1)
tutorial.html#颜色初始化
我猜我会使用 hacky padding 解决方法。似乎是我目前所发现的全部