-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_prompt.R
92 lines (72 loc) · 2.08 KB
/
github_prompt.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
# ============================================================================ #
#
# ---- At GitHub Info to Prompt ----
#
# ============================================================================ #
# Add GitHub branch name, if you need to commit a file and if you need to
# push commits.
#
# This would he put in a your computuer `.Rprofile`. You can access this file by
# usethis::edit_r_profile(scope = "user")
#
# You can also save this to another `.Rprofile` file and source it in the
# project level `.Rprofile` like `source("C:/<user>/<dir 1>/<dir 2>/.Rprofile")`
.First <- function() {
options(continue = " ")
chg_prompt <- function(...) {
# get project path
proj_path <- here::here()
# get git branch
git_branch <-
suppressWarnings(
system(
"git rev-parse --abbrev-ref HEAD",
ignore.stderr = TRUE,
intern = TRUE
)
)
git_msg <- ""
# if branch exists
if (length(git_branch) != 0) {
# initialize message
git_msg <- paste0(" ", proj_path, " @", git_branch)
# extract status
git_status <-
suppressWarnings(
system(
"git status -sb",
ignore.stderr = TRUE,
intern = TRUE
)
)
# check if ahead
if (any(grepl("ahead", git_status))) {
git_msg <- paste(git_msg, "⬆︎")
}
# check if modified files
if (any(grepl("^ M", git_status[-1]))) {
git_msg <- paste(git_msg, "︎M")
}
# check if untracked
if (any(grepl("^??", git_status[-1]))) {
git_msg <- paste(git_msg, "✘")
}
# check other
if (any(!grepl("ahead|^ M|^??", git_status[-1]))) {
git_msg <- paste(git_msg, "O")
}
}
console_msg <-
paste0(
"[",
format(Sys.time(), "%H:%M"),
git_msg,
"] > "
)
options(prompt = console_msg)
invisible(TRUE)
# ---- end of function chg_prompt
}
chg_prompt()
addTaskCallback(chg_prompt)
}