about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-23 17:39:00 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-23 17:39:00 -0700
commita35b130f08e6f580863edc8603c33b1100709c0b (patch)
treeaadf33317b124e430e437e11bfd9d191aa0a74d3 /text.lua
parent288d8426f1ca9e8df8e3d8ea89c17b61013d5285 (diff)
downloadtext.love-a35b130f08e6f580863edc8603c33b1100709c0b.tar.gz
one bug I've repeatedly run into while testing with Moby Dick
https://www.hogbaysoftware.com/posts/moby-dick-workout
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua71
1 files changed, 68 insertions, 3 deletions
diff --git a/text.lua b/text.lua
index 17d93b0..cb5fe3f 100644
--- a/text.lua
+++ b/text.lua
@@ -356,7 +356,7 @@ end
 
 function test_up_arrow_scrolls_up_by_one_screen_line()
   print('test_up_arrow_scrolls_up_by_one_screen_line')
-  -- display lines starting from the second screen line of line 3
+  -- display lines starting from second screen line of a line
   App.screen.init{width=25+30, height=60}
   Lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
   Line_width = App.screen.width
@@ -371,7 +371,7 @@ function test_up_arrow_scrolls_up_by_one_screen_line()
   App.screen.check(y, 'jkl', 'F - test_up_arrow_scrolls_up_by_one_screen_line/baseline/screen:1')
   y = y + line_height
   App.screen.check(y, 'mno', 'F - test_up_arrow_scrolls_up_by_one_screen_line/baseline/screen:2')
-  -- after hitting the up arrow the screen scrolls up by one line
+  -- after hitting the up arrow the screen scrolls up to first screen line
   App.run_after_keychord('up')
   y = screen_top_margin
   App.screen.check(y, 'ghi ', 'F - test_up_arrow_scrolls_up_by_one_screen_line/screen:1')
@@ -385,6 +385,39 @@ function test_up_arrow_scrolls_up_by_one_screen_line()
   check_eq(Cursor1.pos, 1, 'F - test_up_arrow_scrolls_up_by_one_screen_line/cursor')
 end
 
+function test_up_arrow_scrolls_up_to_final_screen_line()
+  print('test_up_arrow_scrolls_up_to_final_screen_line')
+  -- display lines starting just after a long line
+  App.screen.init{width=25+30, height=60}
+  Lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
+  Line_width = App.screen.width
+  Cursor1 = {line=2, pos=1}
+  Screen_top1 = {line=2, pos=1}
+  Screen_bottom1 = {}
+  Zoom = 1
+  local screen_top_margin = 15  -- pixels
+  local line_height = math.floor(15*Zoom)  -- pixels
+  App.draw()
+  local y = screen_top_margin
+  App.screen.check(y, 'ghi', 'F - test_up_arrow_scrolls_up_to_final_screen_line/baseline/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'jkl', 'F - test_up_arrow_scrolls_up_to_final_screen_line/baseline/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'mno', 'F - test_up_arrow_scrolls_up_to_final_screen_line/baseline/screen:3')
+  -- after hitting the up arrow the screen scrolls up to final screen line of previous line
+  App.run_after_keychord('up')
+  y = screen_top_margin
+  App.screen.check(y, 'def', 'F - test_up_arrow_scrolls_up_to_final_screen_line/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'ghi', 'F - test_up_arrow_scrolls_up_to_final_screen_line/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'jkl', 'F - test_up_arrow_scrolls_up_to_final_screen_line/screen:3')
+  check_eq(Screen_top1.line, 1, 'F - test_up_arrow_scrolls_up_to_final_screen_line/screen_top')
+  check_eq(Screen_top1.pos, 5, 'F - test_up_arrow_scrolls_up_to_final_screen_line/screen_top')
+  check_eq(Cursor1.line, 1, 'F - test_up_arrow_scrolls_up_to_final_screen_line/cursor')
+  check_eq(Cursor1.pos, 5, 'F - test_up_arrow_scrolls_up_to_final_screen_line/cursor')
+end
+
 function Text.compute_fragments(line, line_width)
 --?   print('compute_fragments')
   line.fragments = {}
@@ -557,16 +590,21 @@ function Text.keychord_pressed(chord)
       while new_cursor_line > 1 do
         new_cursor_line = new_cursor_line-1
         if Lines[new_cursor_line].mode == 'text' then
+--?           print('found previous text line')
           Cursor1.line = new_cursor_line
+          Text.populate_screen_line_starting_pos(Cursor1.line)
           if Lines[Cursor1.line].screen_line_starting_pos == nil then
             Cursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)
             break
           end
           -- previous text line found, pick its final screen line
+--?           print('has multiple screen lines')
           local screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos
+--?           print(#screen_line_starting_pos)
           screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
 --?           print('previous screen line starts at pos '..tostring(screen_line_starting_pos)..' of its line')
-          if Screen_top1.line == Cursor1.line and Screen_top1.pos == screen_line_starting_pos then
+          if Screen_top1.line > Cursor1.line then
+            Screen_top1.line = Cursor1.line
             Screen_top1.pos = screen_line_starting_pos
 --?             print('pos of top of screen is also '..tostring(Screen_top1.pos)..' of the same line')
           end
@@ -882,4 +920,31 @@ function Text.previous_screen_line(pos2)
   end
 end
 
+function Text.populate_screen_line_starting_pos(line_index)
+  -- duplicate some logic from Text.draw
+  local line = Lines[line_index]
+  if line.fragments == nil then
+    Text.compute_fragments(line, Line_width)
+  end
+  local x = 25
+  local pos = 1
+  for _, f in ipairs(line.fragments) do
+    local frag, frag_text = f.data, f.text
+--?     print(x, frag)
+    -- render fragment
+    local frag_width = math.floor(App.width(frag_text)*Zoom)
+    if x + frag_width > Line_width then
+      x = 25
+      if line.screen_line_starting_pos == nil then
+        line.screen_line_starting_pos = {1, pos}
+      else
+        table.insert(line.screen_line_starting_pos, pos)
+      end
+    end
+    x = x + frag_width
+    local frag_len = utf8.len(frag)
+    pos = pos + frag_len
+  end
+end
+
 return Text
4914a192ab98'>^
4460dd2 ^



bea671d ^




4460dd2 ^
8f5f24d ^
ea14e7c ^
78778ce ^
6a3cb14 ^



9afdfbd ^


fdc444f ^
6a3cb14 ^




ea14e7c ^
78778ce ^
6a3cb14 ^
fdc444f ^
6a3cb14 ^


ea14e7c ^
78778ce ^
8f5f24d ^
3368af1 ^
8f5f24d ^

ea14e7c ^
4460dd2 ^













44d2fb9 ^





9afdfbd ^
44d2fb9 ^











3368af1 ^
8f5f24d ^
3368af1 ^
8f5f24d ^
3368af1 ^


8f5f24d ^
3368af1 ^


8f5f24d ^
3368af1 ^



fdc444f ^



44d2fb9 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165