Share Pastebin
Guest
Public paste!

Untitled

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