Skip to content

Commit

Permalink
Add Help menu item to status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseDreki committed May 23, 2024
1 parent fd1981f commit dc5c384
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/nativeMain/kotlin/com/norsedreki/dogcat/app/Keymap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object Keymap {
@OptIn(ExperimentalForeignApi::class)
val bindings =
mapOf(
'h'.code to HELP,
'?'.code to HELP,
'r'.code to AUTOSCROLL,
'q'.code to QUIT,
'f'.code to INPUT_FILTER_BY_SUBSTRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ncurses.COLOR_RED
import ncurses.COLOR_WHITE
import ncurses.COLOR_YELLOW

@Suppress("detekt.MagicNumber")
@ExperimentalForeignApi
enum class CommonColors(
val colorPairCode: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ import com.norsedreki.dogcat.app.ui.Input
import kotlin.coroutines.coroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.yield

class HelpPresenter(
private val input: Input,
private val appState: AppState,
) : HasLifecycle {

private lateinit var view: HelpView

private var showing = false

override suspend fun start() {
val scope = CoroutineScope(coroutineContext)

Expand All @@ -34,6 +31,10 @@ class HelpPresenter(
}

private suspend fun collectKeypresses() {
lateinit var view: HelpView

var showing = false

input.keypresses.collect { key ->
when (Keymap.bindings[key]) {
HELP -> {
Expand All @@ -45,6 +46,7 @@ class HelpPresenter(
view = HelpView()
view.start()

yield()
view.state = HelpView.State(h)

showing = true
Expand All @@ -55,7 +57,6 @@ class HelpPresenter(
appState.holdUi(false)
}
}

else -> {
// Other keys are handled elsewhere
}
Expand Down
37 changes: 23 additions & 14 deletions src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package com.norsedreki.dogcat.app.ui.help

import com.norsedreki.dogcat.app.ui.HasLifecycle
import com.norsedreki.logger.Logger
import kotlin.properties.Delegates
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
Expand All @@ -26,9 +25,7 @@ import ncurses.wresize
@OptIn(ExperimentalForeignApi::class)
class HelpView : HasLifecycle {

data class State(
val text: List<String> = listOf()
)
data class State(val text: List<String> = listOf())

var state: State by Delegates.observable(State()) { _, _, newValue -> updateView(newValue) }

Expand All @@ -38,22 +35,26 @@ class HelpView : HasLifecycle {
val sy = getmaxy(stdscr) / 2
val sx = getmaxx(stdscr) / 2


window = newwin(1, 1, sy, sx)!!
werase(window) // clear the window
wrefresh(window) // refresh the window to apply the clearing

}

override suspend fun stop() {
//werase(window)
delwin(window)
}

private fun updateView(state: State) {
val padding = 2 // adjust this value as needed
val maxWidth = state.text.maxOf { it.length } + padding * 2
val height = state.text.size + padding * 2
val horizontalPadding = 4 // adjust this value as needed
val verticalPadding = 2 // adjust this value as needed

val maxWidth =
maxOf(state.text.maxOf { it.length }, "Help: hotkeys".length) + horizontalPadding * 2

val height =
state.text.size +
verticalPadding * 2 +
2 // add extra lines for the title and blank line

val sy = getmaxy(stdscr)
val sx = getmaxx(stdscr)
Expand All @@ -63,15 +64,23 @@ class HelpView : HasLifecycle {

wresize(window, height, maxWidth)
mvwin(window, startY, startX)
Logger.d("UPDATE HELP VIEW: $startX, $startY, $maxWidth, $height")
box(window, 0U, 0U) // draw a box around the window

// Add title
val title = "Help: hotkeys"
val titleStartX = (maxWidth - title.length) / 2
mvwaddstr(window, verticalPadding, titleStartX, title)

// Add text
state.text.forEachIndexed { index, line ->
mvwaddstr(window, index + padding, padding, line)
mvwaddstr(
window,
index + verticalPadding + 2,
horizontalPadding,
line,
) // start from the third line
}

wrefresh(window)

Logger.d("HELP view refreshed")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class StatusView : HasLifecycle {
updatePackageName(state.packageName)
updateFilters(state.filters)
updateAutoscroll(state.autoscroll)
updateHelp()

wrefresh(window)

Expand Down Expand Up @@ -135,6 +136,20 @@ class StatusView : HasLifecycle {
wattroff(window, COLOR_PAIR(BLACK_ON_WHITE.colorPairCode))
}

private fun updateHelp() {
//wattron(window, COLOR_PAIR(GREEN_ON_WHITE.colorPairCode))
wattron(window, COLOR_PAIR(BLACK_ON_WHITE.colorPairCode))
//wattron(window, A_BOLD.toInt())

val s = "| (?) Help"
val offsetX = STATUS_VIEW_AUTOSCROLL_LEFT_MARGIN + NO_AUTOSCROLL.length + 2
mvwprintw(window, 0, offsetX, s)

//wattroff(window, A_BOLD.toInt())
wattroff(window, COLOR_PAIR(BLACK_ON_WHITE.colorPairCode))
//wattroff(window, COLOR_PAIR(GREEN_ON_WHITE.colorPairCode))
}

private fun updateDevice(device: String, running: Boolean) {
curs_set(0)

Expand Down

0 comments on commit dc5c384

Please sign in to comment.