2021-01-27 13:52:39 +01:00
|
|
|
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
2020-11-26 00:28:57 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-11-12 12:12:51 +01:00
|
|
|
module ui
|
|
|
|
|
|
|
|
import strings
|
|
|
|
|
|
|
|
pub struct Color {
|
|
|
|
pub:
|
|
|
|
r byte
|
|
|
|
g byte
|
|
|
|
b byte
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c Color) hex() string {
|
2020-11-17 15:08:35 +01:00
|
|
|
return '#$c.r.hex()$c.g.hex()$c.b.hex()'
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronized Updates spec, designed to avoid tearing during renders
|
|
|
|
// https://gitlab.com/gnachman/iterm2/-/wikis/synchronized-updates-spec
|
2021-09-06 16:24:39 +02:00
|
|
|
const bsu = '\x1bP=1s\x1b\\'
|
|
|
|
|
|
|
|
const esu = '\x1bP=2s\x1b\\'
|
2020-11-12 12:12:51 +01:00
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// write puts the string `s` into the print buffer.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) write(s string) {
|
2020-11-17 15:08:35 +01:00
|
|
|
if s == '' {
|
|
|
|
return
|
|
|
|
}
|
2021-02-26 22:55:09 +01:00
|
|
|
unsafe { ctx.print_buf.push_many(s.str, s.len) }
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// flush displays the accumulated print buffer to the screen.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) flush() {
|
2021-01-27 13:52:39 +01:00
|
|
|
// TODO: Diff the previous frame against this one, and only render things that changed?
|
|
|
|
if !ctx.enable_su {
|
|
|
|
C.write(1, ctx.print_buf.data, ctx.print_buf.len)
|
|
|
|
} else {
|
2021-05-08 12:32:29 +02:00
|
|
|
C.write(1, ui.bsu.str, ui.bsu.len)
|
2021-01-27 13:52:39 +01:00
|
|
|
C.write(1, ctx.print_buf.data, ctx.print_buf.len)
|
2021-05-08 12:32:29 +02:00
|
|
|
C.write(1, ui.esu.str, ui.esu.len)
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|
2021-01-27 13:52:39 +01:00
|
|
|
ctx.print_buf.clear()
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// bold sets the character state to bold.
|
2020-11-12 12:12:51 +01:00
|
|
|
[inline]
|
|
|
|
pub fn (mut ctx Context) bold() {
|
|
|
|
ctx.write('\x1b[1m')
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// set_cursor_position positions the cusor at the given coordinates `x`,`y`.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) set_cursor_position(x int, y int) {
|
|
|
|
ctx.write('\x1b[$y;${x}H')
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:31:26 +01:00
|
|
|
// show_cursor will make the cursor appear if it is not already visible
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2021-03-04 11:31:26 +01:00
|
|
|
pub fn (mut ctx Context) show_cursor() {
|
|
|
|
ctx.write('\x1b[?25h')
|
|
|
|
}
|
|
|
|
|
|
|
|
// hide_cursor will make the cursor invisible
|
|
|
|
[inline]
|
|
|
|
pub fn (mut ctx Context) hide_cursor() {
|
|
|
|
ctx.write('\x1b[?25l')
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// set_color sets the current foreground color used by any succeeding `draw_*` calls.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) set_color(c Color) {
|
2020-11-26 00:28:57 +01:00
|
|
|
if ctx.enable_rgb {
|
|
|
|
ctx.write('\x1b[38;2;${int(c.r)};${int(c.g)};${int(c.b)}m')
|
|
|
|
} else {
|
|
|
|
ctx.write('\x1b[38;5;${rgb2ansi(c.r, c.g, c.b)}m')
|
|
|
|
}
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// set_color sets the current background color used by any succeeding `draw_*` calls.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) set_bg_color(c Color) {
|
2020-11-26 00:28:57 +01:00
|
|
|
if ctx.enable_rgb {
|
|
|
|
ctx.write('\x1b[48;2;${int(c.r)};${int(c.g)};${int(c.b)}m')
|
|
|
|
} else {
|
|
|
|
ctx.write('\x1b[48;5;${rgb2ansi(c.r, c.g, c.b)}m')
|
|
|
|
}
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// reset_color sets the current foreground color back to it's default value.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) reset_color() {
|
|
|
|
ctx.write('\x1b[39m')
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// reset_bg_color sets the current background color back to it's default value.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) reset_bg_color() {
|
|
|
|
ctx.write('\x1b[49m')
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// reset restores the state of all colors and text formats back to their default values.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) reset() {
|
|
|
|
ctx.write('\x1b[0m')
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn (mut ctx Context) clear() {
|
|
|
|
ctx.write('\x1b[2J\x1b[3J')
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// set_window_title sets the string `s` as the window title.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-13 14:30:47 +01:00
|
|
|
pub fn (mut ctx Context) set_window_title(s string) {
|
|
|
|
print('\x1b]0;$s\x07')
|
|
|
|
}
|
2020-11-12 12:12:51 +01:00
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// draw_point draws a point at position `x`,`y`.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) draw_point(x int, y int) {
|
|
|
|
ctx.set_cursor_position(x, y)
|
|
|
|
ctx.write(' ')
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// draw_text draws the string `s`, starting from position `x`,`y`.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) draw_text(x int, y int, s string) {
|
|
|
|
ctx.set_cursor_position(x, y)
|
|
|
|
ctx.write(s)
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// draw_line draws a line segment, starting at point `x`,`y`, and ending at point `x2`,`y2`.
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) draw_line(x int, y int, x2 int, y2 int) {
|
|
|
|
min_x, min_y := if x < x2 { x } else { x2 }, if y < y2 { y } else { y2 }
|
|
|
|
max_x, _ := if x > x2 { x } else { x2 }, if y > y2 { y } else { y2 }
|
|
|
|
if y == y2 {
|
|
|
|
// Horizontal line, performance improvement
|
|
|
|
ctx.set_cursor_position(min_x, min_y)
|
|
|
|
ctx.write(strings.repeat(` `, max_x + 1 - min_x))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Draw the various points with Bresenham's line algorithm:
|
|
|
|
mut x0, x1 := x, x2
|
|
|
|
mut y0, y1 := y, y2
|
|
|
|
sx := if x0 < x1 { 1 } else { -1 }
|
|
|
|
sy := if y0 < y1 { 1 } else { -1 }
|
|
|
|
dx := if x0 < x1 { x1 - x0 } else { x0 - x1 }
|
|
|
|
dy := if y0 < y1 { y0 - y1 } else { y1 - y0 } // reversed
|
|
|
|
mut err := dx + dy
|
|
|
|
for {
|
|
|
|
// res << Segment{ x0, y0 }
|
|
|
|
ctx.draw_point(x0, y0)
|
2020-11-17 15:08:35 +01:00
|
|
|
if x0 == x1 && y0 == y1 {
|
|
|
|
break
|
|
|
|
}
|
2020-11-12 12:12:51 +01:00
|
|
|
e2 := 2 * err
|
|
|
|
if e2 >= dy {
|
|
|
|
err += dy
|
|
|
|
x0 += sx
|
|
|
|
}
|
|
|
|
if e2 <= dx {
|
|
|
|
err += dx
|
|
|
|
y0 += sy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// draw_dashed_line draws a dashed line segment, starting at point `x`,`y`, and ending at point `x2`,`y2`.
|
2020-11-17 15:08:35 +01:00
|
|
|
pub fn (mut ctx Context) draw_dashed_line(x int, y int, x2 int, y2 int) {
|
|
|
|
// Draw the various points with Bresenham's line algorithm:
|
|
|
|
mut x0, x1 := x, x2
|
|
|
|
mut y0, y1 := y, y2
|
|
|
|
sx := if x0 < x1 { 1 } else { -1 }
|
|
|
|
sy := if y0 < y1 { 1 } else { -1 }
|
|
|
|
dx := if x0 < x1 { x1 - x0 } else { x0 - x1 }
|
|
|
|
dy := if y0 < y1 { y0 - y1 } else { y1 - y0 } // reversed
|
|
|
|
mut err := dx + dy
|
|
|
|
mut i := 0
|
|
|
|
for {
|
|
|
|
if i % 2 == 0 {
|
|
|
|
ctx.draw_point(x0, y0)
|
|
|
|
}
|
|
|
|
if x0 == x1 && y0 == y1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
e2 := 2 * err
|
|
|
|
if e2 >= dy {
|
|
|
|
err += dy
|
|
|
|
x0 += sx
|
|
|
|
}
|
|
|
|
if e2 <= dx {
|
|
|
|
err += dx
|
|
|
|
y0 += sy
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// draw_rect draws a rectangle, starting at top left `x`,`y`, and ending at bottom right `x2`,`y2`.
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) draw_rect(x int, y int, x2 int, y2 int) {
|
|
|
|
if y == y2 || x == x2 {
|
|
|
|
ctx.draw_line(x, y, x2, y2)
|
|
|
|
return
|
|
|
|
}
|
2020-11-17 15:08:35 +01:00
|
|
|
min_y, max_y := if y < y2 { y, y2 } else { y2, y }
|
2020-11-12 12:12:51 +01:00
|
|
|
for y_pos in min_y .. max_y + 1 {
|
|
|
|
ctx.draw_line(x, y_pos, x2, y_pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// draw_empty_dashed_rect draws a rectangle with dashed lines, starting at top left `x`,`y`, and ending at bottom right `x2`,`y2`.
|
2020-11-17 15:08:35 +01:00
|
|
|
pub fn (mut ctx Context) draw_empty_dashed_rect(x int, y int, x2 int, y2 int) {
|
|
|
|
if y == y2 || x == x2 {
|
|
|
|
ctx.draw_dashed_line(x, y, x2, y2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
min_x, max_x := if x < x2 { x, x2 } else { x2, x }
|
|
|
|
min_y, max_y := if y < y2 { y, y2 } else { y2, y }
|
|
|
|
|
|
|
|
ctx.draw_dashed_line(min_x, min_y, max_x, min_y)
|
|
|
|
ctx.draw_dashed_line(min_x, min_y, min_x, max_y)
|
|
|
|
if (max_y - min_y) & 1 == 0 {
|
|
|
|
ctx.draw_dashed_line(min_x, max_y, max_x, max_y)
|
|
|
|
} else {
|
|
|
|
ctx.draw_dashed_line(min_x + 1, max_y, max_x, max_y)
|
|
|
|
}
|
|
|
|
if (max_x - min_x) & 1 == 0 {
|
|
|
|
ctx.draw_dashed_line(max_x, min_y, max_x, max_y)
|
|
|
|
} else {
|
|
|
|
ctx.draw_dashed_line(max_x, min_y + 1, max_x, max_y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// draw_empty_rect draws a rectangle with no fill, starting at top left `x`,`y`, and ending at bottom right `x2`,`y2`.
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) draw_empty_rect(x int, y int, x2 int, y2 int) {
|
|
|
|
if y == y2 || x == x2 {
|
|
|
|
ctx.draw_line(x, y, x2, y2)
|
|
|
|
return
|
|
|
|
}
|
2020-11-17 15:08:35 +01:00
|
|
|
ctx.draw_line(x, y, x2, y)
|
|
|
|
ctx.draw_line(x, y2, x2, y2)
|
|
|
|
ctx.draw_line(x, y, x, y2)
|
|
|
|
ctx.draw_line(x2, y, x2, y2)
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 07:32:04 +01:00
|
|
|
// horizontal_separator draws a horizontal separator, spanning the width of the screen.
|
2021-05-08 12:32:29 +02:00
|
|
|
[inline]
|
2020-11-12 12:12:51 +01:00
|
|
|
pub fn (mut ctx Context) horizontal_separator(y int) {
|
|
|
|
ctx.set_cursor_position(0, y)
|
2021-05-08 12:32:29 +02:00
|
|
|
ctx.write(strings.repeat(`-`, ctx.window_width)) // /* `⎽` */
|
2020-11-12 12:12:51 +01:00
|
|
|
}
|