Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env variables and tdorc config options #10

Merged
merged 9 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,33 @@ If you use Neovim, I highly recommend using [tdo.nvim](https://github.com/2kabhi
   └── note.md
   └── todo.md
```
## Configuration

You can further configure `tdo` by either defining environment variables or via a `tdorc` file.

### Using environment variables

You can set the following (optional) environment variables:

- `TIMESTAMP_ENTRY` (`["true"|"false"]` defaults to `"true"`): Whether to add a time stamp when using `tdo entry` or `tdo e`.
- `TIMESTAMP_NEWNOTE` (`["true"|"false"]` defaults to `"false"`): Whether to add a time stamp when creating new notes with `tdo <note_title>`.
- `FILE_NAME_AS_TITLE` (`["true"|"false"]` defaults to `"false"`): Whether to add the file name as title when creating new notes with `tdo <note_title>`. If `"true"`, then it adds `<note_title>` as a markdown title to the end of the new note.
- `ENTRY_TIMESTAMP` (`[string]` defaults to `"## %a, %I:%M %p"`): can be any bash string such as a date format expression. It is ignored if `TIMESTAMP_ENTRY` is set to `"false"`.
2KAbhishek marked this conversation as resolved.
Show resolved Hide resolved
- `NOTE_TIMESTAMP`(`[string]` defaults to `"## %a. %b %d, %Y - %I:%M%p"`): can be any bash string such as a date format expression. It is ignored if `TIMESTAMP_NEWNOTE` is set to `"false"`.

### Using a `tdorc` file

Alternatively, it is possible to define the same variables in a `$HOME/.config/tdorc` file following bash syntax. For example:

```bash
TIMESTAMP_ENTRY=false
TIMESTAMP_NEWNOTE=true
FILE_NAME_AS_TITLE=true
ENTRY_TIMESTAMP="## %I:%M %p"
NOTE_TIMESTAMP="## Created: %a. %b %d, %Y at %I:%M%p"
2KAbhishek marked this conversation as resolved.
Show resolved Hide resolved
```

**Note** that if any of these variables are set as environment variables, then the `tdorc` will be ignored completely. That is, it is not possible to use both environment variables configuration together with a `tdorc` configuration.

## 🏗️ What's Next

Expand Down
49 changes: 46 additions & 3 deletions tdo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@ For more information, visit https://github.com/2kabhishek/tdo
EOF
}

config_setup() {
# List of variable names to check
local env_vars=("TIMESTAMP_ENTRY" "TIMESTAMP_NEWNOTE" "ENTRY_TIMESTAMP" "NOTE_TIMESTAMP" "FILE_NAME_AS_TITLE")

# Check if any of the variables are set as environment variables
local env_variables_set=false
for var in "${env_vars[@]}"; do
if [ ! -z "${!var}" ]; then
env_variables_set=true
break
fi
done

# Function to source the configuration file if it exists
source_config_file() {
local config_file="$HOME/.config/tdorc"
if [ -f "$config_file" ]; then
source "$config_file"
fi
}

# If none of the variables are set as environment variables, source the config file
if [ "$env_variables_set" = false ]; then
source_config_file
fi
}

# validate whether a variable is set to true or false, and set to default otherwise
validate_and_set() {
local default_val="${2:-true}"
local var_value="${1:-$default_val}"
if [[ "$var_value" != "true" && "$var_value" != "false" ]]; then
var_value=$default_val
fi
echo "$var_value"
}

2KAbhishek marked this conversation as resolved.
Show resolved Hide resolved
check_command() {
if ! command -v "$1" &>/dev/null; then
echo "Error: The $1 command is not available. Make sure it is installed."
Expand Down Expand Up @@ -96,9 +133,9 @@ create_file() {

add_timestamp() {
file_path="$1"
time_format="${2:-%a, %I:%M %p}"
time_format="${2:-## %a, %I:%M %p}"
timestamp=$(date +"$time_format")
echo -e "\n## $timestamp\n" >>"$file_path"
echo -e "\n$timestamp\n" >>"$file_path"
}

write_file() {
Expand Down Expand Up @@ -151,7 +188,12 @@ new_note() {
root="$NOTES_DIR"
note_file="$root/notes/$1.md"
template="$root/templates/note.md"
[ ! -f "$note_file" ] && new_file="true" || new_file="false"
create_file "$note_file" "$template"
if [ "$new_file" = "true" ]; then
[ "$(validate_and_set "${FILE_NAME_AS_TITLE}" false)" = "true" ] && echo -e "# $1" >>"$note_file"
[ "$(validate_and_set "${TIMESTAMP_NEWNOTE}" false)" = "true" ] && add_timestamp "$note_file" "${NOTE_TIMESTAMP:-"## %a. %b %d, %Y - %I:%M%p"}"
fi
write_file "$note_file" "$root"
}

Expand All @@ -173,14 +215,15 @@ new_entry() {
entry_file="$root/entries/$(generate_file_path "$1")"
template="$root/templates/entry.md"
create_file "$entry_file" "$template"
add_timestamp "$entry_file"
[ "$(validate_and_set "${TIMESTAMP_ENTRY}")" = "true" ] && add_timestamp "$entry_file" "${ENTRY_TIMESTAMP:-}"
write_file "$entry_file" "$root"
}

main() {
check_command "rg"
check_command "fzf"
check_env "NOTES_DIR"
config_setup

case "$1" in
-c | --commit | c | commit) commit_changes "$(dirname "$2")" ;;
Expand Down