Mosaic

repository·trunk·Indexed 25 days ago

https://github.com/jakewharton/mosaic

An experimental tool for building console-based user interfaces in Kotlin using the Jetpack Compose compiler and runtime. It includes a runtime for UI development, a TTY library for low-level terminal manipulation, a high-level terminal interface, and an animation library adapted from AndroidX Compose.

Tokens
1.6K
Snippets
4
Records
13
Agent score
83%

What's inside Mosaic

  1. Overview of Mosaic Animation

    trunk

    Mosaic Animation is a library adapted from the androidx.compose.animation-core and androidx.compose.animation AndroidX Compose libraries. It provides animation capabilities specifically tailored for the Mosaic framework.

    Currently, the library includes:

    • Core animation logic adapted from androidx.compose.animation-core.
    • Color animation functionality adapted from androidx.compose.animation.

    Note that certain Android-specific types, such as Dp, have been removed to ensure compatibility with Mosaic.

  2. Use Mosaic snapshots for development

    trunk

    If you need to use the latest development snapshots, configure your repository to include the Sonatype Central Portal Snapshots repository and use the 0.19.0-SNAPSHOT dependency.

    repository {
      mavenCentral()
      maven {
        url 'https://central.sonatype.com/repository/maven-snapshots/'
      }
    }
    dependencies {
      implementation("com.jakewharton.mosaic:mosaic-runtime:0.19.0-SNAPSHOT")
    }
  3. Run Mosaic TTY tests on Windows without input interference

    trunk

    When testing on Windows, running a console application directly can cause regular key and mouse input to interfere with the test. To avoid this, run the compiled .exe in a minimized command prompt window. You can use start /min cmd /k to launch the process and append a system beep (via rundll32 user32.dll,MessageBeep) to signal when the test has finished, allowing you to inspect the results manually.

    start /min cmd /k "Z:\test.exe & rundll32 user32.dll,MessageBeep"
  4. Install Mosaic runtime

    trunk

    Mosaic requires the JetBrains Kotlin Compose plugin to be applied to your module. Once the plugin is configured, add the mosaic-runtime dependency to your project.

    To use the stable version, add the following to your dependencies block:

    implementation("com.jakewharton.mosaic:mosaic-runtime:0.18.0")
    dependencies {
      implementation("com.jakewharton.mosaic:mosaic-runtime:0.18.0")
    }
  5. Troubleshoot broken Mosaic output

    trunk

    If you see output rendering in successive lines instead of redrawing over the previous frame, it is likely because you are running the application via ./gradlew run or inside an IDE like IntelliJ IDEA.

    These environments do not provide access to the TTY and strip ANSI control characters. To ensure correct rendering and interactivity, run your programs directly in a terminal emulator without using Gradle or an IDE.

  6. Create a Mosaic application with runMosaic

    trunk

    The entrypoint for a Mosaic application is the runMosaic function. The lambda passed to runMosaic is responsible for both the UI output and performing background work. You can use Compose state management (like remember and mutableIntStateOf) to update the console UI dynamically.

    import androidx.compose.runtime.getValue
    import androidx.compose.runtime.setValue
    import androidx.compose.runtime.mutableIntStateOf
    import androidx.compose.runtime.remember
    import kotlinx.coroutines.delay
    import kotlin.time.Duration.Companion.milliseconds
    
    suspend fun main() = runMosaic {
      var count by remember { mutableIntStateOf(0) }
    
      Text("The count is: $count")
    
      LaunchedEffect(Unit) {
        for (i in 1..20) {
          delay(250.milliseconds)
          count = i
        }
      }
    }
  7. Convert a TTY to a Terminal using asTerminalIn()

    trunk

    Use the asTerminalIn extension function on a Tty instance to transform it into a high-level Terminal. This function handles raw mode, capability querying (including Kitty-specific features), and state management (focus, theme, size). It is a suspending function that bootstraps terminal capabilities within a 1-second timeout.

    To enable detailed debugging of the capability bootstrapping process, set the environment variable MOSAIC_TTY_TERMINAL_DEBUG to "true".