Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: return errors as values where appropriate #7

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/colors.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
// Colorize all components uniformly
b.colorize(Color.magenta)
for _ in 0 .. b.iters {
b.progress()
b.progress()!
time.sleep(timeout)
}

Expand All @@ -43,7 +43,7 @@ fn main() {
indicator: Color.magenta
})
for _ in 0 .. b2.iters {
b2.progress()
b2.progress()!
time.sleep(timeout * 2)
}

Expand All @@ -65,7 +65,7 @@ fn main() {
indicator: Color.magenta
})
for _ in 0 .. b3.iters {
b3.progress()
b3.progress()!
time.sleep(timeout * 2)
}

Expand All @@ -84,7 +84,7 @@ fn main() {
for i in 0 .. b4.iters {
j := term.bright_black('(${i + 1}/${b4.width})')
b4.post = '${term.cyan('│')} ${j} ${term.blue(b4.spinner())}'
b4.progress()
b4.progress()!
time.sleep(timeout * 3)
}
}
2 changes: 1 addition & 1 deletion examples/multi.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import rand
fn pseudo_dissimilar_progress(mut wg sync.WaitGroup, mut b bartender.Bar) ! {
rand_num := rand.intn(100) or { panic(err) }
for _ in 0 .. b.iters {
b.progress()
b.progress()!
time.sleep((time.millisecond * rand_num) + (50 * time.millisecond))
}
wg.done()
Expand Down
6 changes: 3 additions & 3 deletions examples/simple.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
// ===========================================================================
mut b := bartender.Bar{}
for _ in 0 .. b.iters {
b.progress()
b.progress()!
time.sleep(timeout)
}

Expand All @@ -25,7 +25,7 @@ fn main() {
}
}
for _ in 0 .. b2.iters {
b2.progress()
b2.progress()!
time.sleep(timeout)
}

Expand All @@ -45,7 +45,7 @@ fn main() {
}
}
for _ in 0 .. b3.iters {
b3.progress()
b3.progress()!
time.sleep(timeout * 5)
}
}
14 changes: 7 additions & 7 deletions examples/smooth.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
// Add optional fields
b.post = ' Push Fill'
for _ in 0 .. b.iters {
b.progress()
b.progress()!
time.sleep(timeout)
}

Expand All @@ -22,7 +22,7 @@ fn main() {
theme: Theme.pull
}
for _ in 0 .. b2.iters {
b2.progress()
b2.progress()!
time.sleep(timeout)
}

Expand All @@ -33,7 +33,7 @@ fn main() {
theme: ThemeVariant{.push, .drain}
}
for _ in 0 .. b.iters {
b.progress()
b.progress()!
time.sleep(timeout)
}

Expand All @@ -43,7 +43,7 @@ fn main() {
theme: ThemeVariant{.pull, .drain}
}
for _ in 0 .. b2.iters {
b2.progress()
b2.progress()!
time.sleep(timeout)
}
time.sleep(timeout)
Expand All @@ -59,7 +59,7 @@ fn main() {
}
b3.colorize(Color.cyan)
for _ in 0 .. b3.iters {
b3.progress()
b3.progress()!
time.sleep(timeout * 2)
}

Expand All @@ -70,7 +70,7 @@ fn main() {
}
b4.colorize(Color.bright_black)
for _ in 0 .. b4.iters {
b4.progress()
b4.progress()!
time.sleep(timeout * 2)
}

