Browse Source

Terminal new line (Shift+Enter) functionality

TheSumm 6 years ago
parent
commit
3bffa6b04a
2 changed files with 40 additions and 9 deletions
  1. 26
    6
      modules/client_terminal/terminal.lua
  2. 14
    3
      modules/client_terminal/terminal.otui

+ 26
- 6
modules/client_terminal/terminal.lua View File

@@ -30,6 +30,10 @@ local allLines = {}
30 30
 
31 31
 -- private functions
32 32
 local function navigateCommand(step)
33
+  if commandTextEdit:isMultiline() then
34
+    return
35
+  end
36
+
33 37
   local numCommands = #commandHistory
34 38
   if numCommands > 0 then
35 39
     currentHistoryIndex = math.min(math.max(currentHistoryIndex + step, 0), numCommands)
@@ -96,14 +100,27 @@ local function completeCommand()
96 100
   end
97 101
 end
98 102
 
99
-local function doCommand()
100
-  local currentCommand = commandTextEdit:getText()
103
+local function doCommand(textWidget)
104
+  local currentCommand = textWidget:getText()
101 105
   executeCommand(currentCommand)
106
+  textWidget:clearText()
107
+  return true
108
+end
102 109
 
103
-  if commandTextEdit then
104
-    commandTextEdit:clearText()
110
+local function addNewline(textWidget)
111
+  if not textWidget:isOn() then
112
+    textWidget:setOn(true)
113
+  end
114
+  textWidget:appendText('\n')
115
+end
116
+
117
+local function onCommandChange(textWidget, newText, oldText)
118
+  local _, newLineCount = string.gsub(newText, '\n', '\n')
119
+  textWidget:setHeight((newLineCount + 1) * textWidget.baseHeight)
120
+
121
+  if newLineCount == 0 and textWidget:isOn() then
122
+    textWidget:setOn(false)
105 123
   end
106
-  return true
107 124
 end
108 125
 
109 126
 local function onLog(level, message, time)
@@ -129,6 +146,8 @@ function init()
129 146
   commandHistory = g_settings.getList('terminal-history')
130 147
 
131 148
   commandTextEdit = terminalWindow:getChildById('commandTextEdit')
149
+  commandTextEdit:setHeight(commandTextEdit.baseHeight)
150
+  connect(commandTextEdit, {onTextChange = onCommandChange})
132 151
   g_keyboard.bindKeyPress('Up', function() navigateCommand(1) end, commandTextEdit)
133 152
   g_keyboard.bindKeyPress('Down', function() navigateCommand(-1) end, commandTextEdit)
134 153
   g_keyboard.bindKeyPress('Ctrl+C',
@@ -138,6 +157,7 @@ function init()
138 157
     return true
139 158
     end, commandTextEdit)
140 159
   g_keyboard.bindKeyDown('Tab', completeCommand, commandTextEdit)
160
+  g_keyboard.bindKeyPress('Shift+Enter', addNewline, commandTextEdit)
141 161
   g_keyboard.bindKeyDown('Enter', doCommand, commandTextEdit)
142 162
   g_keyboard.bindKeyDown('Escape', hide, terminalWindow)
143 163
 
@@ -293,7 +313,7 @@ function addLine(text, color)
293 313
 end
294 314
 
295 315
 function executeCommand(command)
296
-  if command == nil or #command == 0 then return end
316
+  if command == nil or #string.gsub(command, '\n', '') == 0 then return end
297 317
 
298 318
   -- add command line
299 319
   addLine("> " .. command, "#ffffff")

+ 14
- 3
modules/client_terminal/terminal.otui View File

@@ -47,7 +47,7 @@ UIWindow
47 47
     anchors.left: parent.left
48 48
     anchors.right: terminalScroll.left
49 49
     anchors.top: terminalScroll.top
50
-    anchors.bottom: commandSymbolLabel.top
50
+    anchors.bottom: commandTextEdit.top
51 51
     layout:
52 52
       type: verticalBox
53 53
       align-bottom: true
@@ -80,14 +80,25 @@ UIWindow
80 80
 
81 81
   UITextEdit
82 82
     id: commandTextEdit
83
-    height: 12
83
+    background: #aaaaaa11
84
+    border-color: #aaaaaa88
85
+    &baseHeight: 12
84 86
     anchors.bottom: parent.bottom
85 87
     anchors.left: commandSymbolLabel.right
86
-    anchors.right: parent.right
88
+    anchors.right: terminalScroll.left
87 89
     margin-left: 1
90
+    padding-left: 2
88 91
     font: terminus-10px
89 92
     selection-color: black
90 93
     selection-background-color: white
94
+    border-width-left: 0
95
+    border-width-top: 0
96
+    multiline: false
97
+
98
+    $on:
99
+      border-width-left: 1
100
+      border-width-top: 1
101
+      multiline: true
91 102
 
92 103
   ResizeBorder
93 104
     id: bottomResizeBorder

Loading…
Cancel
Save