Skip to content

Commit

Permalink
fix!: rewrite bar reader to work with an user specified io.Reader (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Jun 12, 2023
1 parent 6f7c827 commit 8a7d676
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 101 deletions.
34 changes: 23 additions & 11 deletions examples/reader.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ import os
import time
import bartender

// This example shows the progress of writing content to an io.Writer (i.e., a file).
// Example: Creating a bar reader from the file reader of `src_file` (with 500MiB dummy content)
// and copying its contents to `dst_file`.
fn main() {
mut start := time.ticks()
mut start := time.new_stopwatch()

// Create a reader with byte content to be written.
// In this example, 500 megabytes of dummy content.
mut r := bartender.bar_reader(bartender.Bar{}, '1234567890'.repeat(50 * 1024 * 1024).bytes())
mut f := os.create('testfile')!
io.cp(mut r, mut f)!
f.close()
src_file_path := './examples/dummy-src-file.txt'
dst_file_path := './examples/dummy-dst-file.txt'

println('Completed in ${f64(time.ticks() - start) / 1000:.2f}s')
// Cleanup - delete written file.
os.rm('testfile')!
os.write_file(src_file_path, '123456789\n'.repeat(50 * 1024 * 1024))!

mut src_file := os.open(src_file_path)!
mut dst_file := os.create(dst_file_path)!
defer {
src_file.close()
dst_file.close()
os.rm(src_file_path) or { panic(err) }
os.rm(dst_file_path) or { panic(err) }
}

bar := bartender.Bar{}
// Pass src_file as `io.Reader` to use it in a bar reader.
mut bar_reader := bar.reader(src_file, os.file_size(src_file_path))
// Use the `bar_reader`
io.cp(mut bar_reader, mut dst_file)!

println('Completed in ${start.elapsed()}')
}
42 changes: 35 additions & 7 deletions examples/reader_smooth.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,36 @@ import time
import term
import bartender

struct MyCustomReader {
size u64 [required]
mut:
reader io.Reader [required]
pos int
}

fn (mut r MyCustomReader) read(mut buf []u8) !int {
if r.pos >= r.size {
return io.Eof{}
}
n := r.reader.read(mut buf)!
r.pos += n
time.sleep(10 * time.millisecond)
return n
}

fn main() {
mut start := time.ticks()
// Prepare dummy files.
src_file_path := './examples/dummy-src-file.txt'
dst_file_path := './examples/dummy-dst-file.txt'
os.write_file(src_file_path, '123456789\n'.repeat(10 * 1024 * 1024))!
mut src_file := os.open(src_file_path)!
mut dst_file := os.create(dst_file_path)!
defer {
src_file.close()
dst_file.close()
os.rm(src_file_path) or { panic(err) }
os.rm(dst_file_path) or { panic(err) }
}

// Create a smooth bar. Apply customizations.
mut b := bartender.SmoothBar{
Expand All @@ -18,11 +46,11 @@ fn main() {
}
b.colorize(.cyan)

mut r := bartender.bar_reader(b, '1234567890'.repeat(50 * 1024 * 1024).bytes())
mut f := os.create('testfile')!
io.cp(mut r, mut f)!
f.close()
r := MyCustomReader{
reader: src_file
size: os.file_size(src_file_path)
}

println('Completed in ${f64(time.ticks() - start) / 1000:.2f}s')
os.rm('testfile')!
mut bar_reader := b.reader(r, r.size)
io.cp(mut bar_reader, mut dst_file)!
}
50 changes: 25 additions & 25 deletions src/_instructions_reader.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ module bartender

import io

const buf_max_len = 1024
fn bar_reader(b BarType, reader io.Reader, size u64) &io.BufferedReader {
return io.new_buffered_reader(
reader: BarReader{
bar: b
reader: reader
size: size
}
)
}

fn bar_reader_(b BarType, bytes []u8) &io.BufferedReader {
return match b {
fn (mut br BarReader) read(mut buf []u8) !int {
if br.pos >= br.size {
return io.Eof{}
}
n := br.reader.read(mut buf)!
br.pos += n
match mut br.bar {
// Unfortunately, comma separation doesn't work here ATM.
// SmoothBar won't be visible or will have corrupted chars.
Bar {
io.new_buffered_reader(
reader: BarReader{
bytes: bytes
size: bytes.len
bar: b
}
)
if (f64(br.pos) / br.size * br.bar.width) > br.bar.state.pos {
br.bar.progress()
}
}
SmoothBar {
io.new_buffered_reader(
reader: SmoothBarReader{
bytes: bytes
size: bytes.len
bar: b
}
)
if (f64(br.pos) / br.size * br.bar.width) > br.bar.state.pos {
br.bar.progress()
}
}
}
}

fn get_buf_end(r BarReaderType) int {
return if r.pos + bartender.buf_max_len >= r.size {
r.size
} else {
r.pos + bartender.buf_max_len
}
return n
}
16 changes: 0 additions & 16 deletions src/_instructions_simple_bar.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module bartender

import term
import time
import io
import os

fn (mut b Bar) setup() {
Expand Down Expand Up @@ -47,21 +46,6 @@ fn (b Bar) format() string {
return left + indicator + right
}

fn (mut r BarReader) read(mut buf []u8) !int {
if r.pos >= r.size {
return io.Eof{}
}

n := copy(mut buf, r.bytes[r.pos..get_buf_end(r)])
r.pos += n

if (f64(r.pos) / r.size * r.bar.width) > r.bar.pos() {
r.bar.progress()
}

return n
}

fn (mut b Bar) colorize_uni(color Color) {
b.runes_ = BarRunes_{
progress: color.paint(b.runes_.progress, .fg)
Expand Down
16 changes: 0 additions & 16 deletions src/_instructions_smooth_bar.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module bartender

import term
import time
import io
import os

fn (mut b SmoothBar) setup() {
Expand Down Expand Up @@ -183,21 +182,6 @@ fn (b SmoothBar) format() string {
}
}

fn (mut r SmoothBarReader) read(mut buf []u8) !int {
if r.pos >= r.size {
return io.Eof{}
}

n := copy(mut buf, r.bytes[r.pos..get_buf_end(r)])
r.pos += n

if (f64(r.pos) / r.size * r.bar.width) > r.bar.pos() {
r.bar.progress()
}

return n
}

fn (b SmoothBar) next_pos() u16 {
return b.state.pos + u16(if b.theme_ == .push || b.theme_ == .pull {
1
Expand Down
13 changes: 7 additions & 6 deletions src/_state_common.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module bartender

import io

struct BarBase {
pub mut:
width u16 = 60
Expand All @@ -17,11 +19,12 @@ mut:
post_ string
}

struct BarReaderBase {
bytes []u8
size int
struct BarReader {
size u64
mut:
pos int
bar BarType
pos int
reader io.Reader
}

struct State {
Expand All @@ -36,6 +39,4 @@ mut:

type BarType = Bar | SmoothBar

type BarReaderType = BarReader | SmoothBarReader

type MultiBarType = []&Bar | []&SmoothBar
6 changes: 0 additions & 6 deletions src/_state_simple.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,3 @@ struct BarRunes_ {
indicator string
remaining string
}

struct BarReader {
BarReaderBase
mut:
bar Bar
}
6 changes: 0 additions & 6 deletions src/_state_smooth.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ mut: // Strings instead of runes for color support.
sm []string // Smooth Mirrored. Used for merge, expand and split variant.
}

struct SmoothBarReader {
BarReaderBase
mut:
bar SmoothBar
}

const (
smooth_ltr = [' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█']
smooth_rtl = ['█', '🮋', '🮊', '🮉', '▐', '🮈', '🮇', '▕', ' ']
Expand Down
12 changes: 4 additions & 8 deletions src/lib.v
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ pub fn (mut b Bar) reset() {
}

// Returns a `io.BufferedReader` that displays a progressing bar when used in a reader operation.
pub fn (mut b Bar) bar_reader(bytes []u8) &io.BufferedReader {
return bar_reader_(b, bytes)
pub fn (b Bar) reader(reader io.Reader, size u64) &io.BufferedReader {
return bar_reader(b, reader, size)
}

// Monitors the progress of multiple bars until all of them are finished.
Expand Down Expand Up @@ -122,8 +122,8 @@ pub fn (mut b SmoothBar) reset() {
}

// Returns a `io.BufferedReader` that displays a progressing bar when used in a reader operation.
pub fn (mut b SmoothBar) bar_reader(bytes []u8) &io.BufferedReader {
return bar_reader_(b, bytes)
pub fn (b SmoothBar) reader(reader io.Reader, size u64) &io.BufferedReader {
return bar_reader(b, reader, size)
}

// Monitors the progress of multiple bars until all of them are finished.
Expand All @@ -133,10 +133,6 @@ pub fn (bars []&SmoothBar) watch(mut wg sync.WaitGroup) {

// == Misc ====================================================================

pub fn bar_reader(b BarType, bytes []u8) &io.BufferedReader {
return bar_reader_(b, bytes)
}

// Returns the bar's current position.
pub fn (b BarBase) pos() u16 {
return b.state.pos
Expand Down

0 comments on commit 8a7d676

Please sign in to comment.