Expand All @@ -86,7 +86,7 @@ fn main() {
b5.width -= 2
b5.colorize(Color.green)
for _ in 0 .. b5.iters {
b5.progress()
b5.progress()!
time.sleep(timeout * 10)
}
}
2 changes: 1 addition & 1 deletion examples/smooth_multi.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import rand
fn pseudo_dissimilar_progress_(mut wg sync.WaitGroup, mut b SmoothBar) ! {
rand_num := rand.intn(20) or { panic(err) }
for _ in 0 .. b.iters {
b.progress()
b.progress()!
// HACK: modifer to adjust timeout for the scope of this example
modifier := if b.pre.str()[..1].int() > 4 { 2 } else { 1 }
time.sleep((time.millisecond * rand_num) + (15 * modifier * time.millisecond))
Expand Down
24 changes: 6 additions & 18 deletions src/_instructions_multi_bars.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import term
import time
import sync

fn watch_(bars MultiBarType, mut wg sync.WaitGroup) {
fn watch_(bars MultiBarType, mut wg sync.WaitGroup) ! {
// NOTE: Same operation for Bars and SmoothBars (re-check with V's progression if this can be grouped).
// Tested match statements and alias types for arrays with bar references.
if bars is []&Bar {
ensure_mutli(bars) or {
eprintln(err)
exit(0)
}
ensure_multi(bars)!
for {
if bars.draw() {
term.show_cursor()
Expand All @@ -21,10 +18,7 @@ fn watch_(bars MultiBarType, mut wg sync.WaitGroup) {
time.sleep(time.millisecond * 15)
}
} else if bars is []&SmoothBar {
ensure_mutli(bars) or {
eprintln(err)
exit(0)
}
ensure_multi(bars)!
for {
if bars.draw() {
term.show_cursor()
Expand All @@ -37,7 +31,7 @@ fn watch_(bars MultiBarType, mut wg sync.WaitGroup) {
wg.done()
}

fn ensure_mutli(bars MultiBarType) ! {
fn ensure_multi(bars MultiBarType) ! {
// Same operation for both types.
if bars is []&Bar {
mut not_multi := []int{}
Expand All @@ -47,10 +41,7 @@ fn ensure_mutli(bars MultiBarType) ! {
}
}
if not_multi.len > 0 {
return IError(BarError{
kind: .missing_multi
msg: '${not_multi}'
})
return bar_error(.missing_multi, not_multi.str())
}
} else if bars is []&SmoothBar {
mut not_multi := []int{}
Expand All @@ -60,10 +51,7 @@ fn ensure_mutli(bars MultiBarType) ! {
}
}
if not_multi.len > 0 {
return IError(BarError{
kind: .missing_multi
msg: '${not_multi}'
})
return bar_error(.missing_multi, not_multi.str())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/_instructions_reader.v
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ fn (mut br BarReader) read(mut buf []u8) !int {
// SmoothBar won't be visible or will have corrupted chars.
Bar {
if (f64(br.pos) / br.size * br.bar.width) > br.bar.state.pos {
br.bar.progress()
br.bar.progress()!
}
}
SmoothBar {
if (f64(br.pos) / br.size * br.bar.width) > br.bar.state.pos {
br.bar.progress()
br.bar.progress()!
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/_instructions_simple_bar.v
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ fn (mut b Bar) colorize_components(color BarColor) {
}
}

fn (mut b Bar) progress_() {
fn (mut b Bar) progress_() ! {
if b.state.time.start == 0 {
if b.runes_.progress == '' {
b.setup()
}
b.state.time = struct {time.ticks(), 0}
term.hide_cursor()
os.signal_opt(.int, handle_interrupt) or { panic(err) }
os.signal_opt(.int, handle_interrupt)!
// Print empty line before first progress to not overwrite current line.
if !b.multi {
println('')
}
}
if b.state.pos >= b.width_ {
panic(IError(BarError{ kind: .finished }))
return bar_error(.already_finished, none)
}

b.set_vals()
Expand All @@ -98,9 +98,9 @@ fn (mut b Bar) colorize_(color BarColorType) {
}
}

fn (b Bar) eta_(delay u8) string {
fn (b Bar) eta_(delay u8) !string {
if delay > 100 {
panic(IError(BarError{ kind: .delay_exceeded }))
return bar_error(.delay_exceeded, none)
}
next_pos := b.state.pos + 1
if next_pos < f32(b.width_) * delay / 100 {
Expand Down
10 changes: 5 additions & 5 deletions src/_instructions_smooth_bar.v
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,21 @@ fn (b SmoothBar) next_pos() u16 {
})
}

fn (mut b SmoothBar) progress_() {
fn (mut b SmoothBar) progress_() ! {
if b.state.time.start == 0 {
if b.runes.s.len == 0 {
b.setup()
}
b.state.time = struct {time.ticks(), 0}
term.hide_cursor()
os.signal_opt(.int, handle_interrupt) or { panic(err) }
os.signal_opt(.int, handle_interrupt)!
// Print empty line before first progress to not overwrite current line.
if !b.multi {
println('')
}
}
if b.state.pos > b.width_ {
panic(IError(BarError{ kind: .finished }))
return bar_error(.already_finished, none)
}

b.set_vals()
Expand Down Expand Up @@ -238,9 +238,9 @@ fn (mut b SmoothBar) colorize_(color Color) {
b.runes = painted_runes
}

fn (b SmoothBar) eta_(delay u8) string {
fn (b SmoothBar) eta_(delay u8) !string {
if delay > 100 {
panic(IError(BarError{ kind: .delay_exceeded }))
return bar_error(.delay_exceeded, none)
}
next_pos := b.next_pos()
if b.width_ == b.state.pos {
Expand Down
12 changes: 6 additions & 6 deletions src/_tests_simple_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ fn test_setup() {

fn test_progress() {
mut b := bartender.test_bar
b.progress()
b.progress()!
assert b.format() == '[> ] Loading...'
b.progress()
b.progress()!
assert b.format() == '[#> ] Loading...'
for i := 0; i < 8; i++ {
b.progress()
b.progress()!
}
assert b.format() == '[#########> ] Loading...'
for i := 0; i < 10; i++ {
b.progress()
b.progress()!
}
assert b.state.pos == 20
assert b.format() == '[####################] Done!'
Expand All @@ -57,7 +57,7 @@ fn test_eta() {
mut b := bartender.test_bar
b.setup()
for i := 0; i < 10; i++ {
b.progress()
b.progress()!
// would take 2000ms to complete full bar
time.sleep(100 * time.millisecond)
}
Expand All @@ -68,7 +68,7 @@ fn test_eta() {
b.width = 40
b.reset()
for i := 0; i < 20; i++ {
b.progress()
b.progress()!
// would take 2000ms to complete full bar
time.sleep(50 * time.millisecond)
}
Expand Down
12 changes: 6 additions & 6 deletions src/_tests_smooth_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ fn test_setup() {
fn test_progress() {
mut b := bartender.test_bar
// 1/9
b.progress()
b.progress()!
assert b.format() == '▏ Loading...'
// 2/9
b.progress()
b.progress()!
assert b.format() == '▎ Loading...'
// 1
for i := 0; i < 6; i++ {
b.progress()
b.progress()!
}
assert b.format() == '█ Loading...'
// 1 5/9
for i := 0; i < 5; i++ {
b.progress()
b.progress()!
}
assert b.format() == '█▌ Loading...'
// 20 5/9
for i := 0; i < bartender.col_iters * 19; i++ {
b.progress()
b.progress()!
}
assert b.format() == '████████████████████▌ Loading...'

for i := 0; i < bartender.col_iters * 19 + 4; i++ {
b.progress()
b.progress()!
}
assert b.format() == '████████████████████████████████████████ Finished!'
}
Loading