Skip to content

Commit

Permalink
Add command line argument to change tag width
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseDreki committed Feb 27, 2024
1 parent f425da0 commit bf19900
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/nativeMain/kotlin/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/nativeMain/kotlin/ui/logLines/LogLineExtensions.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = ""
Expand Down
2 changes: 2 additions & 0 deletions src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ui.logLines

import AppConfig.DEFAULT_TAG_WIDTH
import AppState
import dogcat.Dogcat
import dogcat.Unparseable
Expand Down Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions src/nativeMain/kotlin/ui/logLines/LogLinesView.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Int, Int>? = null,
)
Expand Down
13 changes: 6 additions & 7 deletions src/nativeMain/kotlin/ui/logLines/TagExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ui.logLines

import AppConfig.DEFAULT_TAG_WIDTH
import AppConfig.TAG_COLOR_PAIR_OFFSET
import kotlinx.cinterop.ExperimentalForeignApi
import ncurses.*
Expand All @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions src/nativeMain/kotlin/userInput/Arguments.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String>) {
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.")
}
}
}
}

0 comments on commit bf19900

Please sign in to comment.