|
| 1 | +import org.jline.terminal.TerminalBuilder |
| 2 | + |
| 3 | +/** |
| 4 | + * Progress bar used reporting the status while checking and downloading libs. |
| 5 | + * |
| 6 | + * @param max count of events e.g. length of the list which progress is monitored. |
| 7 | + */ |
| 8 | +class ProgressBar(max: Int) { |
| 9 | + // Width of the terminal, used for size calculations |
| 10 | + private val width = { |
| 11 | + val width = TerminalBuilder.builder().dumb(true).build().getWidth |
| 12 | + |
| 13 | + // Size couldn't be figured out, use a default |
| 14 | + if (width <= 10) |
| 15 | + 80 |
| 16 | + else |
| 17 | + width |
| 18 | + } |
| 19 | + |
| 20 | + private var count = 0 |
| 21 | + private var description = "" |
| 22 | + |
| 23 | + // We need to create a empty line so that latest line before creation won't be overwritten by the draw method. |
| 24 | + println() |
| 25 | + draw() // Initial draw |
| 26 | + |
| 27 | + /** |
| 28 | + * Increases count by 1 and re-draws the progress bar with the updated count. |
| 29 | + */ |
| 30 | + def countUp(): Unit = { |
| 31 | + // Thread-safeness when working with parallel collections |
| 32 | + count.synchronized { |
| 33 | + count += 1 |
| 34 | + draw() |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Updates the description and re-draws the description line. |
| 40 | + * @param desc the new description |
| 41 | + */ |
| 42 | + def updateDescription(desc: String): Unit = { |
| 43 | + // Thread-safeness when working with parallel collections |
| 44 | + description.synchronized { |
| 45 | + description = desc |
| 46 | + drawDescription() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Deletes the description to result in a blank line. The progress bar will still be visible. |
| 52 | + * After this you can normally print at the beginning of a new line and don't start at the end of the description. |
| 53 | + */ |
| 54 | + def finish(): Unit = { |
| 55 | + description = "" |
| 56 | + drawDescription() |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Draws the progress bar in the line above the current one. |
| 61 | + */ |
| 62 | + private def draw(): Unit = { |
| 63 | + val barWidth = width - 16 // Width of the bar without percentage and counts |
| 64 | + val percentage = count * 100 / max |
| 65 | + val equalsSigns = "=" * (barWidth * percentage / 100) |
| 66 | + val whiteSpaces = " " * (barWidth - equalsSigns.length) |
| 67 | + |
| 68 | + val content = "%3d%% (%2d|%2d) [%s]".format(percentage, count, max, equalsSigns + ">" + whiteSpaces) |
| 69 | + |
| 70 | + print(s"\033[1A\r$content\n") |
| 71 | + // | | | | |
| 72 | + // Go up 1 line | | | |
| 73 | + // Go to beginning of line |
| 74 | + // | | |
| 75 | + // Actual progress bar |
| 76 | + // | |
| 77 | + // Go back down for description |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Draws the description which is located in the current line. |
| 82 | + */ |
| 83 | + private def drawDescription(): Unit = { |
| 84 | + // Cap the description at the width of the terminal, otherwise a new line is created and everything would shift. |
| 85 | + // If the user needs to see a really long url he can just widen his terminal. |
| 86 | + val content = description.take(width) |
| 87 | + |
| 88 | + print(s"\r\033[0K$content") |
| 89 | + // | | |
| 90 | + // Go to beginning |
| 91 | + // | |
| 92 | + // Clear from cursor to end of the line |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments