From 79827cbfbf8d911d305798c2b73dcaec735fab8b Mon Sep 17 00:00:00 2001 From: SatoshiTerasaki Date: Sun, 27 Mar 2022 19:46:47 +0900 Subject: [PATCH] format file according to .JuliaFormatter.toml --- .JuliaFormatter.toml | 7 +++ docs/make.jl | 7 ++- src/TerminalClock.jl | 23 ++++++--- test/runtests.jl | 108 ++++++++++++++++++++++++------------------- 4 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 .JuliaFormatter.toml diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..47e66ed --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1,7 @@ +remove_extra_newlines=true +join_lines_based_on_source=true +whitespace_in_kwargs=false +short_to_long_function_def=true +always_for_in=true +verbose=true +margin=88 diff --git a/docs/make.jl b/docs/make.jl index 5e38e37..6f30ffe 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,7 +1,12 @@ using TerminalClock using Documenter -DocMeta.setdocmeta!(TerminalClock, :DocTestSetup, :(using TerminalClock); recursive=true) +DocMeta.setdocmeta!( + TerminalClock, + :DocTestSetup, + :(using TerminalClock); + recursive=true, +) makedocs(; modules=[TerminalClock], diff --git a/src/TerminalClock.jl b/src/TerminalClock.jl index 90c5515..1135ff8 100644 --- a/src/TerminalClock.jl +++ b/src/TerminalClock.jl @@ -56,7 +56,7 @@ n2d(n::Int, ::Val{:Large}) = n2d(DIALS_LARGE, n) n2d(n::Int, ::Val{:Small}) = n2d(DIALS_SMALL, n) n2d(n::Int) = n2d(n, :Large) -function clearline(; move_up::Bool = false) +function clearline(; move_up::Bool=false) buf = IOBuffer() print(buf, "\x1b[2K") # clear line print(buf, "\x1b[999D") # rollback the cursor @@ -65,8 +65,8 @@ function clearline(; move_up::Bool = false) end function clearlines(H::Integer) - for _ = 1:H - clearline(move_up = true) + for _ in 1:H + clearline(move_up=true) end end @@ -105,7 +105,11 @@ end const Optional = Union{Nothing,Int} -function setup_timer(; hour = nothing::Optional, minute = nothing::Optional, second = nothing::Optional) +function setup_timer(; + hour=nothing::Optional, + minute=nothing::Optional, + second=nothing::Optional, +) if all(isnothing.([hour, second, minute])) hour = 0 minute = 3 @@ -118,7 +122,11 @@ function setup_timer(; hour = nothing::Optional, minute = nothing::Optional, sec return Time(hour, minute, second) end -function countdown(; hour = nothing::Optional, minute = nothing::Optional, second = nothing::Optional) +function countdown(; + hour=nothing::Optional, + minute=nothing::Optional, + second=nothing::Optional, +) countdown(setup_timer(; hour, minute, second)) end @@ -141,13 +149,14 @@ function countdown(t::Time) end end -function stopwatch(duration = 0.1::AbstractFloat) +function stopwatch(duration=0.1::AbstractFloat) start = Dates.now() sleep(0.001) # warmup while true try Δ = Dates.now() - start # millisecond - periods = Dates.canonicalize(Dates.CompoundPeriod(Dates.Millisecond(Δ))).periods + periods = + Dates.canonicalize(Dates.CompoundPeriod(Dates.Millisecond(Δ))).periods str = stopwatch(Time(periods...)) println(str) sleep(duration) diff --git a/test/runtests.jl b/test/runtests.jl index f171213..d468c83 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,15 +21,16 @@ function with_temp_depot(f::Function) end # Taken from Prefernces.jl/test/runtests.jl -function activate_and_run(project_dir::String, code::String; env::Dict = Dict()) +function activate_and_run(project_dir::String, code::String; env::Dict=Dict()) mktempdir() do dir open(joinpath(dir, "test_code.jl"), "w") do io write(io, code) end out = Pipe() - cmd = setenv(`$(Base.julia_cmd()) --project=$(project_dir) $(dir)/test_code.jl`, - env..., "JULIA_DEPOT_PATH" => Base.DEPOT_PATH[1]) + cmd = + setenv(`$(Base.julia_cmd()) --project=$(project_dir) $(dir)/test_code.jl`, + env..., "JULIA_DEPOT_PATH" => Base.DEPOT_PATH[1]) p = run(pipeline(cmd, stdout=out, stderr=out); wait=false) close(out.in) wait(p) @@ -43,7 +44,7 @@ function activate_and_run(project_dir::String, code::String; env::Dict = Dict()) end @testset "Dial" begin - for n = 0:9 + for n in 0:9 @test length(split(n2d(n).str, "\n")) == 9 @test length(split(n2d(n), "\n")) == 9 end @@ -73,61 +74,74 @@ end end @testset "setup_timer" begin - @test setup_timer(hour = 1) == Time(1, 0, 0) - @test setup_timer(minute = 2) == Time(0, 2, 0) - @test setup_timer(second = 3) == Time(0, 0, 3) - @test setup_timer(hour = 1, minute = 2, second = 3) == Time(1, 2, 3) + @test setup_timer(hour=1) == Time(1, 0, 0) + @test setup_timer(minute=2) == Time(0, 2, 0) + @test setup_timer(second=3) == Time(0, 0, 3) + @test setup_timer(hour=1, minute=2, second=3) == Time(1, 2, 3) end @testset "preference" begin - local_prefs_toml = joinpath(dirname(dirname(pathof(TerminalClock))), "LocalPreferences.toml") + local_prefs_toml = + joinpath(dirname(dirname(pathof(TerminalClock))), "LocalPreferences.toml") rm(local_prefs_toml; force=true) with_temp_depot() do - project_dir = joinpath(dirname(@__DIR__,)) + project_dir = joinpath(dirname(@__DIR__)) # test for set_dials - activate_and_run(project_dir, """ - using Pkg; Pkg.instantiate() - using TerminalClock - unicodebox = joinpath(dirname(pathof(TerminalClock)), "dials", "UnicodeBox.toml") - TerminalClock.set_dials(unicodebox) - """) + activate_and_run( + project_dir, + """ + using Pkg; Pkg.instantiate() + using TerminalClock + unicodebox = joinpath(dirname(pathof(TerminalClock)), "dials", "UnicodeBox.toml") + TerminalClock.set_dials(unicodebox) +""", + ) prefs = local_prefs_toml |> TOML.parsefile @test haskey(prefs, "TerminalClock") @test basename(prefs["TerminalClock"]["tomlfile"]) == "UnicodeBox.toml" - activate_and_run(project_dir, """ - using Test - using TerminalClock, Dates - dt = DateTime(2021, 11, 15, 12, 34, 56, 7) - str = clock(dt) - txt = joinpath("references", "UnicodeBox", "clock.txt") - @test str == join(readlines(txt), "\n") + activate_and_run( + project_dir, + """ + using Test + using TerminalClock, Dates + dt = DateTime(2021, 11, 15, 12, 34, 56, 7) + str = clock(dt) + txt = joinpath("references", "UnicodeBox", "clock.txt") + @test str == join(readlines(txt), "\n") - t = Time(12, 34, 56, 789) - str = stopwatch(t) - txt = joinpath("references", "UnicodeBox", "stopwatch.txt") - @test str == join(readlines(txt), "\n") - """) + t = Time(12, 34, 56, 789) + str = stopwatch(t) + txt = joinpath("references", "UnicodeBox", "stopwatch.txt") + @test str == join(readlines(txt), "\n") +""", + ) # test for clear_dials - activate_and_run(project_dir, """ - using TerminalClock - TerminalClock.clear_dials() - """) - - activate_and_run(project_dir, """ - using Test - using TerminalClock, Dates - dt = DateTime(2021, 11, 15, 12, 34, 56, 7) - str = clock(dt) - txt = joinpath("references", "ASCII", "clock.txt") - @test str == join(readlines(txt), "\n") - - t = Time(12, 34, 56, 789) - str = stopwatch(t) - txt = joinpath("references", "ASCII", "stopwatch.txt") - @test str == join(readlines(txt), "\n") - """) + activate_and_run( + project_dir, + """ + using TerminalClock + TerminalClock.clear_dials() +""", + ) + + activate_and_run( + project_dir, + """ + using Test + using TerminalClock, Dates + dt = DateTime(2021, 11, 15, 12, 34, 56, 7) + str = clock(dt) + txt = joinpath("references", "ASCII", "clock.txt") + @test str == join(readlines(txt), "\n") + + t = Time(12, 34, 56, 789) + str = stopwatch(t) + txt = joinpath("references", "ASCII", "stopwatch.txt") + @test str == join(readlines(txt), "\n") +""", + ) end # with_temp_depot -end \ No newline at end of file +end