Skip to content

Commit

Permalink
Organize app colors
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseDreki committed Feb 26, 2024
1 parent 734cbe3 commit 1d64032
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 219 deletions.
4 changes: 3 additions & 1 deletion src/nativeMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun main(args: Array<String>) {
input
.keypresses
.filter {
Keymap.bindings[it] == Actions.Quit
Keymap.bindings[it] == Actions.QUIT
}
.onEach {
Logger.d("${context()} Cancel scope")
Expand All @@ -49,6 +49,8 @@ fun main(args: Array<String>) {
appJob.join()
}
ui.close()
//close presenter
//exit with nonzero upon exception

Logger.d("Exit!")
Logger.close()
Expand Down
20 changes: 10 additions & 10 deletions src/nativeMain/kotlin/ui/AppPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ class AppPresenter(
private suspend fun handleKeypress(keyCode: Int) {
when (Keymap.bindings[keyCode]) {

Autoscroll -> {
AUTOSCROLL -> {
appState.autoscroll(!appState.state.value.autoscroll)
}

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

ToggleFilterByPackage -> {
TOGGLE_FILTER_BY_PACKAGE -> {
val f = appState.state.value.packageFilter

if (f.second) {
Expand All @@ -116,31 +116,31 @@ class AppPresenter(
}
}

ResetFilterBySubstring -> {
RESET_FILTER_BY_SUBSTRING -> {
dogcat(ResetFilter(Substring::class))
}

ResetFilterByMinLogLevel -> {
RESET_FILTER_BY_MIN_LOG_LEVEL -> {
dogcat(ResetFilter(MinLogLevel::class))
}

MinLogLevelV -> {
MIN_LOG_LEVEL_V -> {
dogcat(FilterBy(MinLogLevel(V)))
}

MinLogLevelD -> {
MIN_LOG_LEVEL_D -> {
dogcat(FilterBy(MinLogLevel(D)))
}

MinLogLevelI -> {
MIN_LOG_LEVEL_I -> {
dogcat(FilterBy(MinLogLevel(I)))
}

MinLogLevelW -> {
MIN_LOG_LEVEL_W -> {
dogcat(FilterBy(MinLogLevel(W)))
}

MinLogLevelE -> {
MIN_LOG_LEVEL_E -> {
dogcat(FilterBy(MinLogLevel(E)))
}

Expand Down
45 changes: 15 additions & 30 deletions src/nativeMain/kotlin/ui/AppView.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ui

import dogcat.DogcatException
import kotlinx.cinterop.ExperimentalForeignApi
import ncurses.*
import platform.posix.LC_ALL
Expand All @@ -8,53 +9,37 @@ import platform.posix.printf
import platform.posix.setlocale

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

fun start() {
setlocale(LC_ALL, "en_US.UTF-8") // should be before initscr()
override suspend fun start() {
setlocale(LC_ALL, "en_US.UTF-8")
initscr()

//The keypad function enables the reading of function keys like arrow keys, Home, End, and so on.
keypad(stdscr, true);
noecho();

//intrflush(stdscr, false)

//nonl()

//nl()
//Use the ncurses functions for output. My guess is that initscr changes terminal settings such that \n only performs a line feed, not a carriage return. –
//melpomene

nodelay(stdscr, true) //The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed

// The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR.
// If disabled (bf is FALSE), getch waits until a key is pressed
nodelay(stdscr, true)
//cbreak or raw, to make wgetch read unbuffered data, i.e., not waiting for '\n'.
//nodelay or timeout, to control the amount of time wgetch spends waiting for input.

//??? enable
//cbreak() //making getch() work without a buffer I.E. raw characters

if (!has_colors()) {
endwin()
printf("Your terminal does not support color\n")

throw DogcatException("Your terminal does not support color")
//printf("Your terminal does not support color\n")
exit(1)
}

//idlok¶

use_default_colors()
start_color()
init_pair(1, COLOR_RED.toShort(), -1)
init_pair(2, COLOR_GREEN.toShort(), -1)//COLOR_BLACK.toShort())
init_pair(3, COLOR_YELLOW.toShort(), -1)
//init_pair(4, COLOR_CYAN.toShort(), COLOR_BLACK.toShort())

init_pair(11, COLOR_BLACK.toShort(), COLOR_RED.toShort())
init_pair(12, COLOR_BLACK.toShort(), COLOR_WHITE.toShort())
init_pair(6, COLOR_BLACK.toShort(), COLOR_YELLOW.toShort())

CommonColors.entries.forEach {
init_pair(it.colorPairCode.toShort(), it.foregroundColor, it.backgroundColor)
}
}

fun stop() {
override suspend fun stop() {
endwin()
}
}
89 changes: 0 additions & 89 deletions src/nativeMain/kotlin/ui/ColorMap.kt

This file was deleted.

23 changes: 23 additions & 0 deletions src/nativeMain/kotlin/ui/CommonColors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ui

import kotlinx.cinterop.ExperimentalForeignApi
import ncurses.*

@ExperimentalForeignApi
enum class CommonColors(
val colorPairCode: Int,
val foregroundColor: Short,
val backgroundColor: Short
) {

RED_ON_BG(1, COLOR_RED.toShort(), -1),
GREEN_ON_BG(2, COLOR_GREEN.toShort(), -1),
YELLOW_ON_BG(3, COLOR_YELLOW.toShort(), -1),

RED_ON_WHITE(4, COLOR_RED.toShort(), COLOR_WHITE.toShort()),
GREEN_ON_WHITE(5, COLOR_GREEN.toShort(), COLOR_WHITE.toShort()),

BLACK_ON_WHITE(6, COLOR_BLACK.toShort(), COLOR_WHITE.toShort()),
BLACK_ON_RED(7, COLOR_BLACK.toShort(), COLOR_RED.toShort()),
BLACK_ON_YELLOW(8, COLOR_BLACK.toShort(), COLOR_YELLOW.toShort())
}
13 changes: 7 additions & 6 deletions src/nativeMain/kotlin/ui/logLines/LogLineExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.coroutines.yield
import logger.Logger
import ncurses.*
import ui.CommonColors
import ui.CommonColors.*
import kotlin.math.min

@OptIn(ExperimentalForeignApi::class)
Expand All @@ -33,19 +35,18 @@ suspend fun LogLinesView.processLogLine(
val wrapped = wrappedLine.first
recordLine(wrappedLine.second)

val level = logLine.level
when (level) {
when (val level = logLine.level) {
W -> {
printLevelAndMessage(level.name, 6, wrapped, COLOR_PAIR(3))
printLevelAndMessage(level.name, BLACK_ON_YELLOW.colorPairCode, wrapped, COLOR_PAIR(YELLOW_ON_BG.colorPairCode))
}
E, F -> {
printLevelAndMessage(level.name, 11, wrapped, COLOR_PAIR(1))
printLevelAndMessage(level.name, BLACK_ON_RED.colorPairCode, wrapped, COLOR_PAIR(RED_ON_BG.colorPairCode))
}
I -> {
printLevelAndMessage(level.name, 12, wrapped, A_BOLD.toInt())
printLevelAndMessage(level.name, BLACK_ON_WHITE.colorPairCode, wrapped, A_BOLD.toInt())
}
else -> {
printLevelAndMessage(level.name, 12, wrapped, 0)
printLevelAndMessage(level.name, BLACK_ON_WHITE.colorPairCode, wrapped, 0)
}
}

Expand Down
17 changes: 6 additions & 11 deletions src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,38 +112,33 @@ class LogLinesPresenter(
.keypresses
.collect {
when (Keymap.bindings[it]) {
Home -> {
HOME -> {
appState.autoscroll(false)
//view.state = view.state.copy(autoscroll = false)
view.home()
}

End -> {
END -> {
appState.autoscroll(true)
//view.state = view.state.copy(autoscroll = true)
view.end()
}

LineUp -> {
LINE_UP -> {
appState.autoscroll(false)
//view.state = view.state.copy(autoscroll = false)
view.lineUp()
}

LineDown -> {
LINE_DOWN -> {
appState.autoscroll(false)
//view.state = view.state.copy(autoscroll = false)
view.lineDown(1)
}

PageDown -> {
PAGE_DOWN -> {
appState.autoscroll(false)
view.pageDown()
}

PageUp -> {
PAGE_UP -> {
appState.autoscroll(false)
//view.state = view.state.copy(autoscroll = false)
view.pageUp()
}

Expand Down
Loading

0 comments on commit 1d64032

Please sign in to comment.