Nimでプログレスバー
はじめに
ディレクトリ内の全ファイルを開いてごにょごにょするプログラムをよく書きます。
自分で使う分には気にしないのですが、人から依頼された場合は「親切かな」と思い進捗率を表示させています。
最近マンネリなのでちょっとリッチなプログレスバーを試してみました。
環境
- windows10 home
- nim 0.19.0
いつものプログレスバー
例えばこういうディレクトリのとき
- xxx/logs
- 20190501.log
- 20190502.log
- 20190503.log
- ...
- 20190531.log
logs内のlogを開いてごにょごにょ、の進捗率はキャリッジリターンで表現しています
progress1.nim
import os, strformat
proc getFiles(dir: string): seq[string] =
# walkDirRecでもいいかも
for file in walkFiles(dir):
result.add(file)
when isMainModule:
let
dir = "xxxx\\logs\\*.log"
files = getFiles(dir)
total= len(files)
var done = 0
for file in files:
stdout.write(fmt"{$done} / {$total}")
stdout.write("\r") # キャリッジリターン(カーソルを左に戻す)
# ごにょごにょ
inc(done)
stdout.write(fmt"{$done} / {$total}")
stdout.write("\r\l")
stdout.write("finish!!")
リッチなプログレスバー
矢印を使って進捗率を表現したらリッチになりますね
そんなときはprogressライブラリを使います
nimble install progress
さきほどのソースはprogressを使うとこうなります
progress2.nim
import os, strformat, progress
proc getFiles(dir: string): seq[string] =
for file in walkFiles(dir):
result.add(file)
when isMainModule:
let
dir = "xxxx\\logs\\*.log"
files = getFiles(dir)
total = len(files)
var pb = newProgressBar(total = total) # totalは分母
pb.start()
for file in files:
# ごにょごにょ
pb.increment() # 今回ディレクトリに6ファイルあるので (x / 6 * 100) % を表示
pb.finish()
おわりに
ちょっとリッチになりましたね
え、プログレスバーより中身をリッチにしろ?
ほんそれ
Author And Source
この問題について(Nimでプログレスバー), 我々は、より多くの情報をここで見つけました https://qiita.com/NabeKz/items/69b3c628dab617a2e1bb著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .