Optimize terminal buffering

This commit is contained in:
Eduardo Bart 2013-02-28 21:35:14 -03:00
parent 9312d20a0f
commit dad3026ba0
4 changed files with 66 additions and 42 deletions

View File

@ -3,8 +3,7 @@ local LogColors = { [LogDebug] = 'pink',
[LogInfo] = 'white', [LogInfo] = 'white',
[LogWarning] = 'yellow', [LogWarning] = 'yellow',
[LogError] = 'red' } [LogError] = 'red' }
local MaxLogLines = 512 local MaxLogLines = 128
local LabelHeight = 16
local MaxHistory = 1000 local MaxHistory = 1000
local oldenv = getfenv(0) local oldenv = getfenv(0)
@ -24,6 +23,9 @@ local poped = false
local oldPos local oldPos
local oldSize local oldSize
local firstShown = false local firstShown = false
local flushEvent
local cachedLines = {}
local disabled = false
-- private functions -- private functions
local function navigateCommand(step) local function navigateCommand(step)
@ -104,6 +106,7 @@ local function doCommand()
end end
local function onLog(level, message, time) local function onLog(level, message, time)
if disabled then return end
-- avoid logging while reporting logs (would cause a infinite loop) -- avoid logging while reporting logs (would cause a infinite loop)
if logLocked then return end if logLocked then return end
@ -137,9 +140,11 @@ function init()
g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit) g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit)
g_keyboard.bindKeyDown('Escape', hide, terminalWindow) g_keyboard.bindKeyDown('Escape', hide, terminalWindow)
terminalBuffer = terminalWindow:recursiveGetChildById('terminalBuffer') terminalBuffer = terminalWindow:getChildById('terminalBuffer')
terminalSelectText = terminalWindow:recursiveGetChildById('terminalSelectText') terminalSelectText = terminalWindow:getChildById('terminalSelectText')
terminalSelectText.onDoubleClick = popWindow terminalSelectText.onDoubleClick = popWindow
terminalSelectText.onMouseWheel = function(a,b,c) terminalBuffer:onMouseWheel(b,c) end
terminalBuffer.onScrollChange = function(self, value) terminalSelectText:setTextVirtualOffset(value) end
g_logger.setOnLog(onLog) g_logger.setOnLog(onLog)
g_logger.fireOldMessages() g_logger.fireOldMessages()
@ -148,6 +153,8 @@ end
function terminate() function terminate()
g_settings.setList('terminal-history', commandHistory) g_settings.setList('terminal-history', commandHistory)
removeEvent(flushEvent)
if poped then if poped then
oldPos = terminalWindow:getPosition() oldPos = terminalWindow:getPosition()
oldSize = terminalWindow:getSize() oldSize = terminalWindow:getSize()
@ -231,22 +238,41 @@ end
function disable() function disable()
terminalButton:hide() terminalButton:hide()
g_keyboard.unbindKeyDown('Ctrl+T') g_keyboard.unbindKeyDown('Ctrl+T')
disabled = true
end
function flushLines()
local numLines = terminalBuffer:getChildCount() + #cachedLines
local fulltext = terminalSelectText:getText()
for _,line in pairs(cachedLines) do
-- delete old lines if needed
if numLines > MaxLogLines then
local len = #terminalBuffer:getChildByIndex(1):getText()
terminalBuffer:getChildByIndex(1):destroy()
fulltext = string.sub(fulltext, len)
end
local label = g_ui.createWidget('TerminalLabel', terminalBuffer)
label:setId('terminalLabel' .. numLines)
label:setText(line.text)
label:setColor(line.color)
fulltext = fulltext .. '\n' .. line.text
end
terminalSelectText:setText(fulltext)
cachedLines = {}
flushEvent = nil
end end
function addLine(text, color) function addLine(text, color)
-- delete old lines if needed if not flushEvent then
local numLines = terminalBuffer:getChildCount() + 1 flushEvent = scheduleEvent(flushLines, 10)
if numLines > MaxLogLines then
terminalBuffer:getChildByIndex(1):destroy()
end end
-- create new line label table.insert(cachedLines, {text=text, color=color})
local label = g_ui.createWidget('TerminalLabel', terminalBuffer)
label:setId('terminalLabel' .. numLines)
label:setText(text)
label:setColor(color)
terminalSelectText:setText(terminalSelectText:getText() .. '\n' .. text)
end end
function executeCommand(command) function executeCommand(command)

