【问题标题】:How can I easily set the curses window background color in Ruby?如何在 Ruby 中轻松设置 curses 窗口背景颜色?
【发布时间】: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) 也应该绘制彩色背景

标签: ruby curses


【解决方案1】:

好的,所以我找到了this,实际代码在这里:

已经有一段时间了,但也许我的例子仍然有用:

使用时对我来说是相同的“钻石”

window.bkgd(COLOR_RED) 这个好像出现了,因为bkgd方法 获取一个字符并将其打印到窗口的所有空闲空间(参见旧 文档)。

但是,您可以使用具有所需背景的颜色对 在编写其他内容之前,将颜色应用到所有屏幕位置。

我是这样解决的:

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#colors-initialization

【讨论】:

  • 谢谢!但是,该帖子实际上与我的问题有关。感谢您的努力。
【解决方案2】:

我想我会使用 hacky 填充解决方法。到目前为止似乎是我发现的全部

【讨论】:

    猜你喜欢
    • 2013-10-26
    • 2021-10-02
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2011-01-07
    相关资源
    最近更新 更多