Skip to content

Commit

Permalink
Centralize constants and string resources
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseDreki committed Feb 26, 2024
1 parent 1d64032 commit f99f2da
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/dogcat/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sealed interface Command {

data class FilterBy(val filter: LogFilter) : Command
data class ResetFilter(val filterClass: KClass<out LogFilter>) : Command
data object ClearLogSource : Command
data object ClearLogs : Command

data object Stop : Command
}
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/dogcat/Dogcat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Dogcat(
when (command) {
is Start -> start(command)

ClearLogSource -> {
ClearLogs -> {
stateSubject.emit(Inactive)

// keyboard input hangs upon clearing? when no emulators
Expand Down
14 changes: 13 additions & 1 deletion src/nativeMain/kotlin/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ object AppConfig {

const val COMMAND_TIMEOUT_MILLIS = 3000L

const val INPUT_FILTER_PREFIX = " Filter: "
const val TAG_COLOR_PAIR_OFFSET = 100

const val LOG_LINES_VIEW_BOTTOM_MARGIN = 4

const val STATUS_VIEW_AUTOSCROLL_LEFT_MARGIN = 15

const val STATUS_VIEW_BOTTOM_MARGIN = 2

const val LOG_LEVEL_WIDTH = 1 + 3 + 1 // space, level, space

const val LOG_LINE_ESCAPE_REGEX_STRING = """[\t\n\r\\b\f\v\a\e]"""

const val LOCALE = "en_US.UTF-8"

// Make sure pattern matching works on MinGW using '\n'
// There is no 'line.separator' or 'Environment.NewLine'
Expand Down
6 changes: 0 additions & 6 deletions src/nativeMain/kotlin/AppStateHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ interface AppState {
fun setInputFilterLocation(x: Int, y: Int)

fun holdCursor(hold: Boolean)

fun setCursorHoldLocation(x: Int, y: Int)
}

class InternalAppState : AppState {
Expand Down Expand Up @@ -59,8 +57,4 @@ class InternalAppState : AppState {
Logger.d("HOLD CURSOR $hold")
state.value = state.value.copy(isCursorHeld = hold)
}

override fun setCursorHoldLocation(x: Int, y: Int) {
state.value = state.value.copy(cursorHoldLocation = x to y)
}
}
14 changes: 7 additions & 7 deletions src/nativeMain/kotlin/ui/AppPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import AppState
import dogcat.Command.*
import dogcat.Command.Start.*
import dogcat.Dogcat
import dogcat.LogFilter.*
import dogcat.LogLevel.*
Expand All @@ -13,6 +14,7 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import logger.Logger
import logger.context
import ui.Strings.INPUT_FILTER_PREFIX
import ui.logLines.LogLinesPresenter
import ui.status.StatusPresenter
import userInput.Arguments
Expand All @@ -33,13 +35,11 @@ class AppPresenter(

override suspend fun start() {
when {
Arguments.packageName != null -> dogcat(Start.PickAppPackage(Arguments.packageName!!))
Arguments.current == true -> dogcat(Start.PickForegroundApp)
else -> dogcat(Start.PickAllApps)
Arguments.packageName != null -> dogcat(PickAppPackage(Arguments.packageName!!))
Arguments.current == true -> dogcat(PickForegroundApp)
else -> dogcat(PickAllApps)
}

appState.setInputFilterLocation(AppConfig.INPUT_FILTER_PREFIX.length, 49)

view.start()

val scope = CoroutineScope(coroutineContext)
Expand Down Expand Up @@ -99,7 +99,7 @@ class AppPresenter(
}

CLEAR_LOGS -> {
dogcat(ClearLogSource)
dogcat(ClearLogs)
}

TOGGLE_FILTER_BY_PACKAGE -> {
Expand All @@ -111,7 +111,7 @@ class AppPresenter(
dogcat(ResetFilter(ByPackage::class))
} else if (f.first != null) {
Logger.d("${context()} !SelectAppByPackage")
dogcat(Start.PickAppPackage(f.first!!.packageName))
dogcat(PickAppPackage(f.first!!.packageName))
appState.filterByPackage(f.first, true)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/nativeMain/kotlin/ui/AppView.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package ui

import AppConfig.LOCALE
import dogcat.DogcatException
import kotlinx.cinterop.ExperimentalForeignApi
import ncurses.*
import platform.posix.LC_ALL
import platform.posix.exit
import platform.posix.printf
import platform.posix.setlocale

@OptIn(ExperimentalForeignApi::class)
class AppView : HasLifecycle {

override suspend fun start() {
setlocale(LC_ALL, "en_US.UTF-8")
setlocale(LC_ALL, LOCALE)
initscr()

keypad(stdscr, true);
Expand Down
16 changes: 16 additions & 0 deletions src/nativeMain/kotlin/ui/Strings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ui

object Strings {

const val INPUT_FILTER_PREFIX = " Filter: "

const val LOG_LEVEL_PREFIX = " Log: "

const val AUTOSCROLL = "| Autoscroll"

const val NO_AUTOSCROLL = "| No autoscroll"

const val SEPARATOR = " | "

const val ALL_APPS = "All apps"
}
8 changes: 0 additions & 8 deletions src/nativeMain/kotlin/ui/ViewPosition.kt

This file was deleted.

63 changes: 39 additions & 24 deletions src/nativeMain/kotlin/ui/logLines/LogLineExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
@file:OptIn(ExperimentalForeignApi::class)

package ui.logLines

import AppConfig.DEFAULT_TAG_WIDTH
import AppConfig.LOG_LEVEL_WIDTH
import AppConfig.LOG_LINE_ESCAPE_REGEX_STRING
import dogcat.Brief
import dogcat.LogLevel.*
import dogcat.LogLine
import dogcat.Unparseable
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.coroutines.yield
import logger.Logger
import ncurses.*
import ui.CommonColors
import ui.CommonColors.*
import kotlin.math.min

Expand All @@ -20,7 +19,8 @@ suspend fun LogLinesView.processLogLine(
) {
if (it.value is Unparseable) {
printTag("")
waddstr(pad, " ".repeat(1 + 3 + 1))
waddstr(pad, " ".repeat(LOG_LEVEL_WIDTH))

//account for end of line in the same way as in wrapline
waddstr(pad, (it.value as Unparseable).line + "\n")
recordLine(1)
Expand All @@ -37,14 +37,22 @@ suspend fun LogLinesView.processLogLine(

when (val level = logLine.level) {
W -> {
printLevelAndMessage(level.name, BLACK_ON_YELLOW.colorPairCode, wrapped, COLOR_PAIR(YELLOW_ON_BG.colorPairCode))
printLevelAndMessage(
level.name,
BLACK_ON_YELLOW.colorPairCode,
wrapped,
COLOR_PAIR(YELLOW_ON_BG.colorPairCode)
)
}

E, F -> {
printLevelAndMessage(level.name, BLACK_ON_RED.colorPairCode, wrapped, COLOR_PAIR(RED_ON_BG.colorPairCode))
}

I -> {
printLevelAndMessage(level.name, BLACK_ON_WHITE.colorPairCode, wrapped, A_BOLD.toInt())
}

else -> {
printLevelAndMessage(level.name, BLACK_ON_WHITE.colorPairCode, wrapped, 0)
}
Expand All @@ -65,6 +73,7 @@ suspend fun LogLinesView.processLogLine(
yield()
}

@OptIn(ExperimentalForeignApi::class)
private fun LogLinesView.printLevelAndMessage(
level: String,
levelColorPair: Int,
Expand All @@ -73,9 +82,9 @@ private fun LogLinesView.printLevelAndMessage(
) {
waddstr(pad, " ")

wattron(pad, COLOR_PAIR(levelColorPair));
wattron(pad, COLOR_PAIR(levelColorPair))
waddstr(pad, " $level ")
wattroff(pad, COLOR_PAIR(levelColorPair));
wattroff(pad, COLOR_PAIR(levelColorPair))

waddstr(pad, " ")

Expand All @@ -84,38 +93,44 @@ private fun LogLinesView.printLevelAndMessage(
wattroff(pad, messageColorPair)
}

/**
* @return A pair of wrapped line (with correctly placed EOL) and number of screen lines it takes.
*/
private fun LogLinesView.wrapLine(
message: String
): Pair<String, Int> {

val r = """[\t\n\r\\b\f\v\a\e]""".toRegex()
val escapeRegex = LOG_LINE_ESCAPE_REGEX_STRING.toRegex()
val line = message.replace(escapeRegex, " ")

val width = position.endX
val header = AppConfig.DEFAULT_TAG_WIDTH + 1 + 3 + 1// space, level, space
val line = message.replace(r, " ")
val header = DEFAULT_TAG_WIDTH + LOG_LEVEL_WIDTH
val wrapArea = width - header
var buf = ""
var current = 0

var count = 1
var lineBuffer = ""
var current = 0
var linesCount = 1

while (current < line.length) {
val next = min(current + wrapArea, line.length)
buf += line.substring(current, next)
lineBuffer += line.substring(current, next)

if (next < line.length) {
count += 1
buf += " ".repeat(header) //
linesCount += 1
lineBuffer += " ".repeat(header)
}

current = next
}

val sx = getmaxx(pad)
val fitsWidthPrecisely = (lineBuffer.length + header) % sx == 0

val s = if ((buf.length + header) % sx == 0) {
buf
} else {
buf + "\n"
}
val lineBufferPlus =
if (fitsWidthPrecisely) {
lineBuffer
} else {
lineBuffer + "\n"
}

return s to count
return lineBufferPlus to linesCount
}
8 changes: 0 additions & 8 deletions src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class LogLinesPresenter(
.distinctUntilChanged()
.collect {
if (it) {
Logger.d("AUTOSCROLL: $it")
view.end()
}
}
Expand All @@ -68,7 +67,6 @@ class LogLinesPresenter(

@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun collectLogLines() {
var i = 0
dogcat
.state
.flatMapLatest {
Expand Down Expand Up @@ -97,12 +95,6 @@ class LogLinesPresenter(
}
.buffer(0) //omg!
.collect {
if (i < 20) {
Logger.d("${context()} ${it.index} ll")

i++
}

view.processLogLine(it)
}
}
Expand Down
Loading

0 comments on commit f99f2da

Please sign in to comment.