Skip to content

Commit

Permalink
Add f-append-text and f-append-bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
rejeep committed Nov 9, 2014
1 parent e4bccbc commit f38fca8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Or you can just dump `f.el` in your load path somewhere.
* [f-write-bytes](#f-write-bytes-data-path) `(data path)`
* [f-read-text](#f-read-text-path-optional-coding) `(path &optional coding)`
* [f-write-text](#f-write-text-text-coding-path)`(text coding path)`
* [f-append-text](#f-append-text-text-coding-path)`(text coding path)`
* [f-append-bytes](#f-append-data-path)`(text coding path)`

### Destructive

Expand Down Expand Up @@ -315,6 +317,29 @@ Alias: `f-write`
(f-write "Hello world" 'utf-8 "path/to/file.txt")
```

### f-append-text `(text coding path)`

Append TEXT with CODING to PATH.

If PATH does not exist, it is created.

Alias: `f-append`

```lisp
(f-append-text "Hello world" 'utf-8 "path/to/file.txt")
(f-append "Hello world" 'utf-8 "path/to/file.txt")
```

### f-append-bytes `(data path)`

Append binary DATA to PATH.

If PATH does not exist, it is created.

```lisp
(f-append-bytes "path/to/file" (unibyte-string 72 101 108 108 111 32 119 111 114 108 100))
```

### f-mkdir `(&rest dirs)`

Create directories DIRS.
Expand Down
21 changes: 21 additions & 0 deletions README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Or you can just dump `f.el` in your load path somewhere.
* [f-write-bytes](#f-write-bytes-data-path) `(data path)`
* [f-read-text](#f-read-text-path-optional-coding) `(path &optional coding)`
* [f-write-text](#f-write-text-text-coding-path)`(text coding path)`
* [f-append-text](#f-append-text-text-coding-path)`(text coding path)`
* [f-append-bytes](#f-append-data-path)`(text coding path)`

### Destructive

Expand Down Expand Up @@ -297,6 +299,25 @@ Alias: `f-write`
(f-write "Hello world" 'utf-8 "path/to/file.txt")
```

### f-append-text `(text coding path)`

{{f-append-text}}

Alias: `f-append`

```lisp
(f-append-text "Hello world" 'utf-8 "path/to/file.txt")
(f-append "Hello world" 'utf-8 "path/to/file.txt")
```

### f-append-bytes `(data path)`

{{f-append-bytes}}

```lisp
(f-append-bytes "path/to/file" (unibyte-string 72 101 108 108 111 32 119 111 114 108 100))
```

### f-mkdir `(&rest dirs)`

{{f-mkdir}}
Expand Down
17 changes: 17 additions & 0 deletions f.el
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@ DATA is a unibyte string. PATH is a file name to write to."
(set-buffer-multibyte nil)
(insert data)))))

(defalias 'f-append 'f-append-text)
(defun f-append-text (text coding path)
"Append TEXT with CODING to PATH.
If PATH does not exist, it is created."
(f-append-bytes (encode-coding-string text coding) path))

(defun f-append-bytes (data path)
"Append binary DATA to PATH.
If PATH does not exist, it is created."
(let ((content
(if (f-file? path)
(f-read-bytes path)
"")))
(f-write-bytes (concat content data) path)))


;;;; Destructive

Expand Down
35 changes: 35 additions & 0 deletions test/f-io-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@
(f-write-bytes (unibyte-string 226 152 185 32 226 152 186) "foo.txt")
(should (equal (f-read-text "foo.txt") (f-read "foo.txt")))))


;;;; f-append-text

(ert-deftest f-append-text/does-not-exist ()
(with-playground
(f-append-text "foo" 'utf-8 "foo.txt")
(should-exist "foo.txt" "foo")))

(ert-deftest f-append-text/exists ()
(with-playground
(f-write "foo" 'utf-8 "foo.txt")
(f-append-text "bar" 'utf-8 "foo.txt")
(should-exist "foo.txt" "foobar")))

(ert-deftest f-append/alias ()
(with-playground
(f-append "foo" 'utf-8 "foo.txt")
(should-exist "foo.txt" "foo")))


;;;; f-append-bytes

(ert-deftest f-append-bytes-test/does-not-exist ()
(with-playground
(let ((bytes (apply #'unibyte-string (-map #'random (-repeat 100 255)))))
(f-append-bytes bytes "foo.txt")
(should-exist "foo.txt" bytes))))

(ert-deftest f-append-bytes-test/exists ()
(with-playground
(let ((bytes (apply #'unibyte-string (-map #'random (-repeat 100 255)))))
(f-write-bytes bytes "foo.txt")
(f-append-bytes bytes "foo.txt")
(should-exist "foo.txt" (concat bytes bytes)))))

(provide 'f-io-test)

;;; f-io-test.el ends here

0 comments on commit f38fca8

Please sign in to comment.