Recent Posts
None | 39 sec ago
C++ | 53 sec ago
C | 54 sec ago
None | 1 min ago
None | 1 min ago
Lua | 1 min ago
None | 1 min ago
mIRC | 1 min ago
None | 1 min ago
C++ | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 10th of Feb 2010 12:05:02 AM Download | Raw | Embed | Report
  1. #! /Library/Frameworks/Python.framework/Versions/2.6/bin/python
  2. import curses
  3. import sys
  4. import os
  5. import time
  6. from curses import panel
  7. from curses.wrapper import wrapper
  8.  
  9. class Container:
  10.         pass
  11.        
  12. def drawbottom(mainwind, width, height, status):
  13.         mainwind.move(height-2, 1)
  14.         mainwind.clrtoeol()
  15.         mainwind.addch(height-2, width-1, curses.ACS_VLINE)
  16.         mainwind.move(height-2, 1)
  17.         mainwind.addstr(status)
  18.         mainwind.refresh()
  19.  
  20. def setupdir(mainwind, infowind, width, height, currdir):
  21.         global d
  22.         mainwind.move(1, 1)
  23.         mainwind.clrtoeol()
  24.         mainwind.addch(1, width-1, curses.ACS_VLINE)
  25.         mainwind.move(1, 1)
  26.         mainwind.addstr("Path: " + currdir)
  27.         mainwind.touchwin()
  28.         mainwind.noutrefresh()
  29.         d.l = os.listdir(currdir) #list of the current dir
  30.         if len(d.l) == 0:
  31.                 d.l.append("<EMPTY>")
  32.         d.i = {} #file info
  33.         d.p = curses.newpad(len(d.l), width-3)
  34.         d.selected = 0
  35.         d.oldselected = 0
  36.         d.scroll = 0
  37.         d.bar = 0
  38.         #d.oldbar = 0
  39.         idx = 0
  40.         for x in d.l[:]:
  41.                 if x[0] == "." and d.invisible:
  42.                         d.l.remove(x)
  43.                         continue
  44.                 d.p.move(idx, 0)
  45.                 if os.path.isdir(x):
  46.                         d.i[x] = 1
  47.                         d.p.addstr(x, curses.color_pair(1))
  48.                 else:
  49.                         d.i[x] = 0
  50.                         d.p.addstr(x)
  51.                 idx = idx + 1
  52.         length = len(d.l)
  53.         if length == 0:
  54.                 d.l.append("<EMPTY>")
  55.                 d.i["<EMPTY>"] = 0
  56.         if d.l[0] == "<EMPTY>" and len(d.l) == 1:
  57.                 length = 0
  58.         s = str(length) + " file"
  59.         if length != 1:
  60.                 s = s + "s"
  61.         mainwind.addstr(1, width-2-len(s), s)
  62.         mainwind.addch(3, width-2, curses.ACS_UARROW)
  63.         mainwind.addch(height-4, width-2, curses.ACS_DARROW)
  64.         for x in range(4, height-4):
  65.                 mainwind.addch(x, width-2, curses.ACS_VLINE)
  66.         drawdir(mainwind, infowind, width, height)
  67.  
  68. def drawdir(mainwind, infowind, width, height):
  69.         global d
  70.         d.p.move(d.oldselected, 0)
  71.         if d.i[d.l[d.oldselected]] == 1:
  72.                 d.p.addstr(d.l[d.oldselected], curses.color_pair(1))
  73.         else:
  74.                 d.p.addstr(d.l[d.oldselected])
  75.         d.p.move(d.selected, 0)
  76.         d.p.addstr(d.l[d.selected], curses.A_REVERSE)
  77.         d.p.noutrefresh(d.scroll, 0, 3, 1, height-4, width-2)
  78.         mainwind.addch(d.bar+4, width-2, curses.ACS_VLINE)
  79.         try:
  80.                 d.bar = int((d.selected*1.0/(len(d.l)-1))*(height-9))
  81.         except:
  82.                 d.bar = 0
  83.         #raise Exception(d.bar, d.selected, height-8, len(d.l)-1, (d.selected*1.0/(len(d.l)-1)))
  84.         mainwind.addch(d.bar+4, width-2, " ", curses.A_REVERSE)
  85.         mainwind.noutrefresh()
  86.         if d.info:
  87.                 infowind.erase()
  88.                 infowind.box()
  89.                 infowind.hline(2, 0, curses.ACS_HLINE, 50)
  90.                 infowind.addch(2, 0, curses.ACS_LTEE)
  91.                 infowind.addch(2, 49, curses.ACS_RTEE)
  92.                 fname = d.l[d.selected]
  93.                 if len(fname) > 42:
  94.                         fname = fname[:-(len(fname)-42+3)] + "..."
  95.                 infowind.addstr(1, 1, "File: " + fname)
  96.                 statcall = os.stat(d.l[d.selected])
  97.                 size = statcall.st_size
  98.                 suffixes = ["B", "KB", "MB", "GB", "TB"]
  99.                 for suffix in suffixes:
  100.                         if size > 1024:
  101.                                 size /= 1024
  102.                         else:
  103.                                 break
  104.                 infowind.addstr(3, 1, "File size: " + str(size) + suffix + " (" + str(statcall.st_size) + " bytes)")
  105.                 permslist = ["x", "w", "r"]
  106.                 tlist = range(9)
  107.                 tlist.reverse()
  108.                 if os.path.isdir(d.l[d.selected]):
  109.                         permstr = "d"
  110.                 else:
  111.                         permstr = "-"
  112.                 for num in tlist:
  113.                         if statcall.st_mode & (1 << num) > 0:
  114.                                 permstr = permstr + permslist[num%3]
  115.                         else:
  116.                                 permstr = permstr + "-"
  117.                 infowind.addstr(4, 1, "Permissions: " + permstr)
  118.                 infowind.addstr(5, 1, time.strftime("Last modification: %c", time.localtime(statcall.st_mtime)))
  119.                 infowind.noutrefresh()
  120.         curses.doupdate()
  121.  
  122. def main(mw):
  123.         global d
  124.         mainwind = mw
  125.         mainwind.addstr("Initializing...")
  126.         mainwind.refresh()
  127.         curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
  128.         mainwind.erase()
  129.         height, width = mainwind.getmaxyx()
  130.         if height < 24 or width < 110:
  131.                 return 1
  132.         currdir = os.getcwd()
  133.         #set up pretty view
  134.         mainwind.box()
  135.         mainwind.hline(2, 0, curses.ACS_HLINE, width)
  136.         mainwind.addch(2, 0, curses.ACS_LTEE)
  137.         mainwind.addch(2, width-1, curses.ACS_RTEE)
  138.         mainwind.hline(height-3, 0, curses.ACS_HLINE, width)
  139.         mainwind.addch(height-3, 0, curses.ACS_LTEE)
  140.         mainwind.addch(height-3, width-1, curses.ACS_RTEE)
  141.         mainwind.move(height-2, 1)
  142.         mainwind.addstr("Commands: q: quit, c: chdir, u: up, s: show/hide info window, i: show/hide invisible files, d: delete file")
  143.         infowind = curses.newwin(10, 50, 5, width-54)
  144.         d.info = False
  145.         d.invisible = True
  146.         setupdir(mainwind, infowind, width, height, currdir)
  147.         dirty = False
  148.         while True:
  149.                 mainwind.move(0, 0)
  150.                 c = mainwind.getch()
  151.                 if dirty == True:
  152.                         drawbottom(mainwind, width, height, "Commands: q: quit, c: chdir, u: up, s: show/hide info window, i: show/hide invisible files, d: delete file")
  153.                         dirty = False
  154.                 if c >= 0 and c < 256:
  155.                         c = chr(c)
  156.                 if c == "q":
  157.                         return
  158.                 elif c == curses.KEY_UP:
  159.                         if d.selected > 0:
  160.                                 d.oldselected = d.selected
  161.                                 d.selected = d.selected - 1
  162.                                 if d.selected < d.scroll:
  163.                                         d.scroll -= (height-6)/2
  164.                                 drawdir(mainwind, infowind, width, height)
  165.                 elif c == curses.KEY_DOWN:
  166.                         if d.selected < len(d.l)-1:
  167.                                 d.oldselected = d.selected
  168.                                 d.selected = d.selected + 1
  169.                                 if d.selected > d.scroll + height - 6:
  170.                                         d.scroll += (height-6)/2
  171.                                 if d.scroll > len(d.l)-height+6:
  172.                                         d.scroll = len(d.l)-height+6
  173.                                 drawdir(mainwind, infowind, width, height)
  174.                 elif c == "u":
  175.                         try:
  176.                                 os.chdir("..")
  177.                         except OSError as e:
  178.                                 drawbottom(mainwind, width, height, "CHDir error: " + e[1] + " (" + str(e[0]) + ")")
  179.                                 dirty = True
  180.                                 continue
  181.                         currdir = os.getcwd()
  182.                         setupdir(mainwind, infowind, width, height, currdir)
  183.                 elif c == "c":
  184.                         if d.i[d.l[d.selected]] != 1:
  185.                                 drawbottom(mainwind, width, height, "Error: Not a directory!")
  186.                                 dirty = True
  187.                         else:
  188.                                 try:
  189.                                         os.chdir(d.l[d.selected])
  190.                                 except OSError as e:
  191.                                         drawbottom(mainwind, width, height, "CHDir error: " + e[1] + " (" + str(e[0]) + ")")
  192.                                         dirty = True
  193.                                         continue
  194.                                 currdir = os.getcwd()
  195.                                 setupdir(mainwind, infowind, width, height, currdir)
  196.                 elif c == "s":
  197.                         if d.info == True:
  198.                                 d.info = False
  199.                                 mainwind.touchwin()
  200.                                 mainwind.refresh()
  201.                                 drawdir(mainwind, infowind, width, height)
  202.                         else:
  203.                                 d.info = True
  204.                                 drawdir(mainwind, infowind, width, height)
  205.                 elif c == "i":
  206.                         d.invisible = not d.invisible
  207.                         setupdir(mainwind, infowind, width, height, currdir)
  208.                 elif c == "d":
  209.                         dirty = True
  210.                         if d.i[d.l[d.selected]] != 0:
  211.                                 drawbottom(mainwind, width, height, "Error: Not a file!")
  212.                                 continue
  213.                         drawbottom(mainwind, width, height, "Are you sure? ")
  214.                         yn = mainwind.getch()
  215.                         if yn != ord("y"):
  216.                                 drawbottom(mainwind, width, height, "File deletion aborted!")
  217.                                 continue
  218.                         try:
  219.                                 os.unlink(d.l[d.selected])
  220.                                 setupdir(mainwind, infowind, width, height, currdir)
  221.                                 drawbottom(mainwind, width, height, "File deleted successfully!")
  222.                         except OSError as e:
  223.                                 drawbottom(mainwind, width, height, "Deletion error: " + e[1] + " (" + str(e[0]) + ")")
  224.                
  225. d = Container()
  226. r = wrapper(main)
  227. if r == 1:
  228.         print "Your terminal isn't big enough! It needs to be at least 110x24."
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: