展示 HN:RatatuiRuby 将 Rust Ratatui 封装为 RubyGem – 使用 Ruby 的 TUI。
Show HN: RatatuiRuby wraps Rust Ratatui as a RubyGem – TUIs with the joy of Ruby

原始链接: https://www.ratatui-ruby.dev/

内联视口标准 TUI 在退出时会擦除自身。你精心格式化的 CLI 输出会消失。用户会丢失他们的滚动历史。内联视口解决了这个问题。它们占据固定数量的行,渲染丰富的 UI,然后在完成时将输出保留在原位。非常适合微调器、菜单、进度指示器——任何短暂的丰富时刻。 class Spinner def main RatatuiRuby.run(viewport: :inline, height: 1) do |tui| until connected? status = tui.paragraph(text: "#{spin} 连接中...") tui.draw { |frame| frame.render_widget(status, frame.area) } return ending(tui, "已取消!", :red) if tui.poll_event.ctrl_c? end ending(tui, "已连接!", :green) end end def ending(tui, message, color) = tui.draw do |frame| frame.render_widget(tui.paragraph(text: message, fg: color), frame.area) end def initialize = (@frame, @finish = 0, Time.now + 2) def connected? = Time.now >= @finish def spin = SPINNER[(@frame += 1) % SPINNER.length] SPINNER = %w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏] end Spinner.new.main; puts

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 展示 HN: RatatuiRuby 将 Rust Ratatui 封装为 RubyGem – 使用 Ruby 的 TUI (ratatui-ruby.dev) 10 分,Kerrick 1 小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 aaronbrethorst 16 分钟前 [–] 太棒了,Kerrick 干得好!回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Inline Viewports

Standard TUIs erase themselves on exit. Your carefully formatted CLI output disappears. Users lose their scrollback.

Inline viewports solve this. They occupy a fixed number of lines, render rich UI, then leave the output in place when done.

Perfect for spinners, menus, progress indicators—any brief moment of richness.

class Spinner
  def main
    RatatuiRuby.run(viewport: :inline, height: 1) do |tui|
      until connected?
        status = tui.paragraph(text: "#{spin} Connecting...")
        tui.draw { |frame| frame.render_widget(status, frame.area) }
        return ending(tui, "Canceled!", :red) if tui.poll_event.ctrl_c?
      end
      ending(tui, "Connected!", :green)
    end
  end

  def ending(tui, message, color) = tui.draw do |frame|
    frame.render_widget(tui.paragraph(text: message, fg: color), frame.area)
  end

  def initialize = (@frame, @finish = 0, Time.now + 2)
  def connected? = Time.now >= @finish 
  def spin = SPINNER[(@frame += 1) % SPINNER.length]
  SPINNER = %w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏]
end
Spinner.new.main; puts
联系我们 contact @ memedata.com