jline: split up cli line reading

This commit is contained in:
Bixilon 2023-03-19 16:40:46 +01:00
parent abbf5d0f84
commit d9a3221df4
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 19 additions and 13 deletions

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger * Copyright (C) 2020-2023 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *

View File

@ -70,11 +70,14 @@ object CLI {
this::connection.observe(this) { register() } this::connection.observe(this) { register() }
reader.pollLines()
}
private fun LineReader.pollLines() {
while (true) { while (true) {
var line = "" val line: String
try { try {
line = reader.readLine().trimWhitespaces() line = readLine().trimWhitespaces()
terminal.flush() terminal.flush()
} catch (exception: EndOfFileException) { } catch (exception: EndOfFileException) {
Log.log(LogMessageType.GENERAL, LogLevels.VERBOSE) { exception.printStackTrace() } Log.log(LogMessageType.GENERAL, LogLevels.VERBOSE) { exception.printStackTrace() }
@ -87,16 +90,19 @@ object CLI {
exception.printStackTrace() exception.printStackTrace()
continue continue
} }
try { if (line.isBlank()) continue
if (line.isBlank()) {
continue processLine(line)
} }
ROOT_NODE.execute(line, connection) }
} catch (error: ReaderError) {
Log.log(LogMessageType.OTHER, LogLevels.WARN) { error.message } private fun processLine(line: String) {
} catch (error: Throwable) { try {
error.printStackTrace() ROOT_NODE.execute(line, connection)
} } catch (error: ReaderError) {
Log.log(LogMessageType.OTHER, LogLevels.WARN) { error.message }
} catch (error: Throwable) {
error.printStackTrace()
} }
} }