2022-02-24 07:08:40 +01:00
|
|
|
package obiiter
|
2022-02-18 22:53:09 +01:00
|
|
|
|
2022-02-21 19:00:23 +01:00
|
|
|
import (
|
2023-02-16 13:32:27 +01:00
|
|
|
"fmt"
|
2022-02-21 19:00:23 +01:00
|
|
|
"os"
|
2023-02-23 23:35:58 +01:00
|
|
|
"time"
|
2022-02-21 19:00:23 +01:00
|
|
|
|
|
|
|
"github.com/schollz/progressbar/v3"
|
|
|
|
)
|
|
|
|
|
2024-07-12 15:17:48 +02:00
|
|
|
func (iterator IBioSequence) Speed(message string, size ...int) IBioSequence {
|
2023-02-16 13:32:27 +01:00
|
|
|
|
|
|
|
// If the STDERR is redicted and doesn't end up to a terminal
|
|
|
|
// No progress bar is printed.
|
|
|
|
o, _ := os.Stderr.Stat()
|
|
|
|
if (o.Mode() & os.ModeCharDevice) != os.ModeCharDevice {
|
|
|
|
return iterator
|
|
|
|
}
|
|
|
|
|
2023-01-22 22:04:17 +01:00
|
|
|
newIter := MakeIBioSequence()
|
2022-02-21 19:00:23 +01:00
|
|
|
|
|
|
|
newIter.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
newIter.WaitAndClose()
|
|
|
|
}()
|
|
|
|
|
2022-02-24 12:14:52 +01:00
|
|
|
pbopt := make([]progressbar.Option, 0, 5)
|
|
|
|
pbopt = append(pbopt,
|
2022-02-21 19:00:23 +01:00
|
|
|
progressbar.OptionSetWriter(os.Stderr),
|
|
|
|
progressbar.OptionSetWidth(15),
|
|
|
|
progressbar.OptionShowCount(),
|
|
|
|
progressbar.OptionShowIts(),
|
2024-07-12 15:17:48 +02:00
|
|
|
progressbar.OptionSetDescription(message),
|
2022-02-24 12:14:52 +01:00
|
|
|
)
|
|
|
|
|
2024-07-12 15:17:48 +02:00
|
|
|
n := -1
|
|
|
|
if len(size) > 0 {
|
|
|
|
n = size[0]
|
2022-02-24 12:14:52 +01:00
|
|
|
}
|
|
|
|
|
2024-07-12 15:17:48 +02:00
|
|
|
bar := progressbar.NewOptions(n, pbopt...)
|
2022-02-21 19:00:23 +01:00
|
|
|
|
|
|
|
go func() {
|
2023-02-23 23:35:58 +01:00
|
|
|
c := 0
|
|
|
|
start := time.Now()
|
2022-02-21 19:00:23 +01:00
|
|
|
|
|
|
|
for iterator.Next() {
|
|
|
|
batch := iterator.Get()
|
2023-02-23 23:35:58 +01:00
|
|
|
c += batch.Len()
|
2022-02-21 19:00:23 +01:00
|
|
|
newIter.Push(batch)
|
2023-02-23 23:35:58 +01:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
if elapsed > (time.Millisecond * 100) {
|
|
|
|
bar.Add(c)
|
|
|
|
c = 0
|
|
|
|
start = time.Now()
|
|
|
|
}
|
2022-02-21 19:00:23 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 13:32:27 +01:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2022-02-21 19:00:23 +01:00
|
|
|
newIter.Done()
|
|
|
|
}()
|
|
|
|
|
2023-02-23 23:35:58 +01:00
|
|
|
if iterator.IsPaired() {
|
|
|
|
newIter.MarkAsPaired()
|
|
|
|
}
|
2024-07-12 15:17:48 +02:00
|
|
|
|
2022-02-18 22:53:09 +01:00
|
|
|
return newIter
|
|
|
|
}
|
2022-02-24 07:08:40 +01:00
|
|
|
|
2024-07-12 15:17:48 +02:00
|
|
|
func SpeedPipe(message string) Pipeable {
|
2023-01-22 22:04:17 +01:00
|
|
|
f := func(iterator IBioSequence) IBioSequence {
|
2024-07-12 15:17:48 +02:00
|
|
|
return iterator.Speed(message)
|
2022-02-24 07:08:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
2022-02-24 12:14:52 +01:00
|
|
|
}
|