-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.R
106 lines (90 loc) · 2.66 KB
/
write.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#' Jot Down a Note
#'
#' @param note data to save. required.
#' @param name character, name of data. required.
#' @param overwrite logical, default is FALSE. If a note of the same name exists, should it be overwritten?
#' @param user name of user. default is `get_jot_user()`.
#' @param time system time. default is `get_sys_time()`.
#' @param pad path to file of pad to write to. default is `jot_active()`.
#'
#' @return path to pad, invisibly
#' @export
#'
#' @concept write
#'
#' @examples
#' tf <- tempfile()
#' jot_new_pad(tf)
#' jot_activate(tf)
#' jot(3, 'example')
jot <- function(note, name, overwrite = FALSE, user = get_jot_user(), time = get_sys_time(), pad = jot_active()) {
if (missing(note)) {
cli::cli_abort('{.arg note} cannot be missing.')
}
if (missing(name)) {
cli::cli_abort('{.arg name} cannot be missing.')
}
if (name %in% c('title', 'locked', 'home')) {
cli::cli_abort('{.arg name} cannot be in {.code c("title", "locked", "home")}.')
}
notes <- yaml::read_yaml(file = pad)
if (jot_is_locked(notes)) {
cli::cli_warn('{.arg pad} is locked. No updates made. Unlock with {.fn jot_unlock}.')
return(invisible(pad))
}
if (!is.null(notes[[name]]) && !overwrite) {
cli::cli_warn('{.arg name} already exists and {.arg overwrite} is {.val FALSE}. No updates were made.')
return(invisible(pad))
}
quoted <- FALSE
if (!is.vector(note)) {
quoted <- TRUE
note <- paste0(utils::capture.output(dput(note)), collapse = '')
}
notes[[name]] <- list(
last_update = time,
user = user,
content = note,
quoted = quoted
)
yaml::write_yaml(x = notes, file = pad)
invisible(pad)
}
#' @rdname jot
#' @export
write_jot <- jot
#' Erase a Note
#'
#' @param name character, name of note to erase. required.
#' @param pad path to file of pad to write to. default is `jot_active()`.
#'
#' @return path to pad, invisibly
#' @export
#'
#' @concept write
#'
#' @examples
#' tf <- tempfile()
#' jot_new_pad(tf)
#' jot(3, 'example')
#' jot_erase('example')
jot_erase <- function(name, pad = jot_active()) {
if (missing(name)) {
cli::cli_abort('{.arg name} cannot be missing.')
}
notes <- yaml::read_yaml(file = pad)
vals <- jot_list(pad = pad)
if (jot_is_locked(notes)) {
cli::cli_warn('{.arg pad} is locked. No updates made. Unlock with {.fn jot_unlock}.')
return(invisible(pad))
}
if (!name %in% vals) {
cli::cli_warn('{.arg name} is not a note in {.arg pad}. No updates made.')
}
if (name %in% c('title', 'locked', 'home')) {
cli::cli_abort('{.arg name} cannot be in {.code c("title", "locked", "home")}.')
}
notes[[name]] <- NULL
yaml::write_yaml(x = notes, file = pad)
invisible(pad)
}