我创建了一个类,我正在尝试模拟richtextbox,在Windows窗体上排序。这意味着当您向窗体/ richtextbox添加新数据时,它将添加到框/窗口的底部,其余部分将向上滚动一行。我试过启用scrollok()
,但它似乎不想滚动。我不确定它是否被窃听或我的实施方式是错误的。可滚动窗口ncurses ruby
class Textpad
attr_accessor :data, :name, :window
def initialize(name, height, width, startx, starty)
@data = []
@name = name
@height = height
@width = width
@startx = startx
@starty = starty
Ncurses.refresh
@window = Ncurses.newwin(height, width, starty, startx)
@window.scrollok true
@window.wrefresh
end
def add(packetid, username, message)
@data.push [Time.new.strftime('[%T]'), packetid, username, message]
@data.shift if @data.length > 500
end
def draw
Ncurses.init_pair(1, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK)
Ncurses.init_pair(2, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK)
Ncurses.init_pair(3, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK)
Ncurses.init_pair(4, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK)
@window.wclear
position = 0
@data.each do |timestamp, packetid, username, message|
case packetid
when '1005'
@window.mvwprintw(1*position, 1, "#{timestamp} «#{username}» #{message}")
@window.mvchgat(1*position, timestamp.length+2, 1, Ncurses::A_NORMAL, 3, NIL)
@window.mvchgat(1*position, timestamp.length+3+username.length, 1, Ncurses::A_NORMAL, 3, NIL) #colorize the symboles around the username
end
position += 1
end
@window.wrefresh
end
end
问题出在我的Textpad类的绘图方法中。我可以用数百个条目填充Textpad类的数据数组,但只有数组的顶部才会被写入(直到它到达窗口的底部)而没有滚动。我手动滚动屏幕还是什么?从文档说它应该自动滚动,当光标到达底部,并添加另一条线。
得到它的工作。显然'mvwprintw()'不会移动光标或者所以我不得不切换到正常的'wprintw()'这不是一个问题,因为我可以添加一个\ n到换行符。唯一的缺点是我的'mvchgat()'函数现在会缩进文本,而不是给特定的位置着色。 –