AshlinHarris / Spinners.jl

Command line spinners in Julia with Unicode support
MIT License
13 stars 1 forks source link

Switch back to external process? #102

Closed AshlinHarris closed 1 year ago

AshlinHarris commented 1 year ago

The following seems to be working on Windows now:

function spinner()
        local p # Process
        try
                # Generate the spinner command
                c = "while true;" *
                "for i in \"\\\\|/-\";" *
                "print(\"\\b\$i\");" *
                "sleep(0.1);" *
                "end;" *
                "end"

                # Display the spinner as an external program
                p = run(pipeline(` julia -e $c`, stdout), wait=false)

                # Do some actual work
                s = 0
                for i in 10:17
                        s += BigInt(999)^10_000_000 % 17
                end
                return(s)

        finally
                # Signal the external program to end
                kill(p)
                print("\b")
        end
end

println(spinner())

It might be feasible to abandon multithreading and parallel in favor of just shelling out with a new julia process. This would also work for Julia v1.2 and up, rather than relying on experimental features.

AshlinHarris commented 1 year ago

Currently, this doesn't work in Julia 1.9 on Linux. The process remains with the status ProcessSignaled(15) and isn't terminated.

AshlinHarris commented 1 year ago

I finally hacked together something that updates a spinner while calculations are done and closes the process (by sending a character to its stdin):

command = "t=Threads.@async read(stdin, Char);while !istaskdone(t);for q=['\\\\','|','/','-'];print(q);sleep(0.1);print('\b')end;end;exit()"
proc_input = Pipe()
proc = run(pipeline(`julia -e $command`, stdin = proc_input, stdout = stdout, stderr = stderr), wait = false)
sum(map(i->BigInt(999)^10_000_000 % i, 1:10)); # Do some calculations
write(proc_input,'c') #Signal the spinner process to stop

Now I can update the source and push v0.4.

AshlinHarris commented 1 year ago

I'll adapt the spinners from Spinners.jl (Oct. 5, 2022).