From 3bffa6b04a0e4a75de8c66270e3e1918e52c9f75 Mon Sep 17 00:00:00 2001 From: TheSumm Date: Sat, 7 Mar 2015 16:32:45 +0100 Subject: [PATCH] Terminal new line (Shift+Enter) functionality --- modules/client_terminal/terminal.lua | 32 ++++++++++++++++++++++----- modules/client_terminal/terminal.otui | 17 +++++++++++--- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/modules/client_terminal/terminal.lua b/modules/client_terminal/terminal.lua index 861cbf35..38d74dde 100644 --- a/modules/client_terminal/terminal.lua +++ b/modules/client_terminal/terminal.lua @@ -30,6 +30,10 @@ local allLines = {} -- private functions local function navigateCommand(step) + if commandTextEdit:isMultiline() then + return + end + local numCommands = #commandHistory if numCommands > 0 then currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands) @@ -96,14 +100,27 @@ local function completeCommand() end end -local function doCommand() - local currentCommand = commandTextEdit:getText() +local function doCommand(textWidget) + local currentCommand = textWidget:getText() executeCommand(currentCommand) + textWidget:clearText() + return true +end - if commandTextEdit then - commandTextEdit:clearText() +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 - return true end local function onLog(level, message, time) @@ -129,6 +146,8 @@ function init() commandHistory = g_settings.getList('terminal-history') commandTextEdit = terminalWindow:getChildById('commandTextEdit') + commandTextEdit:setHeight(commandTextEdit.baseHeight) + connect(commandTextEdit, {onTextChange = onCommandChange}) g_keyboard.bindKeyPress('Up', function() navigateCommand(1) end, commandTextEdit) g_keyboard.bindKeyPress('Down', function() navigateCommand(-1) end, commandTextEdit) g_keyboard.bindKeyPress('Ctrl+C', @@ -138,6 +157,7 @@ function init() return true end, commandTextEdit) g_keyboard.bindKeyDown('Tab', completeCommand, commandTextEdit) + g_keyboard.bindKeyPress('Shift+Enter', addNewline, commandTextEdit) g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit) g_keyboard.bindKeyDown('Escape', hide, terminalWindow) @@ -293,7 +313,7 @@ function addLine(text, color) end 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 addLine("> " .. command, "#ffffff") diff --git a/modules/client_terminal/terminal.otui b/modules/client_terminal/terminal.otui index afcd0a26..67b8ba06 100644 --- a/modules/client_terminal/terminal.otui +++ b/modules/client_terminal/terminal.otui @@ -47,7 +47,7 @@ UIWindow anchors.left: parent.left anchors.right: terminalScroll.left anchors.top: terminalScroll.top - anchors.bottom: commandSymbolLabel.top + anchors.bottom: commandTextEdit.top layout: type: verticalBox align-bottom: true @@ -80,14 +80,25 @@ UIWindow UITextEdit id: commandTextEdit - height: 12 + background: #aaaaaa11 + border-color: #aaaaaa88 + &baseHeight: 12 anchors.bottom: parent.bottom anchors.left: commandSymbolLabel.right - anchors.right: parent.right + anchors.right: terminalScroll.left margin-left: 1 + padding-left: 2 font: terminus-10px selection-color: black 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 id: bottomResizeBorder