Introduce XDG base directories built-in support in calc v3#200
Conversation
Use XDG_CONFIG_HOME, XDG_DATA_HOME, and XDG_STATE_HOME to derive default values for CALCRC, CALCPATH, and CALCHISTFILE unless calc-specific environment variables are set. Preserve the legacy compiled defaults as fallback. Ref: lcn2#195
With the new default prioritising $XDG_STATE_HOME/calc/history, in order to have the history mechanism functioning out of the box with calc v3, we need to be able to create that history file. This commit lets calc create that path, so as to restore the original behaviour whereby the expressions history is backed up and restored without any extra set-up from the user.
1cf99da to
0af5e3a
Compare
There was a problem hiding this comment.
Looks good, @ccjmne
I tried pretty hard to adhere to the style of the code base, but my C is very rusty and was never to a professional level in the first place.
You did very well, @ccjmne 👍
We plan to make a mini tweak or 2, as we have an opportunity to "sneek" this into a new calc version 2.17.0.0 as well.
Stay tuned as we tweak your PR (which happens to be a nice round number 200 😉).
lcn2
left a comment
There was a problem hiding this comment.
We will need to update calc.man accordingly too.
Stay tuned ...
|
To test without installing: mkdir -p /tmp/calc-xdg/{config,data,state}
env -u CALCPATH \
-u CALCRC \
-u CALCHISTFILE \
XDG_CONFIG_HOME=/tmp/calc-xdg/config \
XDG_DATA_HOME=/tmp/calc-xdg/data \
XDG_STATE_HOME=/tmp/calc-xdg/state \
LD_LIBRARY_PATH=. \
DYLD_LIBRARY_PATH=. \
CALCHELP=./help \
CALCCUSTOMHELP=./custom
./calc -q 'calcpath()'This PR #200 passes the above test as well as the calc regression test: mkdir -p /tmp/calc-xdg/{config,data,state}
env -u CALCPATH \
-u CALCRC \
-u CALCHISTFILE \
XDG_CONFIG_HOME=/tmp/calc-xdg/config \
XDG_DATA_HOME=/tmp/calc-xdg/data \
XDG_STATE_HOME=/tmp/calc-xdg/state \
LD_LIBRARY_PATH=. \
DYLD_LIBRARY_PATH=. \
CALCHELP=./help \
CALCCUSTOMHELP=./custom
./calc -q 'read regress.cal' |
|
We need to tweak the calc man page, as per GH pullrequestreview-4260884475 before we push a commit onto this nice PR #200. However, it is late, and so we will try this when we are not sleepy 😴 |
f477187 to
a02eaa7
Compare
|
Good catches! I think we missed these two: - .:./cal:${XDG_DATA_HOME}/calc:$XDG_CONFIG_HOME/calc:${CALC_SHAREDIR}:${CUSTOMCALDIR}
+ .:./cal:${XDG_DATA_HOME}/calc:${XDG_CONFIG_HOME}/calc:${CALC_SHAREDIR}:${CUSTOMCALDIR}
- $XDG_STATE_HOME and uses $XDG_STATE_HOME/calc/history
+ $XDG_STATE_HOME and uses ${XDG_STATE_HOME}/calc/history... which may be better surrounded with -- I'm going over Beej's Guide to C Programming and a few other resources, while playing around on my own... and already having my mind blown: I can "instantiate" a sparse And creating X11 windows is that simple? I've got first-class libraries for And people have come up with creative ways to work out the more tricky bits...
I'm not saying I'll soon contribute anything of any interest to the IOCCC, but I'm definitely having a blast taking my "first steps" (for the second time) in C ;-) |
Also update calc.man accordingly. Adjust mention of CALCHISTFILE being used if it's pointing to a file in an absolute *writable* directory (as it was specified in calc.man already.
|
If we're gonna "sneak" that into calc v2, I think we should consider favouring the "legacy" default Well, no data would actually be lost, they could still simply (I'm assuming there's no explicit |
Since we're considering bringing the XDG base directory spec. implementation to calc v2, we should make sure to not have users suddenly lose access to their history only because they've upgraded calc, without crossing the boundary of a new major version. This PR proposes to prefer using a pre-existing calc history file at its (soon to be) "legacy" location, ~/.calc_history, if it exists.
|
Hey @lcn2, I went ahead and implemented the gymnastics I alluded to above, whereby we'd favour preserving a preexisting I think it'll probably make for the smoothest transition: I believe the intersection of users that are interested in organising their configurations according to that spec but have no time to stop and investigate what is going on whenever a tool they use for work isn't behaving like it used to it far from empty. I made sure to document what is going on, so that even the longstanding users that would now choose to adhere to the XDG spec could make sense of what is going on (and know how to "correct course" themselves). I may be way overstepping, however—don't hesitate to push back :-) |
Add, for purposes of debugging, to `make env`, the
'${XDG_CONFIG_HOME}` and `${XDG_DATA_HOME}` variables.
|
Hello @ccjmne , Please download the latest version of calc off of the master branch, which will be calc v2.17.0.0, as this now contains an XDG Base Directory conforming implementation of calc v2. Thanks. |
As discussed in #195, I propose here to adhere to the XDG Base Directory specification if the user has adequately set the corresponding variables.
TL;DR:
In this implementation, the
XDG_XXXenvironment variables are used to derive theCALCPATH,CALCRC, andCALCHISTFILEwhen:Windows
I read up a bit on the topic of support for Windows and it seems that the problem isn't all that easy, considering that
%APPDATA%and other directories seem to be well-established there. I'm frankly quite like a fish out of water when it comes to Windows, and figured I would simply disregard anything XDG on that platform, see 0ffa8aa.We may just choose to not pull in that specific commit: I suppose that if some Windows users have set up all their XDG variables, they likely do want them to be used... I merely am not confident enough with that platform to make any semblance of educated guess.
History file
I took the liberty of automatically creating the directory structure that will point to the history file, so as to preserve the out-of-the-box experience that's well-established with calc, where the history is preserved between sessions; see 0af5e3a.
The crucial point here is that where the previous default
~/.calc_historywould always point to a directory that exists, the user's home; the new$XDG_STATE_HOME/calc/historywould certainly not.This file is the only one that calc needs to create itself and would result in degraded functionality without some extra set-up from the user: I believe that this basis being covered covers all the bases that need covering :-)
Testing
A quick test I used for myself was essentially as follows:
mkdir -p /tmp/calc-xdg/{config,data,state} env -u CALCPATH \ -u CALCRC \ -u CALCHISTFILE \ XDG_CONFIG_HOME=/tmp/calc-xdg/config \ XDG_DATA_HOME=/tmp/calc-xdg/data \ XDG_STATE_HOME=/tmp/calc-xdg/state \ ./calc -q 'calcpath()'I don't recommend that simply running the test be a sufficient validation, I'm only offering some bit that I found was handy for exploration :-)
Disclaimer
I tried pretty hard to adhere to the style of the code base, but my C is very rusty and was never to a professional level in the first place.
I'm truly grateful that I get to contribute, and would have no problem changing anything: don't hesitate to share any concern or preference you may have, I would love to know better.