Skip to content

Commit

Permalink
Add persistent_history configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
dalum committed Aug 17, 2020
1 parent 9a71d3f commit 800355a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Diary"
uuid = "601e5a01-99c6-4314-b727-62e0eec533dd"
authors = ["Sakse <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
16 changes: 9 additions & 7 deletions docs/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
21 changes: 12 additions & 9 deletions src/Diary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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.
Expand Down Expand Up @@ -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

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

Expand Down
39 changes: 34 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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 = [
Expand All @@ -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
Expand Down

2 comments on commit 800355a

@dalum
Copy link
Owner Author

@dalum dalum commented on 800355a Aug 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/19683

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 800355ab6f399fb60b7eaa93e6e15a5e7c60ecfa
git push origin v0.1.1

Please sign in to comment.