Open zkwinkle opened 1 year ago
Sorry, I haven't had time to properly review this. I will probably implement this later using a $RANDOM
generated once and stored in $XDG_CONFIG_HOME
, which will be used as the ${ID}
in preloadLink, or use something simpler as a consistent ID
That's okay, but I urge you to reconsider my method because it's self contained, and doesn't create any unnecessary files in the system. Right now, discocss
is POSIX shell compliant, making it basically as portable as a shell script can be, but the $RANDOM
magic variable isn't POSIX shell.
Summary
This PR addressed #20.
The method to generate the random ID's is to transform the $USER variable into a numerical seed and then use awk
srand(seed)
andrand()
to generate the 7 character long ID.The solution is a little bit over-engineered I think, but it works perfectly!
I will explain my reasoning but feel free to change anything.
Rationale
I put the procedure inside a
gen_id()
function only for the reason that I thought it was orderly.Didn't want to add any dependencies so that's why I went with the current approach. The hashing commands I found depnded on Perl, and even the
$RANDOM
variable apparently wasn't POSIX compliant. That's why I ended up usingawk
for the RNG.The reason the string is decoded into ASCII values is because the seed must be numerical, if you try to pass a string rand() will always return the same values.
The
SEED=$((SEED % 4294967295))
is there becausesrand()
takes in anunsigned int
and 4294967295 is the max value of one.The while loop where I'm checking
while [ ${#ID} -lt $WIDTH ]
is there because even if I multipliedrand()
by a large enough number, I think there's always a chance it returns really low numbers so this while loop guarantees that ID will reach at least 7 characters long. Could've also added padding to the string but I think to keep generating more random numbers generates more unique numbers than adding padding does?The reason I multiply
rand()*32768
in specific is simply because that's what they multiply it by in the shellcheck example for RNG. I think it could be literally any number and it wouldn't matter anyways.Shellcheck
I made sure the commit didn't add any shellcheck warnings or anything, this is my
shellcheck
output: