简体   繁体   English

如何在Ruby中轻松设置curses窗口背景颜色?

[英]How can I easily set the curses window background color in Ruby?

The ruby code below prints two windows (overlapping) via Curses . 下面的ruby代码通过Curses打印两个窗口(重叠)。 The first "border" window prints in black/cyan and the "content" window prints in blue on cyan. 第一个“边框”窗口以黑色/青色打印,“内容”窗口以青色打印蓝色。

The content window only displays the background color where text is printed. 内容窗口仅显示打印文本的背景色。 The rest of the content window remains black. 内容窗口的其余部分保持黑色。 The ruby dox describe ways to manipulate window backgrounds using either color_set , bkgd or bkgdset methods. ruby dox描述了使用color_setbkgdbkgdset方法处理窗口背景的方法。 I can only get color_set() to work however and only for text that is being printed: 我只能使color_set()可以工作,并且只能用于正在打印的文本:

在此处输入图片说明

How can I fill the reset of the content window with the appropriate background color? 如何用适当的背景色填充内容窗口的重置? I found some code to Set a window's background color in Ruby curses but it does not seem to work and is quite old. 在Ruby curses中找到了一些代码来设置窗口的背景色,但是它似乎不起作用,而且已经很老了。 The only other idea I have is to right-pad the string with spaces to fill the entire window with the background character but this seems reeealy hacky. 我唯一的另一个想法是用空格右击该字符串,以用背景字符填充整个窗口,但这似乎实在是太过分了。

EDIT: added code 编辑:添加代码

EDIT2: added "hacky padding" work around 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

Ok, so I found this , the actual code is here: 好的,所以我找到 ,实际的代码在这里:

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). window.bkgd(COLOR_RED)似乎出现,因为bkgd方法采用一个char并将其打印到窗口的所有可用空间中(请参阅旧文档)。

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

Also found 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 tutorial.html#颜色初始化

Guess I'll use the hacky padding workaround. 猜猜我将使用hacking padding解决方法。 Seems to be all I've found so far 似乎是我到目前为止所发现的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM