arclanguage / anarki

Community-managed fork of the Arc dialect of Lisp; for commit privileges submit a pull request.
http://arclanguage.github.io
Other
1.17k stars 160 forks source link

When opening a file, expand user directories. #206

Closed zck closed 3 years ago

zck commented 3 years ago

We use the racket function expand-user-path to expand ~/ to the user's home directory. We call this function for both infiles and outfiles.

This can be tested with (outfile "~/test.txt"). You should see something like this: #<output-port:/home/zck/test.txt>

Infiles only work if the file exists. Here's a way to print out all lines of a file:

(w/infile file "~/test.txt" (whilet line (readline file) (prn line)))

Obviously, both fully qualified names and relative names should continue to work:

arc> (outfile "/tmp/test.txt")
#<output-port:/tmp/test.txt>

arc> (outfile "test.txt")
#<output-port:/home/zck/programs/arc/anarki/test.txt>

I'd like to have this tested on Windows; I've tested it on Linux.

Also, I'd be interested to see if this can be unit tested. I'm not especially sure how to at the moment.

akkartik commented 3 years ago

+1, looks good. Is this a purely aesthetic choice or did you see this changing behavior somewhere?

I haven't tested it on Windows but it should be fine based on the Racket docs. (search for "cleanse")

zck commented 3 years ago

It's just an aesthetic choice, one to work better with the general expectations of the underlying OS.

Basically when I use Anarki and want to open a file, I always forget ~/ doesn't work. So now it does.

akkartik commented 3 years ago

Oh so it doesn't actually work? 👍

zck commented 3 years ago

Haha, it did not work before this PR, no. Previously, you'd see something like this:

arc> (outfile "~/test.txt")
open-output-file: error opening file
  path: /home/zck/programs/arc/anarki/~/test.txt
  system error: No such file or directory; errno=2
  context...:
   body of "/home/zck/programs/arc/anarki/ac.rkt"
   /home/zck/programs/arc/anarki/ac.rkt:1456:4

With this PR:

arc> (outfile "~/test.txt")
#<output-port:/home/zck/test.txt>