Skip to content
Browse files

Start rewriting the ls code example, to start showing a more idiosync…

…ratic approach, and demonstrating more pervasive use of Rosella functionality
  • Loading branch information...
1 parent 2c2eec3 commit 7cba405eb0b1d923dfd9df26e7a1d618005885a5 @Whiteknight committed Nov 23, 2011
Showing with 39 additions and 34 deletions.
  1. +30 −34 _includes/ls.winxed
  2. +9 −0 winxed/examples.md
View
64 _includes/ls.winxed
@@ -1,39 +1,35 @@
class Rosella.FileSystem.Directory;
-function show_help(var args) {
- print("Usage: ");
- print(args[0]);
- say(" [directory]");
- exit(0);
+function show_help(var args)
+{
+ Rosella.IO.sayf("Usage: %s [directory]", args[0]);
+ exit(0);
}
-function main[main](var args) {
- var rosella = load_packfile("rosella/core.pbc");
- var(Rosella.initialize_rosella)("filesystem");
- var dirname = ".";
- for(var i = 1; i < args; i++) {
- var arg = args[i];
- if(arg == "-h" || arg == "--help")
- show_help(args);
- else
- dirname = arg;
- }
- var dir = new Rosella.FileSystem.Directory(dirname);
- if(!(dir.exists())) {
- print("ls: cannot access ");
- print(dirname);
- say(": No such file or directory");
- exit(-1);
- }
- var files = dir.get_files();
- for(var file in files) {
- print(file.short_name());
- print(" ");
- }
- var dirs = dir.get_subdirectories();
- for(var curdir in dirs) {
- print(curdir.short_name());
- print(" ");
- }
- say("");
+function ls_main(var args)
+{
+ var dirname = ".";
+ for(var i = 1; i < args; i++) {
+ var arg = args[i];
+ if(arg == "-h" || arg == "--help")
+ show_help(args);
+ else
+ dirname = arg;
+ }
+ var dir = new Rosella.FileSystem.Directory(dirname);
+ if(!(dir.exists())) {
+ Rosella.IO.sayf("ls: cannot access %s: No such file or directory", dirname);
+ exit(-1);
+ }
+ for(var file in dir)
+ Rosella.IO.printf("%s\t", file.short_name());
+
+ say("");
+}
+
+function main[main](var args)
+{
+ var rosella = load_packfile("rosella/core.pbc");
+ var(Rosella.initialize_rosella)("filesystem");
+ var(Rosella.Parrot.try_report)(function() { ls_main(args); });
}
View
9 winxed/examples.md
@@ -8,12 +8,21 @@ using Rosella:
### ls Clone
+The Unix `ls` utility is used to show the contents of a directory. This example
+uses the Rosella FileSystem library and the Directory class to get the
+directory contents:
+
{% highlight javascript linenos %}
{% include ls.winxed %}
{% endhighlight %}
### nl Clone
+The Unix `nl` utility is used to print the contents of a file with line numbers.
+The `-a` and `-t` options change the behavior to print some or all line numbers.
+This example uses the Rosella FileSystem library and the File class to get the
+file contents:
+
{% highlight javascript linenos %}
{% include nl.winxed %}
{% endhighlight %}

0 comments on commit 7cba405

Please sign in to comment.
Something went wrong with that request. Please try again.