如何在Ruby中轻松设置curses窗口的背景色?
下面的ruby代码通过如何在Ruby中轻松设置curses窗口的背景色?,ruby,curses,Ruby,Curses,下面的ruby代码通过Curses打印两个窗口(重叠)。第一个“边框”窗口以黑色/青色打印,“内容”窗口以蓝色青色打印 “内容”窗口仅显示打印文本的背景色。内容窗口的其余部分保持黑色。ruby dox描述了使用color\u set、bkgd或bkgdset方法操纵窗口背景的方法。我只能使color\u set()正常工作,但只能用于正在打印的文本: 如何用适当的背景色填充内容窗口的重置?我找到了一些代码,但它似乎不起作用,而且相当旧。我唯一的另一个想法是用空格右键填充字符串,以用背景字符填充
Curses
打印两个窗口(重叠)。第一个“边框”窗口以黑色/青色打印,“内容”窗口以蓝色青色打印
“内容”窗口仅显示打印文本的背景色。内容窗口的其余部分保持黑色。ruby dox描述了使用color\u set
、bkgd
或bkgdset
方法操纵窗口背景的方法。我只能使color\u set()
正常工作,但只能用于正在打印的文本:
如何用适当的背景色填充内容窗口的重置?我找到了一些代码,但它似乎不起作用,而且相当旧。我唯一的另一个想法是用空格右键填充字符串,以用背景字符填充整个窗口,但这似乎很老套
编辑:添加代码
EDIT2:添加了“黑客填充”解决方案
#!/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
好的,我发现,实际代码如下:
虽然已经有一段时间了,但也许我的例子仍然有用:
在使用时,它对我来说是相同的“钻石”
bkgd(COLOR_RED)这似乎出现了,因为bkgd方法
获取一个字符并将其打印到窗口的所有可用空间(请参见旧
博士)
但是,您可以使用具有所需背景的颜色对
在写其他东西之前,把它涂到所有的屏幕位置
我是这样解决的:
html#colors初始化我想我会使用hacky padding解决方案。似乎是目前为止我所发现的全部生成此特定窗口的代码是什么?这有助于将此类内容添加到问题中,如果它足够小,有意义,因为其他人可以对其进行迭代并修复您的问题。哎呀,将其拉出进行编辑,但忘记将其放回。正在使用哪个诅咒库(平台等)?在快速检查中,ncurses按预期工作。对于其他实现,情况可能并非如此。我不知道RubyGem使用的是什么诅咒实现。我在Ubuntu 14.04上运行了
gem安装诅咒<代码>宝石列表
显示了诅咒(1.2.3)
win.bkgdset(诅咒::颜色\u对(1)
也应该画一个彩色背景。谢谢!那篇文章实际上是在我的问题中链接的。感谢你的努力。
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
# 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)