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

Conversation

idr4n
Copy link
Contributor

@idr4n idr4n commented Apr 3, 2024

What

What does this pull request accomplish?

  • Add env variables for several config options.
  • Add tdorc config file option instead of using env variables.

Note that only one option can be used by the user. If one or more env variables are set, then the tdorc file is ignored.

How

What code changes were made to accomplish it?

  • Add a config_setup function to setup the config variables.
  • Modify the new_note and new_entry functions to allow for additional config options.

Why

Additional Notes

  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.

@2KAbhishek
Copy link
Owner

Can you please also add a section under usage in the README for this @idr4n

@idr4n
Copy link
Contributor Author

idr4n commented Apr 4, 2024

Can you please also add a section under usage in the README for this @idr4n

Sure... done! @2KAbhishek

tdo.sh Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@2KAbhishek
Copy link
Owner

@idr4n thanks for all the efforts on this, I left some suggestions

I can do another round of review once you are done,

since we are changing a lot of the code, we'll have to confirm that all the functions + tdo.nvim integrations work as expected with these changes

lmk if you need any help

@idr4n
Copy link
Contributor Author

idr4n commented Apr 5, 2024

@2KAbhishek Thanks for the comments. I will tackle them soon! However, before that, I was thinking whether we should use a different approach for templates, as it would modify to some extend the logic of this PR when creating new entries and new notes.

Basically, I was thinking that it would be better to parse the template rather than just cp, so templates can have, say three general variables, {{TITLE}}, {{NOTE_TIMESTAMP}} {{ENTRY_TIMESTAMP}}, the first one being replaced by the name of the file (without .md), and the other two being replaced by NOTE_TIMESTAMP_FORMAT and ENTRY_TIMESTAMP_FORMAT env variables (or from tdorc config), respectively. In that way, we would change some of the logic of add_timestamp used in new_entry and new_note.

Now, to do the parsing, I suggest two options:

  • Using sed. For example sed -e "s/{{TITLE}}/$title/g" -e "s/{{ENTRY_TIMESTAMP}}/$ENTRY_TIMESTAMP_FORMAT/g" $template_path > $note_file
  • Or without using sed, something like this:
    # Read the template file
    template=$(<$template_path)
    
    # Replace placeholders with actual values
    output="${template//\{\{TITLE\}\}/$title}"
    output="${output//\{\{ENTRY_TIMESTAMP\}\}/$ENTRY_TIMESTAMP_FORMAT}"
      output="${output//\{\{NOTE_TIMESTAMP\}\}/$NOTE_TIMESTAMP_FORMAT}"
    
    # Output to a new file
    echo "$output" > $note_file

Let me know if you prefer this suggestion so I would implement it in this PR, or if we go as initially proposed here with your comments above.

since we are changing a lot of the code, we'll have to confirm that all the functions + tdo.nvim integrations work as expected with these changes

Yes, definitely. I am using tdo.nvim now so I can test any change there as well ;)... The current PR behaves well with tdo.nvim as fas as I can tell, but will test it further. Speaking of which, what do you think of implementing some unit testing after this PR? Haven't done so in bash but would be helpful and interesting to do so, I think.

@2KAbhishek
Copy link
Owner

The template parsing idea seems really interesting, but to limit the scope of the changes on this PR, let's stick to configs only for now

Unit tests will be nice, I have something in my mind for that, will open up a PR once I get some time

@idr4n
Copy link
Contributor Author

idr4n commented Apr 5, 2024

The template parsing idea seems really interesting, but to limit the scope of the changes on this PR, let's stick to configs only for now

Sound good. Will be working on the comments soon then.

Unit tests will be nice, I have something in my mind for that, will open up a PR once I get some time

Great, look forward to it!

@idr4n
Copy link
Contributor Author

idr4n commented Apr 6, 2024

@2KAbhishek I have addressed the comments above. Hopefully there are not errors. I tested it both in interactive mode and in tdo.nvim.

tdo.sh Outdated Show resolved Hide resolved
tdo.sh Outdated Show resolved Hide resolved
tdo.sh Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@2KAbhishek
Copy link
Owner

Thanks for the continued effort on this @idr4n, I left some final suggestions

@2KAbhishek
Copy link
Owner

Do the config values behave the same way when used non interactively, e.g with tdo.nvim?

@idr4n
Copy link
Contributor Author

idr4n commented Apr 7, 2024

to my understanding, technically [ $add_entry_timestamp = "true" ] and [ $add_entry_timestamp = true ] are the same, just trying to be consistent throughout. However, since $add_entry_timestamp is potentially set by the user, not sure what could be the behavior if set to some other string or command by the user. So just to avoid undesirable behavior, I explicitly compare against true, specially since you are suggesting to remove the validate_and_set function above. Also it seems to me more idiomatic or expressive. But I let you decide on these aspects. I will follow your suggestions nonetheless.

Do the config values behave the same way when used non interactively, e.g with tdo.nvim?

Yes, In the current code, the config values behave the same when used non interactively (that is, with tdo.nvim).

@idr4n
Copy link
Contributor Author

idr4n commented Apr 7, 2024

You are suggesting:

    create_file "$note_file" "$template"
    if [ ! -f "$note_file" ]; then
      $filename_as_title && echo -e "# $1" >>"$note_file"
      $add_new_note_timestamp && add_timestamp "$note_file" "$note_timestamp_format"
    fi

however, that condition will never be met as it runs after create_file, therefore note_file always exists. Also, the reason for [ ! -f "$note_file" ] && new_file="true" || new_file="false" is to detect if the file didn't exists before running create_file, and so if it is a new file then decide whether to add timestamps on new files only (otherwise adding stamps everytime I open the same note doesn't make much sense to me). Let me know what you think.

@2KAbhishek
Copy link
Owner

@idr4n just edited the suggestion, lmk what you think

@idr4n
Copy link
Contributor Author

idr4n commented Apr 7, 2024

@2KAbhishek yes, the suggestion looks good. I have pushed the new suggested changes.

tdo.sh Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@2KAbhishek
Copy link
Owner

@idr4n the changes look good, I did some cleanup, the only major change:

ADD_NEWNOTE_TIMESTAMP has been renamed to ADD_NEW_NOTE_TIMESTAMP

@2KAbhishek 2KAbhishek merged commit abf64d0 into 2KAbhishek:main Apr 7, 2024
@idr4n
Copy link
Contributor Author

idr4n commented Apr 7, 2024

@idr4n the changes look good, I did some cleanup, the only major change:

ADD_NEWNOTE_TIMESTAMP has been renamed to ADD_NEW_NOTE_TIMESTAMP

@2KAbhishek Awesome! Thanks a lot.

@idr4n idr4n deleted the add-todorc branch April 7, 2024 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants