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

refactor: update structure #3

Merged
merged 5 commits into from
Jun 4, 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
16 changes: 0 additions & 16 deletions src/affixations.v → src/instructioins_affixations.v
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
module bartender

pub struct Affix {
pub mut:
pending string
finished string
}

enum AffixState {
pending
finished
}

type AffixInput = Affix
| fn (b Bar) (string, string)
| fn (b SmoothBar) (string, string)
| string

fn (a AffixInput) resolve_affix(b BarType, state AffixState) string {
return match a {
fn (SmoothBar) (string, string) {
Expand Down
23 changes: 23 additions & 0 deletions src/instructions_base.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module bartender

import term

fn (mut b BarBase) set_fit_width() {
term_width, _ := term.get_terminal_size()
affix_width := utf8_str_visible_length(term.strip_ansi(b.pre_)) +
utf8_str_visible_length(term.strip_ansi(b.post_))

if term_width > b.width_ + affix_width {
return
}
new_width := u16(term_width - affix_width)
diff := b.width_ - new_width

if diff > b.state.pos {
b.state.pos = 0
} else {
b.state.pos -= diff
}

b.width_ = new_width
}
40 changes: 0 additions & 40 deletions src/colors.v → src/instructions_colors.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,6 @@ module bartender

import term

type BarColorType = BarColor | Color
type ColorType = Color | FgBg

pub struct BarColor {
progress ColorType = Color.white
remaining ColorType = Color.bright_black
indicator ColorType = Color.white
}

pub struct FgBg {
fg Color = .white
bg Color = .reset
}

enum Surface {
fg
bg
}

pub enum Color {
reset
black
red
green
yellow
blue
magenta
cyan
white
gray
bright_black
bright_red
bright_green
bright_yellow
bright_blue
bright_magenta
bright_cyan
bright_white
}

fn (color Color) paint(s string, surface Surface) string {
// vfmt off
return term.colorize(match color {
Expand Down
36 changes: 36 additions & 0 deletions src/instructions_reader.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module bartender

import io

const buf_max_len = 1024

fn bar_reader_(b BarType, bytes []u8) &io.BufferedReader {
return match b {
Bar {
io.new_buffered_reader(
reader: BarReader{
bytes: bytes
size: bytes.len
bar: b
}
)
}
SmoothBar {
io.new_buffered_reader(
reader: SmoothBarReader{
bytes: bytes
size: bytes.len
bar: b
}
)
}
}
}

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
}
}
104 changes: 50 additions & 54 deletions src/bar.v → src/instructions_simple_bar.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,7 @@ module bartender
import term
import time
import io

pub struct Bar {
BarBase
pub mut:
runes BarRunes
pre AffixInput = '['
post AffixInput = fn (b Bar) (string, string) {
return '] ${b.pct()}% (${b.eta(0)})', '] ${b.pct()}%'
}
mut:
runes_ BarRunes_
indicator_ string
}

pub struct BarRunes {
progress rune = `#`
indicator ?rune
remaining rune = ` `
}

// Internally resolve to strings instead of runes for color support.
struct BarRunes_ {
progress string
indicator string
remaining string
}

struct BarReader {
BarReaderBase
mut:
bar Bar
}
import os

fn (mut b Bar) setup() {
b.state.pos = 0
Expand Down Expand Up @@ -112,33 +81,60 @@ fn (mut b Bar) colorize_components(color BarColor) {
}
}

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_ {
finished = false
// Functions for exposure.

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) }
}
if b.state.pos >= b.width_ {
panic(IError(BarError{ kind: .finished }))
}
println(formatted.join_lines())
if !finished {
term.cursor_up(bars.len)

b.set_vals()
if b.multi {
return
}
b.draw()
if b.state.pos >= b.width_ {
term.show_cursor()
}
return finished
}

fn (bars []&Bar) ensure_mutli() ! {
mut not_multi := []int{}
for i, bar in bars {
if !bar.multi {
not_multi << i
}
fn (mut b Bar) colorize_(color BarColorType) {
b.setup()
if color is BarColor {
b.colorize_components(color)
} else {
b.colorize_uni(color as Color)
}
}

fn (b Bar) eta_(delay u8) string {
if delay > 100 {
panic(IError(BarError{ kind: .delay_exceeded }))
}
if not_multi.len > 0 {
return IError(BarError{
kind: .missing_multi
msg: '${not_multi}'
})
next_pos := b.state.pos + 1
if next_pos < f32(b.width_) * delay / 100 {
return b.spinner()
}
// Avg. time(until current position) to move up one position * remaining positions.
return '${f64(b.state.time.last_change - b.state.time.start) / next_pos * (b.width_ - next_pos) / 1000:.1f}s'
}

fn (b Bar) pct_() u16 {
if b.width_ == 0 {
return 0
}
return (b.state.pos + 1) * 100 / b.width_
}

fn (mut b Bar) reset_() {
b.setup()
b.state.time = struct {0, 0}
}
53 changes: 53 additions & 0 deletions src/instructions_simple_bar_multi.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module bartender

import term
import time
import sync

fn (bars []&Bar) watch_(mut wg sync.WaitGroup) {
bars.ensure_mutli() or {
eprintln(err)
exit(0)
}
time.sleep(time.millisecond * 15)
for {
if bars.draw() {
term.show_cursor()
break
}
// Redraw the bars every 15ms to reduce load and prevent flashing output.
time.sleep(time.millisecond * 15)
}
wg.done()
}

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_ {
finished = false
}
}
println(formatted.join_lines())
if !finished {
term.cursor_up(bars.len)
}
return finished
}

fn (bars []&Bar) ensure_mutli() ! {
mut not_multi := []int{}
for i, bar in bars {
if !bar.multi {
not_multi << i
}
}
if not_multi.len > 0 {
return IError(BarError{
kind: .missing_multi
msg: '${not_multi}'
})
}
}
Loading