Skip to content

Commit

Permalink
Rename Terminal.info to terminalInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Sep 7, 2024
1 parent 5ae773d commit 3ca2acd
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 173 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Renamed `TerminalInfo.crClearsLine` to `supportsAnsiCursor`
- Combined all `ColumnWidth` subclasses into a single class with factory methods. If you were using `ColumnWidth.Custom`, you should now use the `ColumnWidth` constructor.
- The following `Terminal` methods are now extensions: `prompt()`, `info()`, `danger()`, `warning()`, `success()`, `muted()`
- Renamed `Terminal.info` property to `Terminal.terminalInfo`

### Removed
- Removed constructor overloads for `Terminal`. There is now one constructor with all default parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Markdown(
private var document: Widget? = null
private fun document(t: Terminal): Widget {
if (document == null) {
document = MarkdownRenderer(markdown, t.theme, showHtml, hyperlinks ?: t.info.ansiHyperLinks).render()
document = MarkdownRenderer(markdown, t.theme, showHtml, hyperlinks ?: t.terminalInfo.ansiHyperLinks).render()
}
return document!!
}
Expand Down
196 changes: 40 additions & 156 deletions mordant/api/mordant.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ abstract class Animation<T>(
text = terminal.render(rendered)
)
}
if (!old.interceptorInstalled && terminal.info.outputInteractive) {
if (!old.interceptorInstalled && terminal.terminalInfo.outputInteractive) {
terminal.addInterceptor(interceptor)
}
// Print an empty widget to trigger our interceptor, which will add the rendered text
Expand All @@ -169,7 +169,7 @@ abstract class Animation<T>(
startOfLine()
if (CR_IMPLIES_LF) up(1)

if (terminal.info.supportsAnsiCursor) {
if (terminal.terminalInfo.supportsAnsiCursor) {
// IntelliJ doesn't support cursor moves, so this is all we can do
return@getMoves
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.time.TimeSource
* @throws RuntimeException if the terminal is not interactive or raw mode cannot be entered.
*/
fun Terminal.enterRawMode(mouseTracking: MouseTracking = MouseTracking.Off): RawModeScope {
if (!info.inputInteractive) {
if (!terminalInfo.inputInteractive) {
throw IllegalStateException("Cannot enter raw mode on a non-interactive terminal")
}
return RawModeScope(this, terminalInterface.enterRawMode(mouseTracking), mouseTracking)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Terminal private constructor(
private val nonInteractiveWidth: Int?,
private val nonInteractiveHeight: Int?,
/** The terminal capabilities that were detected or set in the constructor. */
val info: TerminalInfo,
val terminalInfo: TerminalInfo,
) {

/**
Expand Down Expand Up @@ -65,7 +65,7 @@ class Terminal private constructor(
forceHeight = height,
nonInteractiveWidth = nonInteractiveWidth,
nonInteractiveHeight = nonInteractiveHeight,
info = terminalInterface.info(
terminalInfo = terminalInterface.info(
ansiLevel = ansiLevel,
hyperlinks = hyperlinks,
outputInteractive = interactive,
Expand All @@ -79,7 +79,7 @@ class Terminal private constructor(
private val atomicSize: MppAtomicRef<Size> =
MppAtomicRef(
terminalInterface.detectSize(
info,
terminalInfo,
forceWidth,
forceHeight,
nonInteractiveWidth,
Expand All @@ -103,7 +103,7 @@ class Terminal private constructor(
fun updateSize(): Size {
return atomicSize.update {
terminalInterface.detectSize(
info,
terminalInfo,
forceWidth,
forceHeight,
nonInteractiveWidth,
Expand All @@ -118,15 +118,15 @@ class Terminal private constructor(
* If the terminal is not interactive, all the cursor functions are no-ops.
*/
val cursor: TerminalCursor = when {
info.interactive -> makePrintingTerminalCursor(this)
terminalInfo.interactive -> makePrintingTerminalCursor(this)
else -> DisabledTerminalCursor
}

/**
* Print a [message] to the terminal.
*
* Any contained [TextColors] and [TextStyles] will be automatically downsampled based on the
* current terminal's [info].
* current terminal's [terminalInfo].
*
* @param message The message to print as a string.
* @param whitespace How to handle whitespace and line wrapping. By default, whitespace is
Expand All @@ -152,7 +152,7 @@ class Terminal private constructor(
* Print a [message] to the terminal, followed by a line break.
*
* Any contained [TextColors] and [TextStyles] will be automatically downsampled based on the
* current terminal's [info].
* current terminal's [terminalInfo].
*
* @param message The message to print as a string.
* @param whitespace How to handle whitespace and line wrapping. By default, whitespace is
Expand Down Expand Up @@ -200,7 +200,7 @@ class Terminal private constructor(
* Render a [message] to a string.
*
* Any contained [TextColors] and [TextStyles] will be automatically downsampled based on the
* current terminal's [info].
* current terminal's [terminalInfo].
*
* @param message The message to render as a string.
* @param whitespace How to handle whitespace and line wrapping. By default, whitespace is
Expand All @@ -218,15 +218,15 @@ class Terminal private constructor(
width: Int? = null,
): String {
return when (message) {
is Lines -> renderLinesAnsi(message, info.ansiLevel, info.ansiHyperLinks)
is Lines -> renderLinesAnsi(message, terminalInfo.ansiLevel, terminalInfo.ansiHyperLinks)
is Widget -> render(message)
else -> render(Text(message.toString(), whitespace, align, overflowWrap, width))
}
}

/** Render a [widget] as a string */
fun render(widget: Widget): String {
return renderLinesAnsi(widget.render(this), info.ansiLevel, info.ansiHyperLinks)
return renderLinesAnsi(widget.render(this), terminalInfo.ansiLevel, terminalInfo.ansiHyperLinks)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PromptTest {

@[Test JsName("custom_Prompt")]
fun `custom Prompt`() {
t.info.ansiLevel = AnsiLevel.NONE
t.terminalInfo.ansiLevel = AnsiLevel.NONE
class IntPrompt : Prompt<Int>("pr", t) {
override fun convert(input: String): ConversionResult<Int> {
return input.toIntOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun main() {
val theme = terminal.theme
terminal.println(
Panel(
Text(terminal.info.toString(), whitespace = NORMAL).withPadding(1),
Text(terminal.terminalInfo.toString(), whitespace = NORMAL).withPadding(1),
Text(theme.info("Detected Terminal Info"))
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fun main(args: Array<String>) {
}

// 4 chars per octet, -20 for borders + address
val w = (terminal.info.width - 20) / 4
val w = (terminal.size.width - 20) / 4
// round down to nearest multiple of 8
val octetsPerRow = (w - w % 8).coerceAtLeast(1)

Expand Down

0 comments on commit 3ca2acd

Please sign in to comment.