From bf1990055f427603a1080cdc490d73bf36c21f6d Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 27 Feb 2024 13:21:55 +0300 Subject: [PATCH] Add command line argument to change tag width --- src/nativeMain/kotlin/AppConfig.kt | 2 ++ .../kotlin/ui/logLines/LogLineExtensions.kt | 3 +-- .../kotlin/ui/logLines/LogLinesPresenter.kt | 2 ++ .../kotlin/ui/logLines/LogLinesView.kt | 2 ++ .../kotlin/ui/logLines/TagExtensions.kt | 13 ++++++------ src/nativeMain/kotlin/userInput/Arguments.kt | 20 +++++++++++++++++-- 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/nativeMain/kotlin/AppConfig.kt b/src/nativeMain/kotlin/AppConfig.kt index 28a173e..2fdef97 100644 --- a/src/nativeMain/kotlin/AppConfig.kt +++ b/src/nativeMain/kotlin/AppConfig.kt @@ -4,6 +4,8 @@ object AppConfig { const val DEFAULT_TAG_WIDTH = 23 + const val MAX_TAG_WIDTH = 50 + const val APP_LOG_FILENAME = "log.txt" const val COMMAND_TIMEOUT_MILLIS = 3000L diff --git a/src/nativeMain/kotlin/ui/logLines/LogLineExtensions.kt b/src/nativeMain/kotlin/ui/logLines/LogLineExtensions.kt index 37aa459..cfabd9e 100644 --- a/src/nativeMain/kotlin/ui/logLines/LogLineExtensions.kt +++ b/src/nativeMain/kotlin/ui/logLines/LogLineExtensions.kt @@ -1,6 +1,5 @@ package ui.logLines -import AppConfig.DEFAULT_TAG_WIDTH import AppConfig.LOG_LEVEL_WIDTH import AppConfig.LOG_LINE_ESCAPE_REGEX_STRING import dogcat.Brief @@ -108,7 +107,7 @@ private fun LogLinesView.wrapLine( val line = message.replace(escapeRegex, " ") val width = position.endX - val header = DEFAULT_TAG_WIDTH + LOG_LEVEL_WIDTH + val header = state.tagWidth + LOG_LEVEL_WIDTH val wrapArea = width - header var lineBuffer = "" diff --git a/src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt b/src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt index 8891315..a72a33c 100644 --- a/src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt +++ b/src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt @@ -1,5 +1,6 @@ package ui.logLines +import AppConfig.DEFAULT_TAG_WIDTH import AppState import dogcat.Dogcat import dogcat.Unparseable @@ -57,6 +58,7 @@ class LogLinesPresenter( view.state = view.state.copy( autoscroll = it.autoscroll, showLineNumbers = arguments.lineNumbers ?: false, + tagWidth = arguments.tagWidth ?: DEFAULT_TAG_WIDTH, isCursorHeld = it.isCursorHeld, cursorReturnLocation = it.inputFilterLocation ) diff --git a/src/nativeMain/kotlin/ui/logLines/LogLinesView.kt b/src/nativeMain/kotlin/ui/logLines/LogLinesView.kt index d706bbd..3ebaf27 100644 --- a/src/nativeMain/kotlin/ui/logLines/LogLinesView.kt +++ b/src/nativeMain/kotlin/ui/logLines/LogLinesView.kt @@ -1,5 +1,6 @@ package ui.logLines +import AppConfig.DEFAULT_TAG_WIDTH import AppConfig.LOG_LINES_VIEW_BOTTOM_MARGIN import dogcat.DogcatConfig.MAX_LOG_LINES import kotlinx.cinterop.ExperimentalForeignApi @@ -22,6 +23,7 @@ class LogLinesView : HasLifecycle { data class State( val autoscroll: Boolean = false, val showLineNumbers: Boolean = false, + val tagWidth: Int = DEFAULT_TAG_WIDTH, val isCursorHeld: Boolean = false, val cursorReturnLocation: Pair? = null, ) diff --git a/src/nativeMain/kotlin/ui/logLines/TagExtensions.kt b/src/nativeMain/kotlin/ui/logLines/TagExtensions.kt index 128fdac..cc6886c 100644 --- a/src/nativeMain/kotlin/ui/logLines/TagExtensions.kt +++ b/src/nativeMain/kotlin/ui/logLines/TagExtensions.kt @@ -1,6 +1,5 @@ package ui.logLines -import AppConfig.DEFAULT_TAG_WIDTH import AppConfig.TAG_COLOR_PAIR_OFFSET import kotlinx.cinterop.ExperimentalForeignApi import ncurses.* @@ -14,15 +13,15 @@ internal fun LogLinesView.printTag(tag: String) { wattroff(pad, COLOR_PAIR(TAG_COLOR_MAP[color]!!)) } -private fun procrustes(tag: String) = - if (tag.length > DEFAULT_TAG_WIDTH) { - val excess = 1 - DEFAULT_TAG_WIDTH % 2 +private fun LogLinesView.procrustes(tag: String) = + if (tag.length > state.tagWidth) { + val excess = 1 - state.tagWidth % 2 - tag.take(DEFAULT_TAG_WIDTH / 2 - excess) + + tag.take(state.tagWidth / 2 - excess) + Typography.ellipsis + - tag.takeLast(DEFAULT_TAG_WIDTH / 2) + tag.takeLast(state.tagWidth / 2) } else { - tag.trim().padStart(DEFAULT_TAG_WIDTH) + tag.trim().padStart(state.tagWidth) } private fun allocateColor(tag: String): Int { diff --git a/src/nativeMain/kotlin/userInput/Arguments.kt b/src/nativeMain/kotlin/userInput/Arguments.kt index 33a4f0a..e7a802c 100644 --- a/src/nativeMain/kotlin/userInput/Arguments.kt +++ b/src/nativeMain/kotlin/userInput/Arguments.kt @@ -1,5 +1,7 @@ package userInput +import AppConfig.DEFAULT_TAG_WIDTH +import AppConfig.MAX_TAG_WIDTH import kotlinx.cli.ArgParser import kotlinx.cli.ArgType import kotlinx.cli.optional @@ -18,20 +20,34 @@ class Arguments(private val parser: ArgParser) { val current by parser.option( ArgType.Boolean, shortName = "c", - description = "Show logs for an application currently running in foreground" + description = "Show logs for an application currently running in foreground." ) val lineNumbers by parser.option( ArgType.Boolean, shortName = "ln", - description = "Show line numbers for log lines, embedded into message body" + description = "Show line numbers for log lines, embedded into message body," + ) + + val tagWidth by parser.option( + ArgType.Int, + shortName = "tw", + description = "Specify width for displaying log tags. Values between 1 and $MAX_TAG_WIDTH are accepted. " + + "Default value is $DEFAULT_TAG_WIDTH." ) fun validate(args: Array) { parser.parse(args) + if (packageName != null && current != null) { throw ValidationException("'Package name' and '--current' arguments are mutually exclusive and " + "can't be used at the same time.") } + + tagWidth?.let { + if (tagWidth !in 1..MAX_TAG_WIDTH) { + throw ValidationException("Tag width should be a value between 1 and $MAX_TAG_WIDTH.") + } + } } }