1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: require calling optfn() ? / optfn() or {...} for fn optfn() ? {}

This commit is contained in:
Delyan Angelov
2021-01-26 16:43:10 +02:00
parent 97103f680a
commit e5a84719ca
90 changed files with 1994 additions and 1832 deletions

View File

@@ -2,12 +2,12 @@ import term.ui as tui
const (
colors = [
tui.Color{33, 150, 243}
tui.Color{0, 150, 136}
tui.Color{205, 220, 57}
tui.Color{255, 152, 0}
tui.Color{244, 67, 54}
tui.Color{156, 39, 176}
tui.Color{33, 150, 243},
tui.Color{0, 150, 136},
tui.Color{205, 220, 57},
tui.Color{255, 152, 0},
tui.Color{244, 67, 54},
tui.Color{156, 39, 176},
]
)
@@ -66,27 +66,33 @@ fn event(e &tui.Event, x voidptr) {
}
.space, .enter {
app.color_idx++
if app.color_idx == colors.len { app.color_idx = 0 }
if app.color_idx == colors.len {
app.color_idx = 0
}
app.color = colors[app.color_idx]
} else {}
}
else {}
}
} .mouse_move, .mouse_drag, .mouse_down {
app.points << Point{ e.x, e.y }
} .mouse_scroll {
}
.mouse_move, .mouse_drag, .mouse_down {
app.points << Point{e.x, e.y}
}
.mouse_scroll {
d := if e.direction == .up { 0.1 } else { -0.1 }
app.cut_rate += d
if app.cut_rate < 1 { app.cut_rate = 1 }
} else {}
if app.cut_rate < 1 {
app.cut_rate = 1
}
}
else {}
}
}
mut app := &App{}
app.tui = tui.init(
user_data: app,
frame_fn: frame,
event_fn: event,
user_data: app
frame_fn: frame
event_fn: event
hide_cursor: true
)
app.tui.run()
app.tui.run() ?

View File

@@ -11,29 +11,35 @@ fn event(e &tui.Event, x voidptr) {
app.tui.set_cursor_position(0, 0)
app.tui.write('V term.input event viewer (press `esc` to exit)\n\n')
app.tui.write('$e')
app.tui.write('\n\nRaw event bytes: "${e.utf8.bytes().hex()}" = ${e.utf8.bytes()}')
app.tui.write('\n\nRaw event bytes: "$e.utf8.bytes().hex()" = $e.utf8.bytes()')
if e.modifiers != 0 {
app.tui.write('\nModifiers: $e.modifiers = ')
if e.modifiers & tui.ctrl != 0 { app.tui.write('ctrl. ') }
if e.modifiers & tui.shift != 0 { app.tui.write('shift ') }
if e.modifiers & tui.alt != 0 { app.tui.write('alt. ') }
if e.modifiers & tui.ctrl != 0 {
app.tui.write('ctrl. ')
}
if e.modifiers & tui.shift != 0 {
app.tui.write('shift ')
}
if e.modifiers & tui.alt != 0 {
app.tui.write('alt. ')
}
}
app.tui.flush()
if e.typ == .key_down && e.code == .escape { exit(0) }
if e.typ == .key_down && e.code == .escape {
exit(0)
}
}
mut app := &App{}
app.tui = tui.init(
user_data: app,
user_data: app
event_fn: event
window_title: 'V term.ui event viewer'
hide_cursor: true
capture_events: true
frame_rate: 60
use_alternate_buffer: false
)
println('V term.ui event viewer (press `esc` to exit)\n\n')
app.tui.run()
app.tui.run() ?

View File

