Split in TextWindow and SourceWindow.
Count lines in Python.
This commit is contained in:
parent
0bf32e3c78
commit
6fd42b8e92
|
@ -7,58 +7,62 @@ import basewin
|
||||||
WIDTH = 40
|
WIDTH = 40
|
||||||
MAXHEIGHT = 24
|
MAXHEIGHT = 24
|
||||||
|
|
||||||
class SourceWindow(basewin.BaseWindow):
|
|
||||||
|
|
||||||
def init(self, filename):
|
class TextWindow(basewin.BaseWindow):
|
||||||
self.filename = filename
|
|
||||||
#
|
def init(self, title, contents):
|
||||||
f = open(self.filename, 'r') # raise exception if not found
|
self.contents = contents
|
||||||
self.contents = f.read()
|
self.linecount = countlines(self.contents)
|
||||||
f.seek(0)
|
|
||||||
self.linecount = len(f.readlines())
|
|
||||||
f.close()
|
|
||||||
#
|
#
|
||||||
self.lineheight = lh = stdwin.lineheight()
|
self.lineheight = lh = stdwin.lineheight()
|
||||||
self.leftmargin = stdwin.textwidth('00000000')
|
self.leftmargin = self.getmargin()
|
||||||
|
self.top = 0
|
||||||
self.rightmargin = 30000 # Infinity
|
self.rightmargin = 30000 # Infinity
|
||||||
self.bottom = lh * self.linecount
|
self.bottom = lh * self.linecount
|
||||||
#
|
#
|
||||||
stdwin.setdefwinpos(0, 0)
|
|
||||||
width = WIDTH*stdwin.textwidth('0')
|
width = WIDTH*stdwin.textwidth('0')
|
||||||
height = lh*min(MAXHEIGHT, self.linecount)
|
height = lh*min(MAXHEIGHT, self.linecount)
|
||||||
stdwin.setdefwinsize(width, height)
|
stdwin.setdefwinsize(width, height)
|
||||||
self = basewin.BaseWindow.init(self, filename)
|
self = basewin.BaseWindow.init(self, title)
|
||||||
#
|
#
|
||||||
self.win.setdocsize(0, self.bottom + lh)
|
self.win.setdocsize(0, self.bottom)
|
||||||
self.initeditor()
|
self.initeditor()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def initeditor(self):
|
def initeditor(self):
|
||||||
r = (self.leftmargin, 0), (self.rightmargin, self.bottom)
|
r = (self.leftmargin, self.top), (self.rightmargin, self.bottom)
|
||||||
self.editor = self.win.textcreate(r)
|
self.editor = self.win.textcreate(r)
|
||||||
self.editor.settext(self.contents)
|
self.editor.settext(self.contents)
|
||||||
|
|
||||||
def closeeditor(self):
|
def closeeditor(self):
|
||||||
self.editor.close()
|
self.editor.close()
|
||||||
|
|
||||||
def reopen(self):
|
# def reopen(self):
|
||||||
self.closeeditor()
|
# self.closeeditor()
|
||||||
basewin.BaseWindow.reopen(self)
|
# basewin.BaseWindow.reopen(self)
|
||||||
self.initeditor()
|
# self.initeditor()
|
||||||
|
|
||||||
def close(self):
|
# Override the following two methods to format line numbers differently
|
||||||
self.closeeditor()
|
|
||||||
basewin.BaseWindow.close(self)
|
|
||||||
|
|
||||||
# Override this method to format line numbers differently
|
|
||||||
def getmark(self, lineno):
|
def getmark(self, lineno):
|
||||||
return `lineno`
|
return `lineno`
|
||||||
|
|
||||||
|
def getmargin(self):
|
||||||
|
return stdwin.textwidth(`self.linecount + 1` + ' ')
|
||||||
|
|
||||||
|
# Event dispatcher, called from mainloop.mainloop()
|
||||||
|
|
||||||
def dispatch(self, event):
|
def dispatch(self, event):
|
||||||
if event[0] == WE_NULL: return # Dummy tested by mainloop
|
if event[0] == WE_NULL: return # Dummy tested by mainloop
|
||||||
if event[0] == WE_DRAW or not self.editor.event(event):
|
if event[0] == WE_DRAW or not self.editor.event(event):
|
||||||
basewin.BaseWindow.dispatch(self, event)
|
basewin.BaseWindow.dispatch(self, event)
|
||||||
|
|
||||||
|
# Event handlers
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.closeeditor()
|
||||||
|
basewin.BaseWindow.close(self)
|
||||||
|
|
||||||
def draw(self, detail):
|
def draw(self, detail):
|
||||||
dummy = self.editor.draw(detail)
|
dummy = self.editor.draw(detail)
|
||||||
# Draw line numbers
|
# Draw line numbers
|
||||||
|
@ -75,7 +79,9 @@ class SourceWindow(basewin.BaseWindow):
|
||||||
finally:
|
finally:
|
||||||
d.close()
|
d.close()
|
||||||
|
|
||||||
def changemark(self, lineno):
|
# Calls from outside
|
||||||
|
|
||||||
|
def changemark(self, lineno): # redraw the mark for a line
|
||||||
left = 0
|
left = 0
|
||||||
top = (lineno-1) * self.lineheight
|
top = (lineno-1) * self.lineheight
|
||||||
right = self.leftmargin
|
right = self.leftmargin
|
||||||
|
@ -87,7 +93,7 @@ class SourceWindow(basewin.BaseWindow):
|
||||||
finally:
|
finally:
|
||||||
d.close()
|
d.close()
|
||||||
|
|
||||||
def showline(self, lineno):
|
def showline(self, lineno): # scroll to make a line visible
|
||||||
left = 0
|
left = 0
|
||||||
top = (lineno-1) * self.lineheight
|
top = (lineno-1) * self.lineheight
|
||||||
right = self.leftmargin
|
right = self.leftmargin
|
||||||
|
@ -95,6 +101,27 @@ class SourceWindow(basewin.BaseWindow):
|
||||||
self.win.show((left, top), (right, bottom))
|
self.win.show((left, top), (right, bottom))
|
||||||
|
|
||||||
|
|
||||||
|
# Subroutine to count the number of lines in a string
|
||||||
|
|
||||||
|
def countlines(text):
|
||||||
|
n = 0
|
||||||
|
for c in text:
|
||||||
|
if c == '\n': n = n+1
|
||||||
|
if text and text[-1] != '\n': n = n+1 # Partial last line
|
||||||
|
return n
|
||||||
|
|
||||||
|
|
||||||
|
class SourceWindow(TextWindow):
|
||||||
|
|
||||||
|
def init(self, filename):
|
||||||
|
self.filename = filename
|
||||||
|
f = open(self.filename, 'r')
|
||||||
|
contents = f.read()
|
||||||
|
f.close()
|
||||||
|
return TextWindow.init(self, self.filename, contents)
|
||||||
|
|
||||||
|
# ------------------------------ testing ------------------------------
|
||||||
|
|
||||||
TESTFILE = 'srcwin.py'
|
TESTFILE = 'srcwin.py'
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
|
|
|
@ -7,58 +7,62 @@ import basewin
|
||||||
WIDTH = 40
|
WIDTH = 40
|
||||||
MAXHEIGHT = 24
|
MAXHEIGHT = 24
|
||||||
|
|
||||||
class SourceWindow(basewin.BaseWindow):
|
|
||||||
|
|
||||||
def init(self, filename):
|
class TextWindow(basewin.BaseWindow):
|
||||||
self.filename = filename
|
|
||||||
#
|
def init(self, title, contents):
|
||||||
f = open(self.filename, 'r') # raise exception if not found
|
self.contents = contents
|
||||||
self.contents = f.read()
|
self.linecount = countlines(self.contents)
|
||||||
f.seek(0)
|
|
||||||
self.linecount = len(f.readlines())
|
|
||||||
f.close()
|
|
||||||
#
|
#
|
||||||
self.lineheight = lh = stdwin.lineheight()
|
self.lineheight = lh = stdwin.lineheight()
|
||||||
self.leftmargin = stdwin.textwidth('00000000')
|
self.leftmargin = self.getmargin()
|
||||||
|
self.top = 0
|
||||||
self.rightmargin = 30000 # Infinity
|
self.rightmargin = 30000 # Infinity
|
||||||
self.bottom = lh * self.linecount
|
self.bottom = lh * self.linecount
|
||||||
#
|
#
|
||||||
stdwin.setdefwinpos(0, 0)
|
|
||||||
width = WIDTH*stdwin.textwidth('0')
|
width = WIDTH*stdwin.textwidth('0')
|
||||||
height = lh*min(MAXHEIGHT, self.linecount)
|
height = lh*min(MAXHEIGHT, self.linecount)
|
||||||
stdwin.setdefwinsize(width, height)
|
stdwin.setdefwinsize(width, height)
|
||||||
self = basewin.BaseWindow.init(self, filename)
|
self = basewin.BaseWindow.init(self, title)
|
||||||
#
|
#
|
||||||
self.win.setdocsize(0, self.bottom + lh)
|
self.win.setdocsize(0, self.bottom)
|
||||||
self.initeditor()
|
self.initeditor()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def initeditor(self):
|
def initeditor(self):
|
||||||
r = (self.leftmargin, 0), (self.rightmargin, self.bottom)
|
r = (self.leftmargin, self.top), (self.rightmargin, self.bottom)
|
||||||
self.editor = self.win.textcreate(r)
|
self.editor = self.win.textcreate(r)
|
||||||
self.editor.settext(self.contents)
|
self.editor.settext(self.contents)
|
||||||
|
|
||||||
def closeeditor(self):
|
def closeeditor(self):
|
||||||
self.editor.close()
|
self.editor.close()
|
||||||
|
|
||||||
def reopen(self):
|
# def reopen(self):
|
||||||
self.closeeditor()
|
# self.closeeditor()
|
||||||
basewin.BaseWindow.reopen(self)
|
# basewin.BaseWindow.reopen(self)
|
||||||
self.initeditor()
|
# self.initeditor()
|
||||||
|
|
||||||
def close(self):
|
# Override the following two methods to format line numbers differently
|
||||||
self.closeeditor()
|
|
||||||
basewin.BaseWindow.close(self)
|
|
||||||
|
|
||||||
# Override this method to format line numbers differently
|
|
||||||
def getmark(self, lineno):
|
def getmark(self, lineno):
|
||||||
return `lineno`
|
return `lineno`
|
||||||
|
|
||||||
|
def getmargin(self):
|
||||||
|
return stdwin.textwidth(`self.linecount + 1` + ' ')
|
||||||
|
|
||||||
|
# Event dispatcher, called from mainloop.mainloop()
|
||||||
|
|
||||||
def dispatch(self, event):
|
def dispatch(self, event):
|
||||||
if event[0] == WE_NULL: return # Dummy tested by mainloop
|
if event[0] == WE_NULL: return # Dummy tested by mainloop
|
||||||
if event[0] == WE_DRAW or not self.editor.event(event):
|
if event[0] == WE_DRAW or not self.editor.event(event):
|
||||||
basewin.BaseWindow.dispatch(self, event)
|
basewin.BaseWindow.dispatch(self, event)
|
||||||
|
|
||||||
|
# Event handlers
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.closeeditor()
|
||||||
|
basewin.BaseWindow.close(self)
|
||||||
|
|
||||||
def draw(self, detail):
|
def draw(self, detail):
|
||||||
dummy = self.editor.draw(detail)
|
dummy = self.editor.draw(detail)
|
||||||
# Draw line numbers
|
# Draw line numbers
|
||||||
|
@ -75,7 +79,9 @@ class SourceWindow(basewin.BaseWindow):
|
||||||
finally:
|
finally:
|
||||||
d.close()
|
d.close()
|
||||||
|
|
||||||
def changemark(self, lineno):
|
# Calls from outside
|
||||||
|
|
||||||
|
def changemark(self, lineno): # redraw the mark for a line
|
||||||
left = 0
|
left = 0
|
||||||
top = (lineno-1) * self.lineheight
|
top = (lineno-1) * self.lineheight
|
||||||
right = self.leftmargin
|
right = self.leftmargin
|
||||||
|
@ -87,7 +93,7 @@ class SourceWindow(basewin.BaseWindow):
|
||||||
finally:
|
finally:
|
||||||
d.close()
|
d.close()
|
||||||
|
|
||||||
def showline(self, lineno):
|
def showline(self, lineno): # scroll to make a line visible
|
||||||
left = 0
|
left = 0
|
||||||
top = (lineno-1) * self.lineheight
|
top = (lineno-1) * self.lineheight
|
||||||
right = self.leftmargin
|
right = self.leftmargin
|
||||||
|
@ -95,6 +101,27 @@ class SourceWindow(basewin.BaseWindow):
|
||||||
self.win.show((left, top), (right, bottom))
|
self.win.show((left, top), (right, bottom))
|
||||||
|
|
||||||
|
|
||||||
|
# Subroutine to count the number of lines in a string
|
||||||
|
|
||||||
|
def countlines(text):
|
||||||
|
n = 0
|
||||||
|
for c in text:
|
||||||
|
if c == '\n': n = n+1
|
||||||
|
if text and text[-1] != '\n': n = n+1 # Partial last line
|
||||||
|
return n
|
||||||
|
|
||||||
|
|
||||||
|
class SourceWindow(TextWindow):
|
||||||
|
|
||||||
|
def init(self, filename):
|
||||||
|
self.filename = filename
|
||||||
|
f = open(self.filename, 'r')
|
||||||
|
contents = f.read()
|
||||||
|
f.close()
|
||||||
|
return TextWindow.init(self, self.filename, contents)
|
||||||
|
|
||||||
|
# ------------------------------ testing ------------------------------
|
||||||
|
|
||||||
TESTFILE = 'srcwin.py'
|
TESTFILE = 'srcwin.py'
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
|
|
Loading…
Reference in New Issue