clj-shell.core

This ns contains the core api for clj-shell. It is designed to be used from the repl. Most of the function names mirror the names of unix shell commands (ls, mv!, etc.), but there are also some functions that have no analogous unix command (e.g. paste, up!, etc.).

Note: all ‘path’ arguments can be either string file paths (absolute, or relative to the current working directory) or java.io.File objects. Using ~ for the home directory is allowed.

*cwd

Atom containing the current working directory, used only by functions in this ns. Note that this is completely separate from the current working directory of the application. Avoid swap!-ing or reset!-ing this atom, use cd!, up!, or back! instead.

*cwd-history

Atom containing the history of the *cwd atom, not including the current value. It is recommended not to update this atom, and treat it as read-only.

->file

(->file path)

Coerces the given path to a java.io.File object. If given a file object, will return it unchanged.

back!

(back!)

Goes back to the previous current working directory (if there is one).

cat

Returns a string of the whole contents of the file at the given path.

cd!

(cd! path)

Changes the current working directory to the given path. Note: prefer calling back! or up! instead of passing ‘..’ or ‘-’ to this fn.

clipboard

The system clipboard. See here for available methods.

copy!

(copy! s)

Replaces the data in the clipboard with the given string s.

cp!

(cp! source dest)

Copies a file from source to dest.

exists?

(exists? path)

Returns true if there is a file at the given path, false otherwise.

file-name

(file-name path)

Returns the file name for the file at the given path.

file-path

(file-path path)

Returns the absolute file path for the file at the given path.

file-size

(file-size path)

Returns the size, in bytes, of the file at the given path.

file-type

(file-type path)

Returns the file type of the given file/path.

file?

(file? f)

Returns true if f is a java.io.File object, false otherwise.

filter-tree

(filter-tree predicate t)

Walks the given tree t, presumably returned from the tree fn, and filters out any files not matching the given predicate. Will remove any directories with no children from the returned tree.

find

(find predicate)(find predicate path)

Returns a seq of files under the directory at path (including those in subdirectories) that pass the given predicate. Will use the current working directory (*cwd) as path if no argument is provided.

The ‘matches*’ fns work well for composing the predicate, for example:

(find (matches-exactly "project.clj" file-name))

flatten-tree

(flatten-tree t)

Returns a seq of all files (including directories) in the given tree

head

(head path)(head path n)

Returns a string of the first n lines of the file at the given path. Returns 10 lines if n is not provided.

hidden?

(hidden? path)

Returns true if the file at the given path is hidden (i.e. it is a dotfile), false otherwise.

home-dir

The current user’s home directory

ls

(ls)(ls path)

Lists the files in the directory at the given path. Will use the current working directory (*cwd) if no argument is provided. Returns a seq of java.io.File objects.

matches

(matches value match-value-fn compare)

Returns a predicate which applies match-value-fn to its input and compares it to value, using the compare fn.

matches-exactly

(matches-exactly value match-value-fn)

Returns a predicate that transforms its input using match-value-fn, and returns true iff the result is equal to value.

matches-regex

(matches-regex regex match-value-fn)

Returns a predicate that transforms its input using match-value-fn, and returns true iff regex matches the result.

mv!

(mv! source dest)

Moves a file from source to dest.

paste

(paste)

Returns the data in the clipboard as a string.

print-tree

(print-tree t)(print-tree t opts)(print-tree t {:keys [display-fn], :or {display-fn file-name}, :as opts} indent)

Prints a tree, presumably returned from the tree fn, in a more readable format.

Opts: display-fn - single argument function, taking a java.io.File object, called for each file in the tree. Returns what should be printed for the given file.

pwd

(pwd)

Returns the current working directory, i.e. the value of the *cwd atom.

rm!

(rm! path)

Removes the file at the given path.

tail

(tail path)(tail path n)

Returns a string of the last n lines of the file at the given path. Returns 10 lines if n is not provided.

tree

(tree)(tree path)

Returns a tree of all the files and directories under the given path. The returned tree is in the format [directory '(file1 file2 [subdirectory '(file3)])]. Will not follow symlinks.

up!

(up!)

Moves the current working directory up one level.

walk-tree

(walk-tree f t)

Walks the given tree t, presumably returned from the tree fn, and transforms each file by running it through f.