@@ -14,72 +14,73 @@ function showFileSelector() {
1414 } ;
1515 } ) ;
1616
17- menuItems [ '< Back' ] = ( ) => { load ( ) ; } ; // Go back to the launcher or previous screen
17+ menuItems [ '< Back' ] = ( ) => { load ( ) ; } ;
1818 E . showMenu ( menuItems ) ;
1919}
2020
2121function onFileSelected ( file ) {
22+ const fileSize = require ( "Storage" ) . read ( file , 0 , 0 ) . length ;
23+ const chunkSize = 1024 ;
24+ let currentOffset = 0 ;
25+ let currentPage = 1 ;
26+ let history = [ ] ;
2227
23- var text = require ( "Storage" ) . read ( file ) ;
24-
25- function displayText ( text , startLine , pageNumber ) {
28+ function displayText ( offset , pageNumber ) {
2629 g . clear ( ) ;
2730 g . setFont ( "6x8" , 1 ) ;
2831 g . setColor ( 1 ) ;
2932 g . drawString ( "Page " + pageNumber , 10 , 2 ) ;
30- g . drawString ( file , g . getWidth ( ) - file . length * 6 , 2 ) ;
33+ //g.drawString("Offset " + offset, 60, 2);
34+ g . drawString ( file , g . getWidth ( ) - file . length * 6 , 2 ) ;
3135
36+ var text = require ( "Storage" ) . read ( file , offset , chunkSize ) ;
3237 var lines = text . split ( "\n" ) ;
3338 var y = 15 ; // Text start, top row reserved for page number
34- var currentLine = startLine || 0 ;
35- var linesDisplayed = 0 ; //Per page
39+ var linesDisplayed = 0 ; // Lines per page
40+ var totalCharsDisplayed = 0 ; // Total characters per page
3641
37- for ( var i = currentLine ; i < lines . length ; i ++ ) {
42+ for ( var i = 0 ; i < lines . length ; i ++ ) {
3843 var wrappedLines = g . wrapString ( lines [ i ] , g . getWidth ( ) - 20 ) ;
3944 for ( var j = 0 ; j < wrappedLines . length ; j ++ ) {
4045 g . drawString ( wrappedLines [ j ] , 10 , y ) ;
4146 y += 10 ; // Move down for the next line
4247 linesDisplayed ++ ;
48+ totalCharsDisplayed += wrappedLines [ j ] . length + ( j < wrappedLines . length - 1 ? 0 : 1 ) ; // Add newline character for the last wrapped line
4349 if ( y >= g . getHeight ( ) - 10 ) {
4450 // If we run out of space, stop drawing
45- return { nextStartLine : i , linesDisplayed : linesDisplayed } ;
51+ return { nextOffset : offset + totalCharsDisplayed , linesDisplayed : linesDisplayed } ;
4652 }
4753 }
4854 }
4955 return null ; // No more lines to display
5056 }
5157
52- var currentStartLine = 0 ;
53- var currentPage = 1 ;
54- var history = [ ] ; // Track the start line and lines displayed for each page
55-
5658 // Initial display
57- var result = displayText ( text , currentStartLine , currentPage ) ;
58- history . push ( { startLine : currentStartLine , linesDisplayed : result . linesDisplayed } ) ;
59+ var result = displayText ( currentOffset , currentPage ) ;
60+ history . push ( { offset : currentOffset , linesDisplayed : result . linesDisplayed } ) ;
5961
6062 // Handle touch events
6163 Bangle . on ( 'touch' , function ( button ) {
6264 if ( button === 2 ) { // Right side of the screen (next page)
63- var nextStartLine = displayText ( text , currentStartLine , currentPage + 1 ) ;
64- if ( nextStartLine !== null ) {
65- currentStartLine = nextStartLine . nextStartLine ;
65+ var nextOffset = displayText ( currentOffset , currentPage + 1 ) ;
66+ if ( nextOffset !== null ) {
67+ currentOffset = nextOffset . nextOffset ;
6668 currentPage ++ ;
67- history . push ( { startLine : currentStartLine , linesDisplayed : nextStartLine . linesDisplayed } ) ;
68- displayText ( text , currentStartLine , currentPage ) ;
69+ history . push ( { offset : currentOffset , linesDisplayed : nextOffset . linesDisplayed } ) ;
70+ displayText ( currentOffset , currentPage ) ;
6971 } else {
70- currentStartLine = 0 ;
72+ currentOffset = 0 ;
7173 currentPage = 1 ;
72- history = [ { startLine : currentStartLine , linesDisplayed : result . linesDisplayed } ] ;
73- displayText ( text , currentStartLine , currentPage ) ;
74+ history = [ { offset : currentOffset , linesDisplayed : result . linesDisplayed } ] ;
75+ displayText ( currentOffset , currentPage ) ;
7476 }
7577 } else if ( button === 1 ) { // Left side of the screen (previous page)
7678 if ( currentPage > 1 ) {
77- // Go back to the previous page
78- history . pop ( ) ; // Remove the current page from history
79+ history . pop ( ) ; // Remove current page from history
7980 var previousPage = history [ history . length - 1 ] ;
80- currentStartLine = previousPage . startLine ;
81+ currentOffset = previousPage . offset ;
8182 currentPage -- ;
82- displayText ( text , currentStartLine , currentPage ) ;
83+ displayText ( currentOffset , currentPage ) ;
8384 }
8485 }
8586 } ) ;
0 commit comments