Skip to content

Commit

Permalink
Merge pull request #36 from AtelierArith/format-document
Browse files Browse the repository at this point in the history
format file according to .JuliaFormatter.toml
  • Loading branch information
terasakisatoshi authored Mar 27, 2022
2 parents cb3fe59 + 79827cb commit ebc8734
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 55 deletions.
7 changes: 7 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
23 changes: 16 additions & 7 deletions src/TerminalClock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand Down
108 changes: 61 additions & 47 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
end

0 comments on commit ebc8734

Please sign in to comment.