Selaa lähdekoodia

added 'more/less' and 'wc'

Pat Beirne 3 vuotta sitten
vanhempi
commit
79adae017f
3 muutettua tiedostoa jossa 83 lisäystä ja 42 poistoa
  1. 5 3
      README.md
  2. 2 2
      tf.py
  3. 76 37
      tf_extend.py

+ 5 - 3
README.md

@@ -239,8 +239,7 @@ sed   <pattern> <filename>
 help
 ```
 
-You can also use `copy`, `move`, `del`, `list` and `ls` as synonyms for
- `cp`, `mv`, `rm`, `cat` and `dir` .  The `mv` can rename directories. 
+You can use synonyms for these: `copy = cp`, `move = rename = mv`, `del = rm`, `list = cat`, `ls = dir`. The `mv/move/rename` can rename directories. 
 
 For the `cat/list` command, you can enable line numbers with `-n` and you can limit the display range with `-l n-m` where `n` and `m` are decimal numbers (and n should be less than m). These are all valid uses of `cat`
 
@@ -378,11 +377,14 @@ host <domain.name>        # do an DNS lookup
 freq [160 | 80]           # get/set the ESP8266 frequency
 exec <python-filename>    # execute a small python file
 free                      # display the heap size: used + free
+wc <filename>             # display the line count, word count and bytes
+less <filename>           # similar to cat, but displays 30 lines at a time
 ```
+Synonyms: `ip = ifconfig`, `more = less`, `dig = nslookup = host`
 
 The `tf.py` module checks to see if the `tf_extend.py` files exists, and forwards unknown commands to it. The `help` system also extends when the extension file exists.
 
-Installing the extensions module uses about 2k of flash/disk space and 2kB of heap ram.
+Installing the extensions module uses about 3k of flash/disk space and 4kB of heap ram.
 
 ## Performance
 

+ 2 - 2
tf.py

@@ -126,7 +126,7 @@ if 'tf_extend.py' in os.listdir():
 def _help():
   print("==Simple shell v1.2 for Text Files")
   print("  cp/copy <src-file> <dest-file>")
-  print("  mv/move <src-file> <dest-file>    \t\trm/del <file>")
+  print("  mv/move/rename <src-file> <dest-file>  \trm/del <file>")
   print("  cd [<folder>]\t\tmkdir <folder>\t\trmdir <folder>")
   print("  dir/ls [<folder>]")
   print("  cat/list [-n] [-l <n>,<m>] <file>")
@@ -208,7 +208,7 @@ def main():
           os.mkdir(rp[1])
         elif op=='rmdir':
           os.rmdir(rp[1])
-        elif op in('mv','move'):
+        elif op in('mv','move','rename'):
           os.rename(rp[1],rp[2])
         elif op in('rm','del'):
           os.remove(rp[1])

+ 76 - 37
tf_extend.py

@@ -1,26 +1,70 @@
-import os,network,socket,time,machine,gc
+import os,sys,network,socket,time,machine,gc,tf
+
+# these helper classes let us use the tf.transfer() iterator,
+# by intercepting the .write()
+class wc():
+  def __init__(self):
+    self.words=self.lines=self.bytes_=0
+  def write(self,text):
+    self.bytes_ += len(text)
+    self.lines += 1
+    self.words += len(text.split())
+
+class lessor():
+  def __init__(self):
+    self.i=0
+  def write(self,text):
+    if self.i==-1: return
+    self.i += 1
+    sys.stdout.write(text)
+    if self.i%30==0:
+      sys.stdout.write("====> press <enter> to see more, N or Q to quit <====\n")
+      g=sys.stdin.read(1)
+      if g=='\n': g=sys.stdin.read(1)
+      if g in "nNqQ":
+        self.i=-1
+
+# the main entry point for the extension
+# returns True if the command was interpreted
 def cmd(args):
-  if args[0]=='ifconfig':
+  cmd=args[0]
+  if cmd in ('wc','more','less','exec') and len(args)<2:
+    print("syntax: "+cmd+" <filename>")
+    return True
+
+  if cmd in ('wc','more','less'):
+    if cmd=='wc':
+      w = wc()
+    else:
+      w = lessor()
+    try:
+      tf.transfer(args[1],w)
+      if cmd=='wc':
+        print("lines: {}\twords: {}\tbytes: {}".format(w.lines, w.words, w.bytes_))
+    except:
+      print("file not found: "+args[1])
+
+  elif cmd in ('ifconfig','ip'):
     ifc=network.WLAN().ifconfig()
     print("IP: {}\tmask: {}\tgateway: {}\tDNS: {}".format(*ifc))
-    return True
-  elif args[0]=='host':
+
+  elif cmd in ('host','nslookup','dig'):
     if len(args)<2:
       print("syntax: host <domain.name>")
-      return False
-    print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
-    return True
-  elif args[0]=='connect':
+    else:
+      print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
+
+  elif cmd=='connect':
     if len(args)<3:
       print("syntax: connect <ssid> <password>")
-      return False
-    w=network.WLAN(network.STA_IF)
-    w.connect(args[1],args[2])
-    print("connecting...",end=' ')
-    time.sleep(3)
-    print(w.ifconfig() if w.isconnected() else "not yet connected; try 'ifconfig' in a few seconds")
-    return True
-  elif args[0]=='scan':
+    else:
+      w=network.WLAN(network.STA_IF)
+      w.connect(args[1],args[2])
+      print("connecting...",end=' ')
+      time.sleep(3)
+      print(w.ifconfig() if w.isconnected() else "not yet connected; try 'ifconfig' in a few seconds")
+
+  elif cmd=='scan':
     w=network.WLAN(network.STA_IF)
     print("scanning...")
     s=w.scan()
@@ -29,34 +73,29 @@ def cmd(args):
       return True
     for i in s:
       print("ch: {}\tRSSI: {}\t{}\tSSID: {}".format(i[2],i[3],"open" if i[4]==0 else "",i[0]))
-    return True
-  elif args[0]=='freq':
+
+  elif cmd=='freq':
     if len(args)==1 or args[1] in ("160","80"):
       if len(args)>1:
         machine.freq(int(args[1])*1000000)
       print("master cpu frequency {}MHz".format(machine.freq()//1000000))
     else:
       print("syntax: freq [ 160 | 80 ]")
-    return True
-  elif args[0]=='exec':
-    if len(args)<2:
-      print("syntax: exec <python-filename>")
-    else:
-      try:
-        exec(open(args[1]).read(),globals(),globals())
-      except OSError:
-        print("file not found")
-    return True
-  elif args[0]=='free':
+
+  elif cmd=='exec':
+    try:
+      exec(open(args[1]).read(),globals(),globals())
+    except OSError:
+      print("file not found")
+  elif cmd=='free':
     print("memory used: {}\tmemory free:{}".format(gc.mem_alloc(),gc.mem_free()))
-    return True
-  elif args[0]=='help':
+  elif cmd=='help':
     print("==Extended commands")
     print("  connect <essid> <password> \tscan")
-    print("  ifconfig                   \thost <domain.name>")
-    print("  freq [ 160 | 80 ]          \texec <python-filename>")
-    print("  free")
-    return True
-  else:
+    print("  ifconfig/ip        \t\thost/dig/nslookup <domain.name>")
+    print("  freq [ 160 | 80 ]  \t\texec <python-filename>")
+    print("  free        \t\t\twc <filename>")
+    print("  less/more <filename>")
+  else: # command not found
     return False
-
+  return True