Clean up lisp terminal code

This commit is contained in:
Mike Cifelli 2018-10-03 19:20:42 -04:00
parent 3ec8e72a17
commit 24343b1543
1 changed files with 24 additions and 25 deletions

View File

@ -20,7 +20,10 @@ import java.util.concurrent.Executors
class LispTerminal(configuration: TerminalConfiguration) { 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 inputWriter = SafeOutputStream(configuration.inputWriter)
private val outputReader = SafeInputStream(configuration.outputReader) private val outputReader = SafeInputStream(configuration.outputReader)
private val controlSequenceHandler = ControlSequenceHandler() private val controlSequenceHandler = ControlSequenceHandler()
@ -33,29 +36,8 @@ class LispTerminal(configuration: TerminalConfiguration) {
private var originColumn = 0 private var originColumn = 0
private var originRow = 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 { init {
setOriginToCurrentPosition() setOriginToCurrentPosition()
terminal.addResizeListener { _, _ -> resize() }
} }
@Synchronized @Synchronized
@ -88,7 +70,17 @@ class LispTerminal(configuration: TerminalConfiguration) {
@Synchronized @Synchronized
private fun moveCursorToEndOfInput() { 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() { fun start() {
@ -363,13 +355,20 @@ class LispTerminal(configuration: TerminalConfiguration) {
private fun doCharacter(character: Char) { private fun doCharacter(character: Char) {
val cursorPosition = terminal.cursorPosition val cursorPosition = terminal.cursorPosition
if (!isBufferFilled) if (!isBufferFilled())
if (isPossibleToMoveRight(cursorPosition)) if (isPossibleToMoveRight(cursorPosition))
insertCharacter(character, cursorPosition) insertCharacter(character, cursorPosition)
else else
appendCharacter(character, cursorPosition) appendCharacter(character, cursorPosition)
} }
@Synchronized
private fun isBufferFilled() = getLeadingEdge().let { leadingEdge ->
leadingEdge.row == terminalSize.rows - 1
&& leadingEdge.column >= terminalSize.columns - 1
&& originRow <= 0
}
@Synchronized @Synchronized
private fun insertCharacter(character: Char, cursorPosition: TerminalPosition) { private fun insertCharacter(character: Char, cursorPosition: TerminalPosition) {
val shiftedPosition = shiftPositionIfNewRowAdded(cursorPosition) val shiftedPosition = shiftPositionIfNewRowAdded(cursorPosition)
@ -385,7 +384,7 @@ class LispTerminal(configuration: TerminalConfiguration) {
@Synchronized @Synchronized
private fun shiftPositionIfNewRowAdded(cursorPosition: TerminalPosition): TerminalPosition { private fun shiftPositionIfNewRowAdded(cursorPosition: TerminalPosition): TerminalPosition {
val oldOriginRow = originRow val oldOriginRow = originRow
moveCursorRight(leadingEdge) moveCursorRight(getLeadingEdge())
return if (isNewRowAdded(oldOriginRow)) adjustCursorPosition(cursorPosition) else cursorPosition return if (isNewRowAdded(oldOriginRow)) adjustCursorPosition(cursorPosition) else cursorPosition
} }