View File

@ -42,29 +42,23 @@ UIWindow
visible: false visible: false
ScrollablePanel ScrollablePanel
id: terminalScrollArea id: terminalBuffer
focusable: false focusable: false
anchors.left: parent.left anchors.left: parent.left
anchors.right: terminalScroll.left anchors.right: terminalScroll.left
anchors.top: terminalScroll.top anchors.top: terminalScroll.top
anchors.bottom: commandSymbolLabel.top anchors.bottom: commandSymbolLabel.top
layout:
type: verticalBox
align-bottom: true
vertical-scrollbar: terminalScroll vertical-scrollbar: terminalScroll
inverted-scroll: true inverted-scroll: true
margin-left: 2 margin-left: 2
Panel TerminalSelectText
anchors.bottom: parent.bottom id: terminalSelectText
anchors.left: parent.left anchors.fill: terminalBuffer
anchors.right: parent.right focusable: false
id: terminalBuffer
layout:
type: verticalBox
fit-children: true
focusable: false
TerminalSelectText
id: terminalSelectText
anchors.fill: terminalBuffer
VerticalScrollBar VerticalScrollBar
id: terminalScroll id: terminalScroll

View File

@ -73,23 +73,23 @@ end
function UIScrollArea:setVerticalScrollBar(scrollbar) function UIScrollArea:setVerticalScrollBar(scrollbar)
self.verticalScrollBar = scrollbar self.verticalScrollBar = scrollbar
self.verticalScrollBar.onValueChange = function(scrollbar, value) connect(self.verticalScrollBar, 'onValueChange', function(scrollbar, value)
local virtualOffset = self:getVirtualOffset() local virtualOffset = self:getVirtualOffset()
virtualOffset.y = value virtualOffset.y = value
self:setVirtualOffset(virtualOffset) self:setVirtualOffset(virtualOffset)
if self.onScrollbarChange then self:onScrollbarChange(value) end signalcall(self.onScrollChange, self, virtualOffset)
end end)
self:updateScrollBars() self:updateScrollBars()
end end
function UIScrollArea:setHorizontalScrollBar(scrollbar) function UIScrollArea:setHorizontalScrollBar(scrollbar)
self.horizontalScrollBar = scrollbar self.horizontalScrollBar = scrollbar
self.horizontalScrollBar.onValueChange = function(scrollbar, value) connect(self.horizontalScrollBar, 'onValueChange', function(scrollbar, value)
local virtualOffset = self:getVirtualOffset() local virtualOffset = self:getVirtualOffset()
virtualOffset.x = value virtualOffset.x = value
self:setVirtualOffset(virtualOffset) self:setVirtualOffset(virtualOffset)
if self.onScrollbarChange then self:onScrollbarChange(value) end signalcall(self.onScrollChange, self, virtualOffset)
end end)
self:updateScrollBars() self:updateScrollBars()
end end

View File

@ -70,9 +70,11 @@ void UITextEdit::drawSelf(Fw::DrawPane drawPane)
return; return;
if(hasSelection()) { if(hasSelection()) {
g_painter->setColor(m_color); if(m_color != Color::alpha) {
for(int i=0;i<m_selectionStart;++i) g_painter->setColor(m_color);
g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]); for(int i=0;i<m_selectionStart;++i)
g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]);
}
for(int i=m_selectionStart;i<m_selectionEnd;++i) { for(int i=m_selectionStart;i<m_selectionEnd;++i) {
g_painter->setColor(m_selectionBackgroundColor); g_painter->setColor(m_selectionBackgroundColor);
@ -81,10 +83,12 @@ void UITextEdit::drawSelf(Fw::DrawPane drawPane)
g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]); g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]);
} }
g_painter->setColor(m_color); if(m_color != Color::alpha) {
for(int i=m_selectionEnd;i<textLength;++i) g_painter->setColor(m_color);
g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]); for(int i=m_selectionEnd;i<textLength;++i)
} else { g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]);
}
} else if(m_color != Color::alpha) {
g_painter->setColor(m_color); g_painter->setColor(m_color);
for(int i=0;i<textLength;++i) for(int i=0;i<textLength;++i)
g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]); g_painter->drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]);