Skip to content

Commit

Permalink
Add progress sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Feb 2, 2021
1 parent e98ea22 commit dacac1c
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 14 deletions.
Binary file added .github/example_progress.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,6 @@ t.println(rgb("#b4eeb4")("This will get downsampled on terminals that don't supp
![](.github/example_rgb.png)
<p></p>

```kotlin
for (v in 0..100 step 4) {
for (h in 0..360 step 4) {
t.print(hsv(h, 100, 100 - v).bg(" "))
}
t.println()
}
```

![](.github/example_hsv.png)

### Terminal color support detection

By default, `Terminal()` will try to detect ANSI support in the current stdout stream. If you'd
Expand Down Expand Up @@ -213,6 +202,27 @@ repeat(120) {

![](.github/animation.svg)

## Progress bars

You can create customizable progress bars that automatically compute speed and time remaining.

```kotlin
val t = Terminal()
val progress = t.progressAnimation {
text("my-file.iso")
percentage()
progressBar()
completed()
speed("B/s")
timeRemaining()
}
```

![](.github/example_progress.png)

Call `progress.start` to animate the progress, and `progress.update` or `progress.advance` as your
task completes.

## Installation

Mordant is distributed through Maven Central.
Expand Down
3 changes: 3 additions & 0 deletions samples/markdown/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Markdown Sample

This sample is a command-line application that takes the path to a markdown file as an argument and renders that file to the terminal.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

application {
mainClass.set( "com.github.ajalt.mordant.samples.MainKt")
mainClass.set("com.github.ajalt.mordant.samples.MainKt")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.github.ajalt.mordant.terminal.Terminal
import java.io.File

fun main(args: Array<String>) {
val terminal = Terminal()
val path = args.singleOrNull() ?: error("must specify a markdown file")
Terminal().println(Markdown(File(path).readText()))
val markdown = File(path).readText()
val widget = Markdown(markdown)
terminal.println(widget)
}
3 changes: 3 additions & 0 deletions samples/progress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Progress Sample

This sample shows a simulated wget-style download progress.
21 changes: 21 additions & 0 deletions samples/progress/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
application
kotlin("jvm")
}

application {
mainClass.set("com.github.ajalt.mordant.samples.MainKt")
}

dependencies {
api(kotlin("stdlib"))
api(project(":mordant"))
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "1.8"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.ajalt.mordant.samples

import com.github.ajalt.mordant.animation.progressAnimation
import com.github.ajalt.mordant.terminal.Terminal

fun main() {
val terminal = Terminal()

// Detect the terminal size so our progress bar is as wide as the screen
terminal.info.updateTerminalSize()

val progress = terminal.progressAnimation {
text("my-file.bin")
percentage()
progressBar()
completed()
speed("B/s")
timeRemaining()
}

progress.start()

// Sleep for a few seconds to show the indeterminate state
Thread.sleep(5000)

// Update the progress as the download progresses
progress.updateTotal(3_000_000_000)
repeat(200) {
progress.advance(15_000_000)
Thread.sleep(100)
}

progress.stop()
}
4 changes: 3 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
include("mordant", "samples")
include("mordant")
include("samples:markdown")
include("samples:progress")

0 comments on commit dacac1c

Please sign in to comment.