@@ -19,7 +19,7 @@ const (
struct App {
mut:
tui &ui.Context = 0
mode Mode = Mode.menu
mode Mode = Mode.menu
width int
height int
game &Game = 0
@@ -35,10 +35,10 @@ fn (mut a App) init() {
a.width = w
a.height = h
term.erase_del_clear()
term.set_cursor_position({
term.set_cursor_position(
x: 0
y: 0
})
)
}
fn (mut a App) start_game() {
@@ -66,10 +66,10 @@ fn (mut a App) quit() {
a.game.quit()
return
}
term.set_cursor_position({
term.set_cursor_position(
x: 0
y: 0
})
)
exit(0)
}
@@ -482,7 +482,7 @@ fn event(e &ui.Event, x voidptr) {
// main
mut app := &App{}
app.tui = ui.init({
app.tui = ui.init(
user_data: app
init_fn: init
frame_fn: frame
@@ -492,5 +492,5 @@ app.tui = ui.init({
capture_events: true
hide_cursor: true
frame_rate: 60
})
app.tui.run()
)
app.tui.run() ?

View File

@@ -34,8 +34,8 @@ fn event(e &tui.Event, x voidptr) {
app.is_drag = true
app.cur_rect = {
c: random_color()
x: e.x
y: e.y
x: e.x
y: e.y
x2: e.x
y2: e.y
}
@@ -43,20 +43,28 @@ fn event(e &tui.Event, x voidptr) {
.mouse_drag {
app.cur_rect.x2 = e.x
app.cur_rect.y2 = e.y
} .mouse_up {
}
.mouse_up {
app.rects << app.cur_rect
app.is_drag = false
} .key_down {
if e.code == .c { app.rects.clear() }
else if e.code == .escape { exit(0) }
} else {}
}
.key_down {
if e.code == .c {
app.rects.clear()
} else if e.code == .escape {
exit(0)
}
}
else {}
}
app.redraw = true
}
fn frame(x voidptr) {
mut app := &App(x)
if !app.redraw { return }
if !app.redraw {
return
}
app.tui.clear()
@@ -76,15 +84,12 @@ fn frame(x voidptr) {
app.redraw = false
}
mut app := &App{}
app.tui = tui.init(
user_data: app,
event_fn: event,
user_data: app
event_fn: event
frame_fn: frame
hide_cursor: true
frame_rate: 60
)
app.tui.run()
app.tui.run() ?

View File

@@ -3,73 +3,73 @@
// that can be found in the LICENSE file.
module main
import term.ui as tui
import term.ui
// The color palette, taken from Google's Material design
const (
colors = [
[
tui.Color{239, 154, 154},
tui.Color{244, 143, 177},
tui.Color{206, 147, 216},
tui.Color{179, 157, 219},
tui.Color{159, 168, 218},
tui.Color{144, 202, 249},
tui.Color{129, 212, 250},
tui.Color{128, 222, 234},
tui.Color{128, 203, 196},
tui.Color{165, 214, 167},
tui.Color{197, 225, 165},
tui.Color{230, 238, 156},
tui.Color{255, 245, 157},
tui.Color{255, 224, 130},
tui.Color{255, 204, 128},
tui.Color{255, 171, 145},
tui.Color{188, 170, 164},
tui.Color{238, 238, 238},
tui.Color{176, 190, 197},
ui.Color{239, 154, 154},
ui.Color{244, 143, 177},
ui.Color{206, 147, 216},
ui.Color{179, 157, 219},
ui.Color{159, 168, 218},
ui.Color{144, 202, 249},
ui.Color{129, 212, 250},
ui.Color{128, 222, 234},
ui.Color{128, 203, 196},
ui.Color{165, 214, 167},
ui.Color{197, 225, 165},
ui.Color{230, 238, 156},
ui.Color{255, 245, 157},
ui.Color{255, 224, 130},
ui.Color{255, 204, 128},
ui.Color{255, 171, 145},
ui.Color{188, 170, 164},
ui.Color{238, 238, 238},
ui.Color{176, 190, 197},
],
[
tui.Color{244, 67, 54},
tui.Color{233, 30, 99},
tui.Color{156, 39, 176},
tui.Color{103, 58, 183},
tui.Color{63, 81, 181},
tui.Color{33, 150, 243},
tui.Color{3, 169, 244},
tui.Color{0, 188, 212},
tui.Color{0, 150, 136},
tui.Color{76, 175, 80},
tui.Color{139, 195, 74},
tui.Color{205, 220, 57},
tui.Color{255, 235, 59},
tui.Color{255, 193, 7},
tui.Color{255, 152, 0},
tui.Color{255, 87, 34},
tui.Color{121, 85, 72},
tui.Color{120, 120, 120},
tui.Color{96, 125, 139},
ui.Color{244, 67, 54},
ui.Color{233, 30, 99},
ui.Color{156, 39, 176},
ui.Color{103, 58, 183},
ui.Color{63, 81, 181},
ui.Color{33, 150, 243},
ui.Color{3, 169, 244},
ui.Color{0, 188, 212},
ui.Color{0, 150, 136},
ui.Color{76, 175, 80},
ui.Color{139, 195, 74},
ui.Color{205, 220, 57},
ui.Color{255, 235, 59},
ui.Color{255, 193, 7},
ui.Color{255, 152, 0},
ui.Color{255, 87, 34},
ui.Color{121, 85, 72},
ui.Color{120, 120, 120},
ui.Color{96, 125, 139},
],
[
tui.Color{198, 40, 40},
tui.Color{173, 20, 87},
tui.Color{106, 27, 154},
tui.Color{69, 39, 160},
tui.Color{40, 53, 147},
tui.Color{21, 101, 192},
tui.Color{2, 119, 189},
tui.Color{0, 131, 143},
tui.Color{0, 105, 92},
tui.Color{46, 125, 50},
tui.Color{85, 139, 47},
tui.Color{158, 157, 36},
tui.Color{249, 168, 37},
tui.Color{255, 143, 0},
tui.Color{239, 108, 0},
tui.Color{216, 67, 21},
tui.Color{78, 52, 46},
tui.Color{33, 33, 33},
tui.Color{55, 71, 79},
ui.Color{198, 40, 40},
ui.Color{173, 20, 87},
ui.Color{106, 27, 154},
ui.Color{69, 39, 160},
ui.Color{40, 53, 147},
ui.Color{21, 101, 192},
ui.Color{2, 119, 189},
ui.Color{0, 131, 143},
ui.Color{0, 105, 92},
ui.Color{46, 125, 50},
ui.Color{85, 139, 47},
ui.Color{158, 157, 36},
ui.Color{249, 168, 37},
ui.Color{255, 143, 0},
ui.Color{239, 108, 0},
ui.Color{216, 67, 21},
ui.Color{78, 52, 46},
ui.Color{33, 33, 33},
ui.Color{55, 71, 79},
],
]
)
@@ -90,18 +90,18 @@ const (
struct App {
mut:
tui &tui.Context = 0
ui &ui.Context = 0
header_text []string
mouse_pos Point
msg string
msg_hide_tick int
primary_color tui.Color = colors[1][6]
secondary_color tui.Color = colors[1][9]
primary_color_idx int = 25
secondary_color_idx int = 28
bg_color tui.Color = tui.Color{0, 0, 0}
drawing [][]tui.Color = [][]tui.Color{len: h, init: []tui.Color{len: w}}
size int = 1
primary_color ui.Color = colors[1][6]
secondary_color ui.Color = colors[1][9]
primary_color_idx int = 25
secondary_color_idx int = 28
bg_color ui.Color = ui.Color{0, 0, 0}
drawing [][]ui.Color = [][]ui.Color{len: h, init: []ui.Color{len: w}}
size int = 1
should_redraw bool = true
is_dragging bool
}
@@ -114,24 +114,24 @@ mut:
fn main() {
mut app := &App{}
app.tui = tui.init({
app.ui = ui.init(
user_data: app
frame_fn: frame
event_fn: event
frame_rate: frame_rate
hide_cursor: true
window_title: 'V terminal pixelart drawing app'
})
)
app.mouse_pos.x = 40
app.mouse_pos.y = 15
app.tui.clear()
app.tui.run()
app.ui.clear()
app.ui.run() ?
}
fn frame(x voidptr) {
mut app := &App(x)
mut redraw := app.should_redraw
if app.msg != '' && app.tui.frame_count >= app.msg_hide_tick {
if app.msg != '' && app.ui.frame_count >= app.msg_hide_tick {
app.msg = ''
redraw = true
}
@@ -141,12 +141,12 @@ fn frame(x voidptr) {
}
}
fn event(event &tui.Event, x voidptr) {
fn event(event &ui.Event, x voidptr) {
mut app := &App(x)
match event.typ {
.mouse_down {
app.is_dragging = true
if app.tui.window_height - event.y < 5 {
if app.ui.window_height - event.y < 5 {
app.footer_click(event)
} else {
app.paint(event)
@@ -174,8 +174,8 @@ fn event(event &tui.Event, x voidptr) {
y: event.y
}
d := event.direction == .down
if event.modifiers & tui.ctrl != 0 {
p := event.modifiers & tui.shift == 0
if event.modifiers & ui.ctrl != 0 {
p := event.modifiers & ui.shift == 0
c := if d {
if p { app.primary_color_idx - 1 } else { app.secondary_color_idx - 1 }
} else {
@@ -183,53 +183,72 @@ fn event(event &tui.Event, x voidptr) {
}
app.select_color(p, c)
} else {
if d { app.inc_size() } else { app.dec_size() }
if d {
app.inc_size()
} else {
app.dec_size()
}
}
}
.key_down {
match event.code {
.f1, ._1 {
oevent := *event
nevent := tui.Event{ ...oevent, button: tui.MouseButton.left, x: app.mouse_pos.x , y: app.mouse_pos.y }
nevent := ui.Event{
...oevent
button: ui.MouseButton.left
x: app.mouse_pos.x
y: app.mouse_pos.y
}
app.paint(nevent)
}
.f2, ._2 {
oevent := *event
nevent := tui.Event{ ...oevent, button: tui.MouseButton.right, x: app.mouse_pos.x , y: app.mouse_pos.y }
nevent := ui.Event{
...oevent
button: ui.MouseButton.right
x: app.mouse_pos.x
y: app.mouse_pos.y
}
app.paint(nevent)
}
.space {
oevent := *event
nevent := tui.Event{ ...oevent, button: tui.MouseButton.middle, x: app.mouse_pos.x , y: app.mouse_pos.y }
nevent := ui.Event{
...oevent
button: ui.MouseButton.middle
x: app.mouse_pos.x
y: app.mouse_pos.y
}
app.paint(nevent)
}
.j, .down {
if event.modifiers & tui.shift != 0 {
if event.modifiers & ui.shift != 0 {
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
}
app.mouse_pos.y++
}
.k, .up {
if event.modifiers & tui.shift != 0 {
if event.modifiers & ui.shift != 0 {
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
}
app.mouse_pos.y--
}
.h, .left {
if event.modifiers & tui.shift != 0 {
if event.modifiers & ui.shift != 0 {
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
}
app.mouse_pos.x -= 2
}
.l, .right {
if event.modifiers & tui.shift != 0 {
if event.modifiers & ui.shift != 0 {
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
}
app.mouse_pos.x += 2
}
.t {
p := event.modifiers & tui.alt == 0
c := if event.modifiers & tui.shift != 0 {
p := event.modifiers & ui.alt == 0
c := if event.modifiers & ui.shift != 0 {
if p { app.primary_color_idx - 19 } else { app.secondary_color_idx - 19 }
} else {
if p { app.primary_color_idx + 19 } else { app.secondary_color_idx + 19 }
@@ -237,8 +256,8 @@ fn event(event &tui.Event, x voidptr) {
app.select_color(p, c)
}
.r {
p := event.modifiers & tui.alt == 0
c := if event.modifiers & tui.shift != 0 {
p := event.modifiers & ui.alt == 0
c := if event.modifiers & ui.shift != 0 {
if p { app.primary_color_idx - 1 } else { app.secondary_color_idx - 1 }
} else {
if p { app.primary_color_idx + 1 } else { app.secondary_color_idx + 1 }
@@ -252,7 +271,7 @@ fn event(event &tui.Event, x voidptr) {
app.dec_size()
}
.c {
app.drawing = [][]tui.Color{len: h, init: []tui.Color{len: w}}
app.drawing = [][]ui.Color{len: h, init: []ui.Color{len: w}}
}
.q, .escape {
app.render(true)
@@ -267,14 +286,14 @@ fn event(event &tui.Event, x voidptr) {
}
fn (mut app App) render(paint_only bool) {
app.tui.clear()
app.ui.clear()
app.draw_header()
app.draw_content()
if !paint_only {
app.draw_footer()
app.draw_cursor()
}
app.tui.flush()
app.ui.flush()
}
fn (mut app App) select_color(primary bool, idx int) {
@@ -293,10 +312,10 @@ fn (mut app App) select_color(primary bool, idx int) {
app.show_msg('set $c_str color idx: $idx', 1)
}
fn (mut app App) set_pixel(x_ int, y_ int, c tui.Color) {
fn (mut app App) set_pixel(x_ int, y_ int, c ui.Color) {
// Term coords start at 1, and adjust for the header
x, y := x_ - 1, y_ - 4
if y < 0 || app.tui.window_height - y < 3 {
if y < 0 || app.ui.window_height - y < 3 {
return
}
if y >= app.drawing.len || x < 0 || x >= app.drawing[0].len {
@@ -305,8 +324,8 @@ fn (mut app App) set_pixel(x_ int, y_ int, c tui.Color) {
app.drawing[y][x] = c
}
fn (mut app App) paint(event &tui.Event) {
if event.y < 4 || app.tui.window_height - event.y < 4 {
fn (mut app App) paint(event &ui.Event) {
if event.y < 4 || app.ui.window_height - event.y < 4 {
return
}
x_start, y_start := int(f32((event.x - 1) / 2) - app.size / 2 + 1), event.y - app.size / 2
@@ -323,38 +342,38 @@ fn (mut app App) paint(event &tui.Event) {
}
fn (mut app App) draw_content() {
w_, mut h_ := app.tui.window_width / 2, app.tui.window_height - 8
w_, mut h_ := app.ui.window_width / 2, app.ui.window_height - 8
if h_ > app.drawing.len {
h_ = app.drawing.len
}
for row_idx, row in app.drawing[..h_] {
app.tui.set_cursor_position(0, row_idx + 4)
mut last := tui.Color{0, 0, 0}
app.ui.set_cursor_position(0, row_idx + 4)
mut last := ui.Color{0, 0, 0}
for cell in row[..w_] {
if cell.r == 0 && cell.g == 0 && cell.b == 0 {
if !(cell.r == last.r && cell.g == last.g && cell.b == last.b) {
app.tui.reset()
app.ui.reset()
}
} else {
if !(cell.r == last.r && cell.g == last.g && cell.b == last.b) {
app.tui.set_bg_color(cell)
app.ui.set_bg_color(cell)
}
}
app.tui.write(spaces)
app.ui.write(spaces)
last = cell
}
app.tui.reset()
app.ui.reset()
}
}
fn (mut app App) draw_cursor() {
if app.mouse_pos.y in [3, app.tui.window_height - 5] {
if app.mouse_pos.y in [3, app.ui.window_height - 5] {
// inside the horizontal separators
return
}
cursor_color := if app.is_dragging { tui.Color{220, 220, 220} } else { tui.Color{160, 160, 160} }
app.tui.set_bg_color(cursor_color)
if app.mouse_pos.y >= 3 && app.mouse_pos.y <= app.tui.window_height - 4 {
cursor_color := if app.is_dragging { ui.Color{220, 220, 220} } else { ui.Color{160, 160, 160} }
app.ui.set_bg_color(cursor_color)
if app.mouse_pos.y >= 3 && app.mouse_pos.y <= app.ui.window_height - 4 {
// inside the main content
mut x_start := int(f32((app.mouse_pos.x - 1) / 2) - app.size / 2 + 1) * 2 - 1
mut y_start := app.mouse_pos.y - app.size / 2
@@ -366,70 +385,71 @@ fn (mut app App) draw_cursor() {
if y_start < 4 {
y_start = 4
}
if x_end > app.tui.window_width {
x_end = app.tui.window_width
if x_end > app.ui.window_width {
x_end = app.ui.window_width
}
if y_end > app.tui.window_height - 5 {
y_end = app.tui.window_height - 5
if y_end > app.ui.window_height - 5 {
y_end = app.ui.window_height - 5
}
app.tui.draw_rect(x_start, y_start, x_end, y_end)
app.ui.draw_rect(x_start, y_start, x_end, y_end)
} else {
app.tui.draw_text(app.mouse_pos.x, app.mouse_pos.y, space)
app.ui.draw_text(app.mouse_pos.x, app.mouse_pos.y, space)
}
app.tui.reset()
app.ui.reset()
}
fn (mut app App) draw_header() {
if app.msg != '' {
app.tui.set_color({
app.ui.set_color(
r: 0
g: 0
b: 0
})
app.tui.set_bg_color({
)
app.ui.set_bg_color(
r: 220
g: 220
b: 220
})
app.tui.draw_text(0, 0, ' $app.msg ')
app.tui.reset()
)
app.ui.draw_text(0, 0, ' $app.msg ')
app.ui.reset()
}
app.tui.draw_text(3, 2, /* 'tick: $app.tui.frame_count | ' + */ 'terminal size: ($app.tui.window_width, $app.tui.window_height) | primary color: $app.primary_color.hex() | secondary color: $app.secondary_color.hex()')
app.tui.horizontal_separator(3)
//'tick: $app.ui.frame_count | ' +
app.ui.draw_text(3, 2, 'terminal size: ($app.ui.window_width, $app.ui.window_height) | primary color: $app.primary_color.hex() | secondary color: $app.secondary_color.hex()')
app.ui.horizontal_separator(3)
}
fn (mut app App) draw_footer() {
_, wh := app.tui.window_width, app.tui.window_height
app.tui.horizontal_separator(wh - 4)
_, wh := app.ui.window_width, app.ui.window_height
app.ui.horizontal_separator(wh - 4)
for i, color_row in colors {
for j, color in color_row {
x := j * 3 + 19
y := wh - 3 + i
app.tui.set_bg_color(color)
app.ui.set_bg_color(color)
if app.primary_color_idx == j + (i * 19) {
app.tui.set_color(r: 0, g: 0, b: 0)
app.tui.draw_text(x, y, '><')
app.tui.reset_color()
app.ui.set_color(r: 0, g: 0, b: 0)
app.ui.draw_text(x, y, '><')
app.ui.reset_color()
} else if app.secondary_color_idx == j + (i * 19) {
app.tui.set_color(r: 255, g: 255, b: 255)
app.tui.draw_text(x, y, '><')
app.tui.reset_color()
app.ui.set_color(r: 255, g: 255, b: 255)
app.ui.draw_text(x, y, '><')
app.ui.reset_color()
} else {
app.tui.draw_rect(x, y, x + 1, y)
app.ui.draw_rect(x, y, x + 1, y)
}
}
}
app.tui.reset_bg_color()
app.tui.draw_text(3, wh - 3, select_color)
app.tui.bold()
app.tui.draw_text(3, wh - 1, '$select_size $app.size')
app.tui.reset()
app.ui.reset_bg_color()
app.ui.draw_text(3, wh - 3, select_color)
app.ui.bold()
app.ui.draw_text(3, wh - 1, '$select_size $app.size')
app.ui.reset()
// TODO: help button
// if ww >= 90 {
// app.tui.draw_text(80, wh - 3, help_1)
// app.tui.draw_text(80, wh - 2, help_2)
// app.tui.draw_text(80, wh - 1, help_3)
// app.ui.draw_text(80, wh - 3, help_1)
// app.ui.draw_text(80, wh - 2, help_2)
// app.ui.draw_text(80, wh - 1, help_3)
// }
}
@@ -449,8 +469,8 @@ fn (mut app App) dec_size() {
app.show_msg('dec. size: $app.size', 1)
}
fn (mut app App) footer_click(event &tui.Event) {
footer_y := 3 - (app.tui.window_height - event.y)
fn (mut app App) footer_click(event &ui.Event) {
footer_y := 3 - (app.ui.window_height - event.y)
match event.x {
8...11 {
app.inc_size()
@@ -464,7 +484,9 @@ fn (mut app App) footer_click(event &tui.Event) {
return
}
idx := footer_y * 19 - 6 + event.x / 3
if idx < 0 || idx > 56 { return }
if idx < 0 || idx > 56 {
return
}
app.select_color(event.button == .left, idx)
}
else {}
@@ -473,6 +495,6 @@ fn (mut app App) footer_click(event &tui.Event) {
fn (mut app App) show_msg(text string, time int) {
frames := time * frame_rate
app.msg_hide_tick = if time > 0 { int(app.tui.frame_count) + frames } else { -1 }
app.msg_hide_tick = if time > 0 { int(app.ui.frame_count) + frames } else { -1 }
app.msg = text
}

View File

@@ -26,7 +26,7 @@ pub:
struct App {
mut:
tui &tui.Context = 0
ed &Buffer = 0
ed &Buffer = 0
current_file int
files []string
status string
@@ -44,7 +44,7 @@ fn (mut a App) set_status(msg string, duration_ms int) {
fn (mut a App) save() {
if a.cfile().len > 0 {
b := a.ed
os.write_file(a.cfile(), b.raw())
os.write_file(a.cfile(), b.raw()) or { panic(err) }
a.set_status('Saved', 2000)
} else {
a.set_status('No file loaded', 4000)
@@ -79,7 +79,6 @@ fn (mut a App) visit_next_file() {
a.init_file()
}
fn (mut a App) footer() {
w, h := a.tui.window_width, a.tui.window_height
mut b := a.ed
@@ -99,16 +98,16 @@ fn (mut a App) footer() {
if a.t <= 0 {
status = ''
} else {
a.tui.set_bg_color({
a.tui.set_bg_color(
r: 200
g: 200
b: 200
})
a.tui.set_color({
)
a.tui.set_color(
r: 0
g: 0
b: 0
})
)
a.tui.draw_text((w + 4 - status.len) / 2, h - 1, ' $status ')
a.tui.reset()
a.t -= 33
@@ -118,8 +117,8 @@ fn (mut a App) footer() {
struct Buffer {
tab_width int = 4
pub mut:
lines []string
cursor Cursor
lines []string
cursor Cursor
}
fn (b Buffer) flat() string {
@@ -303,7 +302,7 @@ fn (mut b Buffer) free() {
for line in b.lines {
line.free()
}
unsafe {b.lines.free()}
unsafe { b.lines.free() }
}
fn (mut b Buffer) move_updown(amount int) {
@@ -334,7 +333,7 @@ fn (mut b Buffer) move_cursor(amount int, movement Movement) {
b.move_updown(-dlines)
}
.page_down {
dlines := imin(b.lines.len-1, b.cursor.pos_y + amount) - b.cursor.pos_y
dlines := imin(b.lines.len - 1, b.cursor.pos_y + amount) - b.cursor.pos_y
b.move_updown(dlines)
}
.left {
@@ -374,13 +373,19 @@ fn (mut b Buffer) move_to_word(movement Movement) {
x = 0
}
// first, move past all non-`a-zA-Z0-9_` characters
for x+a >= 0 && x+a < line.len && !(line[x+a].is_letter() || line[x+a].is_digit() || line[x+a] == `_`) { x += a }
for x + a >= 0 && x + a < line.len && !(line[x + a].is_letter()
|| line[x + a].is_digit()|| line[x + a] == `_`) {
x += a
}
// then, move past all the letters and numbers
for x+a >= 0 && x+a < line.len && (line[x+a].is_letter() || line[x+a].is_digit() || line[x+a] == `_`) { x += a }
for x + a >= 0 && x + a < line.len && (line[x + a].is_letter()
|| line[x + a].is_digit()|| line[x + a] == `_`) {
x += a
}
// if the cursor is out of bounds, move it to the next/previous line
if x + a >= 0 && x + a <= line.len {
x += a
} else if a < 0 && y+1 > b.lines.len && y-1 >= 0 {
} else if a < 0 && y + 1 > b.lines.len && y - 1 >= 0 {
y += a
x = 0
}
@@ -388,11 +393,19 @@ fn (mut b Buffer) move_to_word(movement Movement) {
}
fn imax(x int, y int) int {
return if x < y { y } else { x }
return if x < y {
y
} else {
x
}
}
fn imin(x int, y int) int {
return if x < y { x } else { y }
return if x < y {
x
} else {
y
}
}
struct Cursor {
@@ -443,9 +456,7 @@ fn (mut a App) init_file() {
// 'vico: ' +
a.tui.set_window_title(a.files[a.current_file])
mut b := a.ed
content := os.read_file(a.files[a.current_file]) or {
panic(err)
}
content := os.read_file(a.files[a.current_file]) or { panic(err) }
b.put(content)
a.ed.cursor.pos_x = init_x
a.ed.cursor.pos_y = init_y
@@ -573,12 +584,12 @@ fn main() {
mut a := &App{
files: files
}
a.tui = tui.init({
a.tui = tui.init(
user_data: a
init_fn: init
frame_fn: frame
event_fn: event
capture_events: true
})
a.tui.run()
)
a.tui.run() ?
}

View File

@@ -60,12 +60,12 @@ fn (mut v Vec) randomize(min_x int, min_y int, max_x int, max_y int) {
// part of snake's body representation
struct BodyPart {
mut:
pos Vec = {
x: block_size
y: block_size
}
pos Vec = {
x: block_size
y: block_size
}
color termui.Color = green
facing Orientation = .top
facing Orientation = .top
}
// snake representation
@@ -75,9 +75,9 @@ mut:
direction Orientation
body []BodyPart
velocity Vec = Vec{
x: 0
y: 0
}
x: 0
y: 0
}
}
// length returns the snake's current length
@@ -125,8 +125,16 @@ fn (mut s Snake) move() {
piece.facing = s.direction
new_x := piece.pos.x + s.velocity.x
new_y := piece.pos.y + s.velocity.y
piece.pos.x += if new_x > block_size && new_x < width - block_size { s.velocity.x } else { 0 }
piece.pos.y += if new_y > block_size && new_y < height - block_size { s.velocity.y } else { 0 }
piece.pos.x += if new_x > block_size && new_x < width - block_size {
s.velocity.x
} else {
0
}
piece.pos.y += if new_y > block_size && new_y < height - block_size {
s.velocity.y
} else {
0
}
}
s.body[i] = piece
}
@@ -205,10 +213,10 @@ fn (s Snake) check_overlap() bool {
fn (s Snake) check_out_of_bounds() bool {
h := s.get_head()
return h.pos.x + s.velocity.x <= block_size ||
h.pos.x + s.velocity.x > s.app.width - s.velocity.x || h.pos.y + s.velocity.y <= block_size ||
h.pos.y + s.velocity.y >
s.app.height - block_size - s.velocity.y
return h.pos.x + s.velocity.x <= block_size
|| h.pos.x + s.velocity.x > s.app.width - s.velocity.x
|| h.pos.y + s.velocity.y <= block_size
|| h.pos.y + s.velocity.y > s.app.height - block_size - s.velocity.y
}
// draw draws the parts of the snake
@@ -233,10 +241,10 @@ fn (s Snake) draw() {
// rat representation
struct Rat {
mut:
pos Vec = {
x: block_size
y: block_size
}
pos Vec = {
x: block_size
y: block_size
}
captured bool
color termui.Color = grey
app &App
@@ -244,8 +252,8 @@ mut:
// randomize spawn the rat in a new spot within the playable field
fn (mut r Rat) randomize() {
r.pos.randomize(2 * block_size + buffer, 2 * block_size + buffer, r.app.width - block_size -
buffer, r.app.height - block_size - buffer)
r.pos.randomize(2 * block_size + buffer, 2 * block_size + buffer, r.app.width - block_size - buffer,
r.app.height - block_size - buffer)
}
struct App {
@@ -255,7 +263,7 @@ mut:
rat Rat
width int
height int
redraw bool = true
redraw bool = true
state GameState = .game
}
@@ -387,9 +395,8 @@ fn (mut a App) move_snake(direction Orientation) {
fn (a App) check_capture() bool {
snake_pos := a.snake.get_head().pos
rat_pos := a.rat.pos
return snake_pos.x <= rat_pos.x + block_size &&
snake_pos.x + block_size >= rat_pos.x && snake_pos.y <= rat_pos.y + block_size && snake_pos.y +
block_size >= rat_pos.y
return snake_pos.x <= rat_pos.x + block_size && snake_pos.x + block_size >= rat_pos.x
&& snake_pos.y <= rat_pos.y + block_size&& snake_pos.y + block_size >= rat_pos.y
}
fn (mut a App) draw_snake() {
@@ -454,12 +461,12 @@ fn (mut a App) draw_gameover() {
}
mut app := &App{}
app.termui = termui.init({
app.termui = termui.init(
user_data: app
event_fn: event
frame_fn: frame
init_fn: init
hide_cursor: true
frame_rate: 10
})
app.termui.run()
)
app.termui.run() ?