def populate(self, fname):
# this function populates the TextList and creates a new tabs
# The same Textlist is used for each tab but when tabs are switched the
# contents of the tab are grabbed from the files TabInfo instance
if fname not in self.display.file_names:
# grab the lines from the file and strip the newline char
# then iterate through and create a new TextLine object for each line
try:
with open(fname) as f:
content = [x.strip('\n') for x in f.readlines()]
except:
self.redraw_tabs()
return
# the short name is the file name without a path
self.short_name = strip_fname(fname)
self.display.file_names.append(fname)
new_lines = []
# grab the lines from the file and strip the newline char
# Then iterate through and create a new TextLine object for each line
for line in content:
text = TextLine(line, self.display)
new_lines.append(text)
# if the file is empty then add one empty line so it can be displayed
if len(new_lines) < 1:
text = TextLine(' ', self.display)
new_lines.append(text)
# create a new tab (button widget) with the correct attributes
self.display.cur_tab = self.display.tab_info[fname] = TabInfo(self.display)
new_tab_info = self.display.tab_info[fname]
new_tab_info.lines = new_lines
new_tab_info.undo = UndoStack(self.display)
new_tab_info.cursor = (0, 0)
self.display.line_nums.populate(new_lines)
button = urwid.Button(self.short_name)
button._label.align = 'center'
attrib = urwid.AttrMap(button, 'footer')
self.display.tabs.append(attrib)
# switch to the new tab
self.switch_tabs(fname)
else:
self.display.line_nums.populate(self.lines)
self.redraw_tabs()
评论列表
文章目录