红宝石诅咒颜色

发布于 2024-12-03 04:40:13 字数 1050 浏览 0 评论 0原文

如何使用 Ruby 的默认 Curses 库获取颜色?

所以类似:

puts "\e[0m\e[30;47mtest\e[0m"

效果很好。在浅灰色背景上呈现漂亮的黑色。但这:

#!/usr/bin/env ruby

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (for pageup/down)
Curses.stdscr.nodelay = 1

Curses.clear
Curses.setpos(0,0)
Curses.addstr "Hello!"
Curses.addstr "\e[0m\e[30;47mHello!\e[0m"

显示:

你好!

^[[0米^[[30;47米你好!^[[0米

最终,我需要为此的颜色:

如何在 Ruby 中捕获按键操作?

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)

loop do
  case Curses.getch
  when Curses::Key::PPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Up")
  when Curses::Key::NPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Dn")
  end
end

How can I get colors with Ruby's default Curses library?

So something like:

puts "\e[0m\e[30;47mtest\e[0m"

works great. Gives a nice black on light gray background. But this:

#!/usr/bin/env ruby

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (for pageup/down)
Curses.stdscr.nodelay = 1

Curses.clear
Curses.setpos(0,0)
Curses.addstr "Hello!"
Curses.addstr "\e[0m\e[30;47mHello!\e[0m"

Displays:

Hello!

^[[0m^[[30;47mHello!^[[0m

Ultimately, I need colors for this:

How to capture a key press in Ruby?

require 'curses'

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)

loop do
  case Curses.getch
  when Curses::Key::PPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Up")
  when Curses::Key::NPAGE
    Curses.setpos(0,0)
    Curses.addstr("Page Dn")
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一刻暧昧 2024-12-10 04:40:13

ruby源代码中有curses用法的示例,请参见,例如此处

你的代码可能是这样的:

require 'curses'
include Curses

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)
Curses.start_color
# Determines the colors in the 'attron' below
Curses.init_pair(COLOR_BLUE,COLOR_BLUE,COLOR_BLACK) 
Curses.init_pair(COLOR_RED,COLOR_RED,COLOR_BLACK)

loop do

  case Curses.getch

  when Curses::Key::PPAGE
    Curses.clear
    Curses.setpos(0,0)
    # Use colors defined color_init
    Curses.attron(color_pair(COLOR_RED)|A_NORMAL){
      Curses.addstr("Page Up")
    }
  when Curses::Key::NPAGE
    Curses.clear
    Curses.setpos(0,0)
    Curses.attron(color_pair(COLOR_BLUE)|A_NORMAL){
      Curses.addstr("Page Down")
    }
  end
end

There are examples of curses usage in the ruby source code, see, e.g. here.

Your code could be something like:

require 'curses'
include Curses

Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys (required for pageup/down)
Curses.start_color
# Determines the colors in the 'attron' below
Curses.init_pair(COLOR_BLUE,COLOR_BLUE,COLOR_BLACK) 
Curses.init_pair(COLOR_RED,COLOR_RED,COLOR_BLACK)

loop do

  case Curses.getch

  when Curses::Key::PPAGE
    Curses.clear
    Curses.setpos(0,0)
    # Use colors defined color_init
    Curses.attron(color_pair(COLOR_RED)|A_NORMAL){
      Curses.addstr("Page Up")
    }
  when Curses::Key::NPAGE
    Curses.clear
    Curses.setpos(0,0)
    Curses.attron(color_pair(COLOR_BLUE)|A_NORMAL){
      Curses.addstr("Page Down")
    }
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文