|
30 | 30 |
|
31 | 31 | <section>
|
32 | 32 | <h1>Josh.PathHandler</h1>
|
| 33 | + |
33 | 34 | <p><code>pathhandler.js</code> is a mix in to easily add the <code>cd</code>, <code>ls</code> and
|
34 |
| - <code>pwd</code> commands as well as path completion. It has <a href="http://underscorejs.org/">Underscore</a> as its dependency for templating, however since all |
35 |
| - templates it uses are exposed, they could easily be replaced with a different function that accepts an argument object returns html.</p> |
| 35 | + <code>pwd</code> commands as well as path completion. It has |
| 36 | + <a href="http://underscorejs.org/">Underscore</a> as its dependency for templating, however since all |
| 37 | + templates it uses are exposed, they could easily be replaced with a different function that accepts an argument object returns html. |
| 38 | + </p> |
36 | 39 |
|
37 | 40 | <p>By implementing the functions <code>getNode</code> and
|
38 | 41 | <code>getChildNodes</code>, this library adds path traversal, discovery and completion just like a bash shell.
|
39 | 42 | </p>
|
40 | 43 |
|
| 44 | + <h2>The Path Node</h2> |
| 45 | + |
| 46 | + <p><code>PathHandler</code> deals with files/directories a path node with at least the following two fields:</p> |
| 47 | + <pre>{ |
| 48 | + name: 'localname', |
| 49 | + path: '/full/path/to/localname' |
| 50 | +}</pre> |
| 51 | + <p>where <code>name</code> is the name of the node and |
| 52 | + <code>path</code> is the absolute path to the node. PathHandler gets path nodes as the callback argument for |
| 53 | + <code>getNode</code> and does not modify it itself, so any additional state required can be attached to the node and be relied on as being part of the node when it is provided to |
| 54 | + <code>getChildNodes</code> or a template.</p> |
| 55 | + |
| 56 | + <h2>The Commands</h2> |
| 57 | + |
| 58 | + <p><code>PathHandler</code> is used to add standard unix directory handling commands and path completion to <code>Josh.Shell</code>.</p> |
| 59 | + |
| 60 | + <h3>pwd</h3> |
| 61 | + |
| 62 | + <p>Prints the current directory, as defined by <code>pathhandler.current</code>.</p> |
| 63 | + |
| 64 | + <h3>ls [path]</h3> |
| 65 | + |
| 66 | + <p>Prints out the listing of all childNodes of the node represented by <code>path</code>. If path is not specified, <code>pathhandler.current</code> is used instead.</p> |
| 67 | + |
| 68 | + <h3>cd [path]</h3> |
| 69 | + |
| 70 | + <p>Changes <code>pathhandler.current</code> to the node found at <code>path</code>, if one could be found. If path is not specified, <code>pathhandler.current</code> is used instead, resulting in a no-op.</p> |
| 71 | + |
| 72 | + <h2>Templates</h2> |
| 73 | + |
| 74 | + <p>Each template is expected to a function that takes a data object and returns html. The default templates are built with |
| 75 | + <code>_.template</code> but any templating system can be used as long as follows the same behavior.</p> |
| 76 | + |
| 77 | + <h3>not_found</h3> |
| 78 | + |
| 79 | + <p>Called when <code>getNode</code> returns null as its callback argument.</p> |
| 80 | + |
| 81 | + <p>Data:</p> |
| 82 | + <ul> |
| 83 | + <li><code>cmd</code> - command that resulted in a miss on path lookup</li> |
| 84 | + <li><code>path</code> - the path that did not match a node</li> |
| 85 | + </ul> |
| 86 | + <h3>ls</h3> |
| 87 | + |
| 88 | + <p>Called to generate output for a successful <code>ls</code> cmd.</p> |
| 89 | + |
| 90 | + <p>Data:</p> |
| 91 | + <ul> |
| 92 | + <li><code>nodes</code> - array of path nodes</li> |
| 93 | + </ul> |
| 94 | + <h3>pwd</h3> |
| 95 | + |
| 96 | + <p>Called to generate output for <code>pwd</code> cmd.</p> |
| 97 | + |
| 98 | + <p>Data:</p> |
| 99 | + <ul> |
| 100 | + <li><code>node</code> - the <code>current</code> node</li> |
| 101 | + </ul> |
| 102 | + <h3>prompt</h3> |
| 103 | + |
| 104 | + <p>Called to generate the new prompt after any cmd attempt (including after completions).</p> |
| 105 | + |
| 106 | + <p>Data:</p> |
| 107 | + <ul> |
| 108 | + <li><code>node</code> - the <code>current</code> node</li> |
| 109 | + </ul> |
| 110 | + <h2>Properties</h2> |
| 111 | + |
| 112 | + <h3>current</h3> |
| 113 | + <p>Contains the current directory. It has to be initialized before activating the shell, since on activation, <code>getPrompt</code> will be called by the shell and requires current to be set to display the prompt. Could be changed manually, but generally is changed as a result of calling <code>cd</code>.</p> |
| 114 | + |
| 115 | + <h3>pathCompletionHandler</h3> |
| 116 | + <p>Contains the path completion handler used by PathHandler. Exposed so that other commands added that work on paths, can use it as their <code>completion</code> handler. It is used for the <code>ls</code> and <code>cd</code> commands. The only assumption it makes about paths is that they are separated by <code>/</code>.</p> |
| 117 | + <p>Path completion is done by first finding the nearest node, i.e. if a trailing slash is found, it calls <code>getNode</code> with the path, otherwise it will call <code>getNode</code> and upon receiving null, will take the subpath to the nearest slash and call <code>getNode</code> again. If a node is found in either scenario, it then calls <code>getChildnodes</code> to get all all children, i.e. the possible completions and in turn call <code>shell.bestMatch</code> with any partial path after the slash and the list of possible child node <code>name</code>s.</p> |
| 118 | + |
| 119 | + <h3>commandAndPathCompletionHandler</h3> |
| 120 | + |
| 121 | + <p>Contains a wrapper around <code>pathCompletionHandler</code> that replaces the default completion handler of the shell, so that a completion event without a known command can determine whether to complete as a command name or a path.</p> |
| 122 | + |
| 123 | + <h3>templates</h3> |
| 124 | + <p>Contains the templates described above.</p> |
| 125 | + |
41 | 126 | <h2>Functions</h2>
|
42 | 127 |
|
43 |
| - <h3>new Josh.PathHandler(config)</h3> |
| 128 | + <h3>new Josh.PathHandler(shell, config)</h3> |
| 129 | + <p>Create a new path handler and attach it to a shell instance.</p> |
| 130 | + |
| 131 | + <h3>getNode(path, callback)</h3> |
| 132 | + <p>This method is called with a <code>path</code> and a <code>callback</code> expecting a path node returned if the path is valid. The default implementation always calls <code>callback</code> with null. This is where custom path resolution logic goes and where path nodes are constructed.</p> |
| 133 | + |
| 134 | + <h3>getChildNodes(node, callback)</h3> |
| 135 | + |
| 136 | + <p>This method is called by the <code>pathCompletionHandler</code> after resolving a path to a node via <code>getNode</code> and expects its callback to be called with an array of pathnodes.</p> |
| 137 | + |
| 138 | + <h3>getPrompt</h3> |
| 139 | + |
| 140 | + <p>This method is called each time the prompt needs to be re-rendered, which in turn calls <code>templates.prompt</code> with <code>pathhandler,current</code>. Could be replaced for custom prompt behavior beyond altering the template.</p> |
44 | 141 |
|
45 | 142 | <h3></h3>
|
46 | 143 |
|
|
0 commit comments