From 24343b1543fa264efab7470f507106e52a808bd0 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Wed, 3 Oct 2018 19:20:42 -0400 Subject: [PATCH] Clean up lisp terminal code --- src/main/kotlin/terminal/LispTerminal.kt | 49 ++++++++++++------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/terminal/LispTerminal.kt b/src/main/kotlin/terminal/LispTerminal.kt index 6dffa1b..64cb3b5 100644 --- a/src/main/kotlin/terminal/LispTerminal.kt +++ b/src/main/kotlin/terminal/LispTerminal.kt @@ -20,7 +20,10 @@ import java.util.concurrent.Executors class LispTerminal(configuration: TerminalConfiguration) { - private val terminal = configuration.terminal!! + private val terminal = configuration.terminal!!.apply { + addResizeListener { _, _ -> resize() } + } + private val inputWriter = SafeOutputStream(configuration.inputWriter) private val outputReader = SafeInputStream(configuration.outputReader) private val controlSequenceHandler = ControlSequenceHandler() @@ -33,29 +36,8 @@ class LispTerminal(configuration: TerminalConfiguration) { private var originColumn = 0 private var originRow = 0 - private val leadingEdge: TerminalPosition - @Synchronized get() { - val inputLength = inputLine.length - val totalColumns = terminalSize.columns - val rowDifference = inputLength / totalColumns - val columnDifference = inputLength % totalColumns - - return TerminalPosition(originColumn + columnDifference, originRow + rowDifference) - } - - private val isBufferFilled: Boolean - @Synchronized get() { - val row = leadingEdge.row - val column = leadingEdge.column - - return row == terminalSize.rows - 1 - && column >= terminalSize.columns - 1 - && originRow <= 0 - } - init { setOriginToCurrentPosition() - terminal.addResizeListener { _, _ -> resize() } } @Synchronized @@ -88,7 +70,17 @@ class LispTerminal(configuration: TerminalConfiguration) { @Synchronized private fun moveCursorToEndOfInput() { - terminal.cursorPosition = leadingEdge + terminal.cursorPosition = getLeadingEdge() + } + + @Synchronized + private fun getLeadingEdge(): TerminalPosition { + val inputLength = inputLine.length + val totalColumns = terminalSize.columns + val rowDifference = inputLength / totalColumns + val columnDifference = inputLength % totalColumns + + return TerminalPosition(originColumn + columnDifference, originRow + rowDifference) } fun start() { @@ -363,13 +355,20 @@ class LispTerminal(configuration: TerminalConfiguration) { private fun doCharacter(character: Char) { val cursorPosition = terminal.cursorPosition - if (!isBufferFilled) + if (!isBufferFilled()) if (isPossibleToMoveRight(cursorPosition)) insertCharacter(character, cursorPosition) else appendCharacter(character, cursorPosition) } + @Synchronized + private fun isBufferFilled() = getLeadingEdge().let { leadingEdge -> + leadingEdge.row == terminalSize.rows - 1 + && leadingEdge.column >= terminalSize.columns - 1 + && originRow <= 0 + } + @Synchronized private fun insertCharacter(character: Char, cursorPosition: TerminalPosition) { val shiftedPosition = shiftPositionIfNewRowAdded(cursorPosition) @@ -385,7 +384,7 @@ class LispTerminal(configuration: TerminalConfiguration) { @Synchronized private fun shiftPositionIfNewRowAdded(cursorPosition: TerminalPosition): TerminalPosition { val oldOriginRow = originRow - moveCursorRight(leadingEdge) + moveCursorRight(getLeadingEdge()) return if (isNewRowAdded(oldOriginRow)) adjustCursorPosition(cursorPosition) else cursorPosition }