From 800355ab6f399fb60b7eaa93e6e15a5e7c60ecfa Mon Sep 17 00:00:00 2001 From: Sakse Date: Mon, 17 Aug 2020 21:53:31 +0200 Subject: [PATCH] Add `persistent_history` configuration. --- Project.toml | 2 +- docs/src/configuration.md | 16 +++++++++------- src/Diary.jl | 21 ++++++++++++--------- test/runtests.jl | 39 ++++++++++++++++++++++++++++++++++----- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/Project.toml b/Project.toml index 0d3177a..95a524f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Diary" uuid = "601e5a01-99c6-4314-b727-62e0eec533dd" authors = ["Sakse "] -version = "0.1.0" +version = "0.1.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/docs/src/configuration.md b/docs/src/configuration.md index 30743a6..3409749 100644 --- a/docs/src/configuration.md +++ b/docs/src/configuration.md @@ -5,16 +5,18 @@ Configuration of Diary.jl is done through `Diary.toml` files. A global configur An example `Diary.toml` file looks like the following: ```toml author = "Anna" +autocommit = true +blacklist = ["/home/anna/.julia/environments"] date_format = "E U d HH:MM" diary_name = "diary.jl" -autocommit = true directory_mode = false -blacklist = ["/home/anna/.julia/environments"] ``` If a field is not set, it will be set to a default value. -- The `author` and `date_format` fields affect the header written in diary files and default to `""` and `"E U d HH:MM"` respectively. For a full list of date formatting options, consult the documentation for `Dates.format`. -- `diary_file` specifies the name to be used for the diary file and defaults to `"diary.jl"`. -- `autocommit` defaults to `true`. If set to `false`, the diary file will not be automatically updated with the most recent history file changes. Instead, changes must be manually committed by using the diary command comment syntax, `# diary: commit [n]` to commit the `n` most recent code blocks. -- `directory_mode` defaults to `false`. If set to `true`, the root folder of the diary file is set to the current working directory, rather than the project directory. -- `blacklist` is a list of patterns that will disable Diary.jl, for projects that match them. By default, the list is set to `["$HOME/.julia/environments"]`. Set this to an empty vector to enable Diary.jl for all projects. +- `author`: defaults to `""`. Written as part of the comment header to the diary file at the start of every session. +- `autocommit`: defaults to `true`. If set to `false`, the diary file will not be automatically updated with the most recent history file changes. Instead, changes must be manually committed by using the diary command comment syntax, `# diary: commit [n]` to commit the `n` most recent code blocks. +- `blacklist`: defaults to `["$HOME/.julia/environments"]`. `blacklist` is a list of patterns that will disable Diary.jl, if a name or part of the path to a project matches it. Set this to an empty vector to enable Diary.jl for all projects. +- `date_format`: defaults to `"E U d HH:MM"`. The format of the date that is written to the comment header at the start of every session. For a full list of date formatting options, see the documentation for `Dates.format`. +- `diary_file`: defaults to `diary.jl`. Specifies the name to be used for the diary file. +- `directory_mode`: defaults to `false`. If set to `true`, the root folder of the diary file is set to the current working directory, rather than the project directory. +- `persistent_history`: defaults to `true`. If set to `false`, the REPL history will not be saved for future sessions. This option does not affect the diary file. diff --git a/src/Diary.jl b/src/Diary.jl index edcbc06..002d5cd 100644 --- a/src/Diary.jl +++ b/src/Diary.jl @@ -67,8 +67,10 @@ function watch_task(history_file, repl_history_file=nothing) if file_event.changed history_lines = readlines(history_file_handle) @debug "Diary.jl ($history_file): History file has changed:" history_lines + # Read user configuration. + configuration = read_configuration() # Copy history lines to the REPL history file - if !isnothing(repl_history_file) + if configuration["persistent_history"] && !isnothing(repl_history_file) open(repl_history_file, read=true, write=true) do io seekend(io) println(io, join(history_lines, '\n')) @@ -84,8 +86,6 @@ function watch_task(history_file, repl_history_file=nothing) continue end push!(GLOBAL_SEGMENT_BUFFER, diary_lines) - # Read user configuration. - configuration = read_configuration() # Skip if auto-committing is disabled. !configuration["autocommit"] && continue # Locate the diary file. @@ -190,7 +190,9 @@ function find_diary(; configuration=read_configuration()) end # Create the diary file if missing. !isfile(diary_file) && touch(diary_file) - + # Allow task switching by sleeping for 1 ms. Necessary for the tests to pass. + # Shouldn't affect normal usage. + sleep(0.001) return diary_file end @@ -233,13 +235,14 @@ Return the default configuration. function default_configuration() return Dict{String,Any}( "author" => "", - "diary_name" => "diary.jl", - "date_format" => "E U d HH:MM", "autocommit" => true, - "directory_mode" => false, "blacklist" => [ - joinpath(ENV["HOME"], ".julia", "environments"), - ] + joinpath(ENV["HOME"], ".julia", "environments"), + ], + "date_format" => "E U d HH:MM", + "diary_name" => "diary.jl", + "directory_mode" => false, + "persistent_history" => true, ) end diff --git a/test/runtests.jl b/test/runtests.jl index 73291b4..38f6474 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -54,13 +54,14 @@ end Pkg.activate() default_configuration = Dict{String,Any}( "author" => "", - "diary_name" => "diary.jl", - "date_format" => "E U d HH:MM", "autocommit" => true, - "directory_mode" => false, "blacklist" => [ - joinpath(ENV["HOME"], ".julia", "environments"), - ] + joinpath(ENV["HOME"], ".julia", "environments"), + ], + "date_format" => "E U d HH:MM", + "diary_name" => "diary.jl", + "directory_mode" => false, + "persistent_history" => true, ) ENV["JULIA_DIARY_CONFIG"] = tempname() @@ -171,6 +172,7 @@ end # Re-initialise Diary, enabling the watcher. ENV["JULIA_HISTORY"] = tempname() touch(ENV["JULIA_HISTORY"]) + repl_history_file = ENV["JULIA_HISTORY"] Diary.__init__(enabled=true) # Locate relevant files. history_file = ENV["JULIA_HISTORY"] @@ -200,6 +202,7 @@ end file_event = FileWatching.watch_file(diary_file, 5) @test file_event.changed || file_event.timedout @test readlines(diary_file) == ["# Test: ", "", "a = rand(100)"] + @test readlines(repl_history_file) == history_lines # Add line with incorrect syntax and check that it does not update the diary file. history_lines = [ @@ -217,6 +220,32 @@ end @test file_event.timedout @test readlines(diary_file) == ["# Test: ", "", "a = rand(100)"] + @testset "Non-persistent history" begin + previous_history_lines = readlines(repl_history_file) + + open(configuration_file, write=true) do io + configuration = [ + "author = \"Test\"", + "date_format = \"\"", + "persistent_history = false" + ] + join(io, configuration, "\n") + print(io, "\n") + end + # Simulate user interaction by writing lines to the history file. + open(history_file, read=true, write=true) do io + seekend(io) + join(io, ["# time: ***", "# mode: julia", "\tb = 42"], "\n") + print(io, "\n") + end + # Allow the `watch_task` to update the diary file. + file_event = FileWatching.watch_file(diary_file, 5) + @test file_event.changed || file_event.timedout + @test readlines(diary_file) == ["# Test: ", "", "a = rand(100)", "b = 42"] + # Test that the original history file hasn't changed. + @test readlines(repl_history_file) == previous_history_lines + end + # Clean-up rm(diary_file) end