Terminal new line (Shift+Enter) functionality

This commit is contained in:
TheSumm 2015-03-07 16:32:45 +01:00
parent 83dc129f03
commit 3bffa6b04a
2 changed files with 41 additions and 10 deletions

View File

@ -30,6 +30,10 @@ local allLines = {}
-- private functions -- private functions
local function navigateCommand(step) local function navigateCommand(step)
if commandTextEdit:isMultiline() then
return
end
local numCommands = #commandHistory local numCommands = #commandHistory
if numCommands > 0 then if numCommands > 0 then
currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands) currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands)
@ -96,16 +100,29 @@ local function completeCommand()
end end
end end
local function doCommand() local function doCommand(textWidget)
local currentCommand = commandTextEdit:getText() local currentCommand = textWidget:getText()
executeCommand(currentCommand) executeCommand(currentCommand)
textWidget:clearText()
if commandTextEdit then
commandTextEdit:clearText()
end
return true return true
end end
local function addNewline(textWidget)
if not textWidget:isOn() then
textWidget:setOn(true)
end
textWidget:appendText('\n')
end
local function onCommandChange(textWidget, newText, oldText)
local _, newLineCount = string.gsub(newText, '\n', '\n')
textWidget:setHeight((newLineCount + 1) * textWidget.baseHeight)
if newLineCount == 0 and textWidget:isOn() then
textWidget:setOn(false)
end
end
local function onLog(level, message, time) local function onLog(level, message, time)
if disabled then return end 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)
@ -129,6 +146,8 @@ function init()
commandHistory = g_settings.getList('terminal-history') commandHistory = g_settings.getList('terminal-history')
commandTextEdit = terminalWindow:getChildById('commandTextEdit') commandTextEdit = terminalWindow:getChildById('commandTextEdit')
commandTextEdit:setHeight(commandTextEdit.baseHeight)
connect(commandTextEdit, {onTextChange = onCommandChange})
g_keyboard.bindKeyPress('Up', function() navigateCommand(1) end, commandTextEdit) g_keyboard.bindKeyPress('Up', function() navigateCommand(1) end, commandTextEdit)
g_keyboard.bindKeyPress('Down', function() navigateCommand(-1) end, commandTextEdit) g_keyboard.bindKeyPress('Down', function() navigateCommand(-1) end, commandTextEdit)
g_keyboard.bindKeyPress('Ctrl+C', g_keyboard.bindKeyPress('Ctrl+C',
@ -138,6 +157,7 @@ function init()
return true return true
end, commandTextEdit) end, commandTextEdit)
g_keyboard.bindKeyDown('Tab', completeCommand, commandTextEdit) g_keyboard.bindKeyDown('Tab', completeCommand, commandTextEdit)
g_keyboard.bindKeyPress('Shift+Enter', addNewline, commandTextEdit)
g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit) g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit)
g_keyboard.bindKeyDown('Escape', hide, terminalWindow) g_keyboard.bindKeyDown('Escape', hide, terminalWindow)
@ -293,7 +313,7 @@ function addLine(text, color)
end end
function executeCommand(command) function executeCommand(command)
if command == nil or #command == 0 then return end if command == nil or #string.gsub(command, '\n', '') == 0 then return end
-- add command line -- add command line
addLine("> " .. command, "#ffffff") addLine("> " .. command, "#ffffff")

View File

@ -47,7 +47,7 @@ UIWindow
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: commandTextEdit.top
layout: layout:
type: verticalBox type: verticalBox
align-bottom: true align-bottom: true
@ -80,14 +80,25 @@ UIWindow
UITextEdit UITextEdit
id: commandTextEdit id: commandTextEdit
height: 12 background: #aaaaaa11
border-color: #aaaaaa88
&baseHeight: 12
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
anchors.left: commandSymbolLabel.right anchors.left: commandSymbolLabel.right
anchors.right: parent.right anchors.right: terminalScroll.left
margin-left: 1 margin-left: 1
padding-left: 2
font: terminus-10px font: terminus-10px
selection-color: black selection-color: black
selection-background-color: white selection-background-color: white
border-width-left: 0
border-width-top: 0
multiline: false
$on:
border-width-left: 1
border-width-top: 1
multiline: true
ResizeBorder ResizeBorder
id: bottomResizeBorder id: bottomResizeBorder