【发布时间】:2017-07-10 14:27:31
【问题描述】:
下面的 ruby 代码通过Curses 打印两个窗口(重叠)。第一个“边框”窗口以黑色/青色打印,“内容”窗口以蓝色打印在青色上。
内容窗口仅显示打印文本的背景颜色。内容窗口的其余部分保持黑色。 ruby dox 描述了使用color_set、bkgd 或bkgdset 方法来操作窗口背景的方法。但是,我只能让 color_set() 工作,并且只能用于正在打印的文本:
如何使用适当的背景颜色填充内容窗口的重置?我找到了Set a window's background color in Ruby curses 的一些代码,但它似乎不起作用并且很旧。我唯一的另一个想法是用空格右填充字符串以用背景字符填充整个窗口,但这似乎很糟糕。
编辑:添加代码
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
【问题讨论】:
-
你有什么代码可以生成这个特定的窗口?如果它足够小以至于其他人可以迭代它并解决您的问题,那么它有助于将这种东西添加到问题中。
-
哎呀,把它拿出来编辑,忘了放回去。
-
正在使用哪个curses库(平台等)?在快速检查中,ncurses 按预期工作。对于其他实现,情况可能并非如此。
-
我不知道 Ruby gem 使用什么 curses 实现。我在 Ubuntu 14.04 上运行
gem install curses。gem list显示curses (1.2.3) -
win.bkgdset(Curses::color_pair(1)也应该绘制彩色背景