Created
September 20, 2024 19:54
-
-
Save peteristhegreat/488c3cceda8be504a60787dff197af34 to your computer and use it in GitHub Desktop.
Julia progress bar and spinner from Pkg, no additional packages needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Pkg | |
using Pkg.MiniProgressBars | |
# Function that simulates a task with progress | |
function example_task_with_progress_bar(n::Int) | |
bar = MiniProgressBar(; indent=2, header="Processing items", color=Base.info_color()) | |
bar.max = n | |
fancyprint = Pkg.can_fancyprint(stderr) | |
fancyprint && start_progress(stderr, bar) | |
for i in 1:n | |
# Simulate work by sleeping for a bit | |
sleep(0.1) | |
# Update progress bar | |
bar.current = i | |
fancyprint && show_progress(stderr, bar) | |
end | |
fancyprint && end_progress(stderr, bar) | |
println("Task complete!") | |
end | |
# Dictionary to store task handles by task name | |
const task_handles = Dict{String, Any}() | |
# Function to start a spinner with a task description | |
function start_spinner(task_name::String, task_description::String) | |
anim_chars = ["◐", "◓", "◑", "◒"] # Spinner characters | |
i = 1 | |
fancyprint = Pkg.can_fancyprint(stderr) | |
timer = Timer(0; interval=1/10) # Timer for spinner speed | |
# Start the spinner and store the task in the dictionary | |
spinner_task = @async try | |
while isopen(timer) | |
if fancyprint | |
print("\r", anim_chars[i], " - ", task_description) | |
i = mod(i, length(anim_chars)) + 1 | |
flush(stdout) | |
end | |
wait(timer) # Spinner update interval | |
end | |
catch | |
# do nothing | |
finally | |
# interrupt exception | |
print("\r", "✔ - ", task_description) | |
flush(stdout) | |
println() | |
end | |
# Store the task handle in the dictionary | |
task_handles[task_name] = (spinner_task, timer) | |
end | |
# Function to stop the spinner for a given task name | |
function stop_spinner(task_name::String) | |
if haskey(task_handles, task_name) | |
task, timer = task_handles[task_name] | |
# cancel(task) # Doesn't exists | |
# Base.throwto(task, InterruptException()) # sort of works to kill an async | |
close(timer) | |
wait(task) | |
delete!(task_handles, task_name) | |
else | |
println("No such task: $task_name") | |
end | |
end | |
# Example function that simulates a task with a spinner | |
function example_task_with_spinner(n::Int) | |
task_name = "example_task" | |
task_description = "Processing items" | |
# Start the spinner | |
start_spinner(task_name, task_description) | |
# Simulate a task that takes some time | |
for _ in 1:n | |
sleep(0.1) | |
end | |
# Stop the spinner when the task is done | |
stop_spinner(task_name) | |
end | |
# Main function to call the task | |
function main_spinner() | |
n = 100 # Number of tasks | |
example_task_with_spinner(n) | |
end | |
# Main function to call the task | |
function main_progress_bar() | |
n = 100 # Number of tasks | |
example_task_with_progress_bar(n) | |
end | |
main_spinner() | |
# main_progress_bar() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment