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
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
90 changed files with 1994 additions and 1832 deletions

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() ?
}