#-----------------------------------------------------------------------------* # # @file gtl_debugger_input.galgas # # @section desc File description # # Manage the input buffer for debugger commands. # # @section copyright Copyright # # Goil OIL compiler, part of Trampoline RTOS # # Trampoline is copyright (c) CNRS, University of Nantes, # Ecole Centrale de Nantes # Trampoline is protected by the French intellectual property law. # # This software is distributed under the GNU Public Licence V2. # Check the LICENSE file in the root directory of Trampoline # # $Date$ # $Rev$ # $Author$ # $URL$ # #-----------------------------------------------------------------------------* class @debugCommandInput { @stringlist history } setter @debugCommandInput getCommand !@string command { command = "" @string savedCommand = "" @uint historyIndex = 0 @char inputChar = '\r' @uint cursorPos = 0 @uint escapeState = 0 loop (@uint.max) inputChar = @char.unicodeCharacterFromRawKeyboard while inputChar != '\r' do if escapeState == 1 & [inputChar uint] == 0x5B then escapeState = 2 elsif escapeState == 2 then # got ESC ], the char is the arrow if [inputChar uint] == 0x44 then # left arrow if cursorPos > 0 then cursorPos-- message "\u001B[1D" end elsif [inputChar uint] == 0x43 then # right arrow if cursorPos < [command length] then cursorPos++ message "\u001B[1C" end elsif [inputChar uint] == 0x41 then # up arrow if historyIndex == 0 then savedCommand = command end if historyIndex < [history length] then if cursorPos > 0 then message "\u001B[" + cursorPos + "D" end message "\u001B[K" command = [history mValueAtIndex !historyIndex] message command cursorPos = [command length] historyIndex++ end elsif [inputChar uint] == 0x42 then # down arrow if historyIndex > 0 then if cursorPos > 0 then message "\u001B[" + cursorPos + "D" end message "\u001B[K" historyIndex-- if historyIndex == 0 then command = savedCommand else command = [history mValueAtIndex !historyIndex - 1] end message command cursorPos = [command length] end end escapeState = 0 else if [inputChar uint] == 0x7F then # delete key if cursorPos > 0 then cursorPos-- [!?command removeCharacterAtIndex ?* !cursorPos] message "\u001B[1D\u001B[K" let @uint remainingChars = [command length] - cursorPos if remainingChars > 0 then message [command rightSubString !remainingChars] message "\u001B[" + remainingChars + "D" end end message [inputChar string] elsif [inputChar uint] == 0x1B then # escape key escapeState = 1 elsif inputChar != '\0' then if cursorPos == [command length] then command = command + inputChar message [inputChar string] cursorPos++ else [!?command insertCharacterAtIndex !inputChar !cursorPos] message [inputChar string] cursorPos++ let @uint remainingChars = [command length] - cursorPos if remainingChars > 0 then message [command rightSubString !remainingChars] message "\u001B[" + remainingChars + "D" end end end # message "buffer (" + cursorPos +"): " # @uint index = 0 # loop (@uint.max) # while index < [command length] do # if index == cursorPos then # message "^" # end # message "[" + [[[command characterAtIndex !index] uint] xString] + "]" # index++ # end # if index == cursorPos then # message "^" # end # message "\n" end end message "\n" if [command stringByTrimmingWhiteSpaces] != "hist" then [!?history insertAtIndex !command !0] end } method @debugCommandInput listHistory { if [history length] > 0 then message "Command history:\n" for (item) in history do (index) message [[index string] stringByLeftPadding !4 !' '] + ": " + item + "\n" end else message "Command history empty.\n" end }