Skip to content

Commit

Permalink
Add a detection sample (#126)
Browse files Browse the repository at this point in the history
* Turn `TerminalInfo` into a `data class`

This allows to easily compare instances and benefit from an
auto-generated `toString()` representation.

The downside is that `width` and `height` could now be set from the
outside after construction, but that is probably not too much of an
issue as arbitrary values could also have been passed via the
constructor initially anyway.

* Add a basic sample to show information for issue reports

Later on printing of selected environment variables or similar useful
output could be added.
  • Loading branch information
sschuberth authored Sep 18, 2023
1 parent e7d9ef1 commit 44de090
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import com.github.ajalt.mordant.rendering.AnsiLevel
* [width] and [height] don't automatically change if the terminal is resized. Call
* [updateTerminalSize] to get the latest values.
*/
class TerminalInfo(
data class TerminalInfo(
/** The terminal width, in cells */
width: Int,
var width: Int,
/** The terminal height, in cells */
height: Int,
var height: Int,
/** The level of ANSI codes to use when printing to the terminal */
var ansiLevel: AnsiLevel,
/** If true, ANSI hyperlink codes can be used */
Expand All @@ -34,14 +34,6 @@ class TerminalInfo(
*/
val crClearsLine: Boolean,
) {
/** The terminal width, in cells */
var width: Int = width
private set
/** The terminal height, in cells */
var height: Int = height
private set


/** Return true if both input and output are interactive */
val interactive: Boolean get() = inputInteractive && outputInteractive

Expand Down
3 changes: 3 additions & 0 deletions samples/detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Detection Sample

This sample shows the detected terminal, e.g. to collect information for issue reports.
35 changes: 35 additions & 0 deletions samples/detection/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
kotlin("multiplatform")
application
}

kotlin {
jvm {
withJava()
}

macosX64()
linuxX64()
mingwX64()

targets.filterIsInstance<KotlinNativeTarget>().forEach { target ->
target.binaries.executable {
entryPoint = "com.github.ajalt.mordant.samples.main"
}
}

sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":mordant"))
}
}
}
}

application {
mainClass.set("com.github.ajalt.mordant.samples.MainKt")
applicationDefaultJvmArgs = listOf("-Dfile.encoding=utf-8")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.ajalt.mordant.samples

import com.github.ajalt.mordant.rendering.Theme
import com.github.ajalt.mordant.terminal.Terminal

fun main() {
val terminal = Terminal()
terminal.println(Theme.Default.info(terminal.info.toString()))
terminal.println(
"Theme colors: " +
"${Theme.Default.success("success")}, " +
"${Theme.Default.danger("danger")}, " +
"${Theme.Default.warning("warning")}, " +
"${Theme.Default.info("info")}, " +
"${Theme.Default.muted("muted")}"
)
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include("mordant")
include("samples:detection")
include("samples:markdown")
include("samples:progress")
include("samples:table")
Expand Down

0 comments on commit 44de090

Please sign in to comment.