Skip to content

Commit

Permalink
refactor!: handle multi state internally (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Jun 13, 2023
1 parent d1b1949 commit c4e4b7e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 46 deletions.
10 changes: 4 additions & 6 deletions examples/simple_multi.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ fn pseudo_dissimilar_progress(mut wg sync.WaitGroup, mut b bartender.Bar) ! {
}

fn main() {
mut b1 := bartender.Bar{
multi: true
}
mut b1 := bartender.Bar{}
b1.pre = '1: ['
mut b2 := b1
b2.pre = '2: ['
mut b3 := b1
b3.pre = '3: ['
mut bars := [&b1, &b2, &b3]
mut bars := &[&b1, &b2, &b3]

mut wg := sync.new_waitgroup()
wg.add(1)
spawn bars.watch(mut wg)
for mut b in bars {
wg.add(1)
spawn pseudo_dissimilar_progress(mut wg, mut b)
}
wg.add(1)
spawn bars.watch(mut wg)
wg.wait()
}
7 changes: 3 additions & 4 deletions examples/smooth_multi.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fn pseudo_dissimilar_progress_(mut wg sync.WaitGroup, mut b SmoothBar) ! {

fn main() {
mut b1 := SmoothBar{
multi: true
post: ''
}
b1.pre = '1: '
Expand All @@ -40,14 +39,14 @@ fn main() {
mut b7 := b1
b7.pre = '7: '
b7.theme = Theme.merge
mut bars := [&b1, &b2, &b3, &b4, &b5, &b6, &b7]
mut bars := &[&b1, &b2, &b3, &b4, &b5, &b6, &b7]

mut wg := sync.new_waitgroup()
wg.add(1)
spawn bars.watch(mut wg)
for mut b in bars {
wg.add(1)
spawn pseudo_dissimilar_progress_(mut wg, mut b)
}
wg.add(1)
spawn bars.watch(mut wg)
wg.wait()
}
44 changes: 13 additions & 31 deletions src/_instructions_multi_bars.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import term
import time
import sync

fn watch_(bars MultiBarType, mut wg sync.WaitGroup) ! {
fn watch_(mut 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_multi(bars)!
if mut bars is []&Bar {
for mut b in bars {
b.multi = true
b.setup()
}
time.sleep(time.millisecond * 15)
for {
if bars.draw() {
term.show_cursor()
Expand All @@ -17,8 +21,11 @@ fn watch_(bars MultiBarType, mut wg sync.WaitGroup) ! {
// Slow down redraw loop interval to reduce load.
time.sleep(time.millisecond * 15)
}
} else if bars is []&SmoothBar {
ensure_multi(bars)!
} else if mut bars is []&SmoothBar {
for mut b in bars {
b.multi = true
b.setup()
}
for {
if bars.draw() {
term.show_cursor()
Expand All @@ -31,37 +38,12 @@ fn watch_(bars MultiBarType, mut wg sync.WaitGroup) ! {
wg.done()
}

fn ensure_multi(bars MultiBarType) ! {
// Same operation for both types.
if bars is []&Bar {
mut not_multi := []int{}
for i, bar in bars {
if !bar.multi {
not_multi << i
}
}
if not_multi.len > 0 {
return bar_error(.missing_multi, not_multi.str())
}
} else if bars is []&SmoothBar {
mut not_multi := []int{}
for i, bar in bars {
if !bar.multi {
not_multi << i
}
}
if not_multi.len > 0 {
return bar_error(.missing_multi, not_multi.str())
}
}
}

fn (bars []&Bar) draw() bool {
mut finished := true
mut formatted := []string{}
for b in bars {
formatted << b.format()
if b.state.pos > 0 && b.state.pos < b.width_ {
if b.state.pos < b.width_ {
finished = false
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/_state_common.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ pub mut:
// Number of iterations. NOTE: Solution is up for improvement.
// Resolves to `width_` for `Bar` and `smooth_runes.len * width_` for `SmoothBar`.
iters int = 60
multi bool
mut:
state State
multi bool
// Private params. Based on public equivalents.
// Assigned on `<bar>.setup()` or on `<bar>.progress()`.
// Might get mutated by state or terminal size changes.
Expand Down
8 changes: 4 additions & 4 deletions src/lib.v
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ pub fn (b Bar) reader(reader io.Reader, size u64) &io.BufferedReader {
}

// Monitors the progress of multiple bars until all of them are finished.
pub fn (bars []&Bar) watch(mut wg sync.WaitGroup) {
watch_(bars, mut wg) or { panic(err) } // can only fail on initial check if bars in bar array not set to `multi: true`.
pub fn (mut bars []&Bar) watch(mut wg sync.WaitGroup) {
watch_(mut bars, mut wg)
}

// == SmoothBar ===============================================================
Expand Down Expand Up @@ -127,8 +127,8 @@ pub fn (b SmoothBar) reader(reader io.Reader, size u64) &io.BufferedReader {
}

// Monitors the progress of multiple bars until all of them are finished.
pub fn (bars []&SmoothBar) watch(mut wg sync.WaitGroup) {
watch_(bars, mut wg) or { panic(err) } // can only fail on initial check if bars in bar array not set to `multi: true`.
pub fn (mut bars []&SmoothBar) watch(mut wg sync.WaitGroup) {
watch_(mut bars, mut wg)
}

// == Misc ====================================================================
Expand Down

0 comments on commit c4e4b7e

Please sign in to comment.