Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
directory viewer for ui
# coding: utf-8
import ui
import os
import console
class MyTableViewDataSource (object):
sel = [None]
def __init__(self, base_dir = '.'):
self.dir = base_dir
_, folders, files = next(os.walk(base_dir)) = (folders,files)
def tableview_number_of_sections(self, tableview):
return 2
def tableview_number_of_rows(self, tableview, section):
return len([section])
def tableview_cell_for_row(self, tableview, section, row):
cell = ui.TableViewCell()
cell.accessory_type = ('disclosure_indicator', 'detail_button')[section]
cell.text_label.text =[section][row]
return cell
def tableview_title_for_header(self, tableview, section):
return ('Folders','Files')[section]
def tableview_did_select(self, tableview, section, row):
'@type tableview: ui.TableView'
if section == 0:
dir = os.path.join(self.dir,[section][row])
newv = FileViewer(dir)
nav = tableview.superview.navigation_view
self.sel[0] = os.path.join(self.dir,[section][row])
def tableview_accessory_button_tapped(self, tableview, section, row):
full = os.path.join(self.dir,[section][row])
stats = os.stat(full)
console.hud_alert('Size: {0} KB'.format(stats.st_size//1024))
class FileViewer(object):
def __init__(self, base_dir = '.', *args, **kargs):
self.table = ui.TableView(*args, **kargs) = 'FileTable'
self.src = MyTableViewDataSource(base_dir)
self.table.data_source = self.src
self.table.delegate = self.src
self.table.flex = 'WHTBLR'
self.view = ui.View(name = base_dir)
self.view.background_color = 'white'
def selection(self):
return self.src.sel[0]
fv = FileViewer()
nv = ui.NavigationView(fv.view) = 'File Selector'
print fv.selection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment