Skip to content

Commit 800355a

Browse files
committed
Add persistent_history configuration.
1 parent 9a71d3f commit 800355a

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Diary"
22
uuid = "601e5a01-99c6-4314-b727-62e0eec533dd"
33
authors = ["Sakse <[email protected]>"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

docs/src/configuration.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ Configuration of Diary.jl is done through `Diary.toml` files. A global configur
55
An example `Diary.toml` file looks like the following:
66
```toml
77
author = "Anna"
8+
autocommit = true
9+
blacklist = ["/home/anna/.julia/environments"]
810
date_format = "E U d HH:MM"
911
diary_name = "diary.jl"
10-
autocommit = true
1112
directory_mode = false
12-
blacklist = ["/home/anna/.julia/environments"]
1313
```
1414
If a field is not set, it will be set to a default value.
1515

16-
- 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`.
17-
- `diary_file` specifies the name to be used for the diary file and defaults to `"diary.jl"`.
18-
- `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.
19-
- `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.
20-
- `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.
16+
- `author`: defaults to `""`. Written as part of the comment header to the diary file at the start of every session.
17+
- `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.
18+
- `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.
19+
- `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`.
20+
- `diary_file`: defaults to `diary.jl`. Specifies the name to be used for the diary file.
21+
- `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.
22+
- `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.

src/Diary.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ function watch_task(history_file, repl_history_file=nothing)
6767
if file_event.changed
6868
history_lines = readlines(history_file_handle)
6969
@debug "Diary.jl ($history_file): History file has changed:" history_lines
70+
# Read user configuration.
71+
configuration = read_configuration()
7072
# Copy history lines to the REPL history file
71-
if !isnothing(repl_history_file)
73+
if configuration["persistent_history"] && !isnothing(repl_history_file)
7274
open(repl_history_file, read=true, write=true) do io
7375
seekend(io)
7476
println(io, join(history_lines, '\n'))
@@ -84,8 +86,6 @@ function watch_task(history_file, repl_history_file=nothing)
8486
continue
8587
end
8688
push!(GLOBAL_SEGMENT_BUFFER, diary_lines)
87-
# Read user configuration.
88-
configuration = read_configuration()
8989
# Skip if auto-committing is disabled.
9090
!configuration["autocommit"] && continue
9191
# Locate the diary file.
@@ -190,7 +190,9 @@ function find_diary(; configuration=read_configuration())
190190
end
191191
# Create the diary file if missing.
192192
!isfile(diary_file) && touch(diary_file)
193-
193+
# Allow task switching by sleeping for 1 ms. Necessary for the tests to pass.
194+
# Shouldn't affect normal usage.
195+
sleep(0.001)
194196
return diary_file
195197
end
196198

@@ -233,13 +235,14 @@ Return the default configuration.
233235
function default_configuration()
234236
return Dict{String,Any}(
235237
"author" => "",
236-
"diary_name" => "diary.jl",
237-
"date_format" => "E U d HH:MM",
238238
"autocommit" => true,
239-
"directory_mode" => false,
240239
"blacklist" => [
241-
joinpath(ENV["HOME"], ".julia", "environments"),
242-
]
240+
joinpath(ENV["HOME"], ".julia", "environments"),
241+
],
242+
"date_format" => "E U d HH:MM",
243+
"diary_name" => "diary.jl",
244+
"directory_mode" => false,
245+
"persistent_history" => true,
243246
)
244247
end
245248

test/runtests.jl

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ end
5454
Pkg.activate()
5555
default_configuration = Dict{String,Any}(
5656
"author" => "",
57-
"diary_name" => "diary.jl",
58-
"date_format" => "E U d HH:MM",
5957
"autocommit" => true,
60-
"directory_mode" => false,
6158
"blacklist" => [
62-
joinpath(ENV["HOME"], ".julia", "environments"),
63-
]
59+
joinpath(ENV["HOME"], ".julia", "environments"),
60+
],
61+
"date_format" => "E U d HH:MM",
62+
"diary_name" => "diary.jl",
63+
"directory_mode" => false,
64+
"persistent_history" => true,
6465
)
6566

6667
ENV["JULIA_DIARY_CONFIG"] = tempname()
@@ -171,6 +172,7 @@ end
171172
# Re-initialise Diary, enabling the watcher.
172173
ENV["JULIA_HISTORY"] = tempname()
173174
touch(ENV["JULIA_HISTORY"])
175+
repl_history_file = ENV["JULIA_HISTORY"]
174176
Diary.__init__(enabled=true)
175177
# Locate relevant files.
176178
history_file = ENV["JULIA_HISTORY"]
@@ -200,6 +202,7 @@ end
200202
file_event = FileWatching.watch_file(diary_file, 5)
201203
@test file_event.changed || file_event.timedout
202204
@test readlines(diary_file) == ["# Test: ", "", "a = rand(100)"]
205+
@test readlines(repl_history_file) == history_lines
203206

204207
# Add line with incorrect syntax and check that it does not update the diary file.
205208
history_lines = [
@@ -217,6 +220,32 @@ end
217220
@test file_event.timedout
218221
@test readlines(diary_file) == ["# Test: ", "", "a = rand(100)"]
219222

223+
@testset "Non-persistent history" begin
224+
previous_history_lines = readlines(repl_history_file)
225+
226+
open(configuration_file, write=true) do io
227+
configuration = [
228+
"author = \"Test\"",
229+
"date_format = \"\"",
230+
"persistent_history = false"
231+
]
232+
join(io, configuration, "\n")
233+
print(io, "\n")
234+
end
235+
# Simulate user interaction by writing lines to the history file.
236+
open(history_file, read=true, write=true) do io
237+
seekend(io)
238+
join(io, ["# time: ***", "# mode: julia", "\tb = 42"], "\n")
239+
print(io, "\n")
240+
end
241+
# Allow the `watch_task` to update the diary file.
242+
file_event = FileWatching.watch_file(diary_file, 5)
243+
@test file_event.changed || file_event.timedout
244+
@test readlines(diary_file) == ["# Test: ", "", "a = rand(100)", "b = 42"]
245+
# Test that the original history file hasn't changed.
246+
@test readlines(repl_history_file) == previous_history_lines
247+
end
248+
220249
# Clean-up
221250
rm(diary_file)
222251
end

0 commit comments

Comments
 (0)