Merge "Rewritten file pager."
diff --git a/screen_ui.cpp b/screen_ui.cpp
index b62417f..1e42ee7 100644
--- a/screen_ui.cpp
+++ b/screen_ui.cpp
@@ -28,6 +28,8 @@
 #include <time.h>
 #include <unistd.h>
 
+#include <vector>
+
 #include "common.h"
 #include "device.h"
 #include "minui/minui.h"
@@ -203,6 +205,13 @@
     }
 }
 
+void ScreenRecoveryUI::DrawHorizontalRule(int* y) {
+    SetColor(MENU);
+    *y += 4;
+    gr_fill(0, *y, gr_fb_width(), *y + 2);
+    *y += 8;
+}
+
 // Redraw everything on the screen.  Does not flip pages.
 // Should only be called with updateMutex locked.
 void ScreenRecoveryUI::draw_screen_locked() {
@@ -214,12 +223,11 @@
         gr_clear();
 
         int y = 0;
-        int i = 0;
         if (show_menu) {
             SetColor(HEADER);
 
-            for (; i < menu_top + menu_items; ++i) {
-                if (i == menu_top) SetColor(MENU);
+            for (int i = 0; i < menu_top + menu_items; ++i) {
+                if (i == menu_top) DrawHorizontalRule(&y);
 
                 if (i == menu_top + menu_sel) {
                     // draw the highlight bar
@@ -234,11 +242,8 @@
                 }
                 y += char_height+4;
             }
-            SetColor(MENU);
-            y += 4;
-            gr_fill(0, y, gr_fb_width(), y+2);
-            y += 4;
-            ++i;
+
+            DrawHorizontalRule(&y);
         }
 
         SetColor(LOG);
@@ -249,7 +254,7 @@
         int row = (text_top+text_rows-1) % text_rows;
         size_t count = 0;
         for (int ty = gr_fb_height() - char_height;
-             ty > y+2 && count < text_rows;
+             ty >= y && count < text_rows;
              ty -= char_height, ++count) {
             gr_text(0, ty, text[row], 0);
             --row;
@@ -495,72 +500,85 @@
     pthread_mutex_unlock(&updateMutex);
 }
 
-// TODO: replace this with something not line-based so we can wrap correctly without getting
-// confused about what line we're on.
-void ScreenRecoveryUI::print_no_update(const char* s) {
+void ScreenRecoveryUI::PutChar(char ch) {
     pthread_mutex_lock(&updateMutex);
-    if (text_rows > 0 && text_cols > 0) {
-        for (const char* ptr = s; *ptr != '\0'; ++ptr) {
-            if (*ptr == '\n' || text_col >= text_cols) {
-                text[text_row][text_col] = '\0';
-                text_col = 0;
-                text_row = (text_row + 1) % text_rows;
-                if (text_row == text_top) text_top = (text_top + 1) % text_rows;
-            }
-            if (*ptr != '\n') text[text_row][text_col++] = *ptr;
-        }
-        text[text_row][text_col] = '\0';
+    if (ch != '\n') text[text_row][text_col++] = ch;
+    if (ch == '\n' || text_col >= text_cols) {
+        text_col = 0;
+        ++text_row;
     }
     pthread_mutex_unlock(&updateMutex);
 }
 
+void ScreenRecoveryUI::ClearText() {
+    pthread_mutex_lock(&updateMutex);
+    text_col = 0;
+    text_row = 0;
+    text_top = 1;
+    for (size_t i = 0; i < text_rows; ++i) {
+        memset(text[i], 0, text_cols + 1);
+    }
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void ScreenRecoveryUI::ShowFile(FILE* fp) {
+    std::vector<long> offsets;
+    offsets.push_back(ftell(fp));
+    ClearText();
+
+    struct stat sb;
+    fstat(fileno(fp), &sb);
+
+    bool show_prompt = false;
+    while (true) {
+        if (show_prompt) {
+            Print("--(%d%% of %d bytes)--",
+                  static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
+                  static_cast<int>(sb.st_size));
+            Redraw();
+            while (show_prompt) {
+                show_prompt = false;
+                int key = WaitKey();
+                if (key == KEY_POWER) {
+                    return;
+                } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
+                    if (offsets.size() <= 1) {
+                        show_prompt = true;
+                    } else {
+                        offsets.pop_back();
+                        fseek(fp, offsets.back(), SEEK_SET);
+                    }
+                } else {
+                    if (feof(fp)) {
+                        return;
+                    }
+                    offsets.push_back(ftell(fp));
+                }
+            }
+            ClearText();
+        }
+
+        int ch = getc(fp);
+        if (ch == EOF) {
+            text_row = text_top = text_rows - 2;
+            show_prompt = true;
+        } else {
+            PutChar(ch);
+            if (text_col == 0 && text_row >= text_rows - 2) {
+                text_top = text_row;
+                show_prompt = true;
+            }
+        }
+    }
+}
+
 void ScreenRecoveryUI::ShowFile(const char* filename) {
     FILE* fp = fopen_path(filename, "re");
     if (fp == nullptr) {
         Print("  Unable to open %s: %s\n", filename, strerror(errno));
         return;
     }
-
-    char line[1024];
-    int ct = 0;
-    int key = 0;
-    while (fgets(line, sizeof(line), fp) != nullptr) {
-        print_no_update(line);
-        ct++;
-        if (ct % text_rows == 0) {
-            Redraw();
-
-            // give the user time to glance at the entries
-            key = WaitKey();
-
-            if (key == KEY_POWER) {
-                break;
-            } else if (key == KEY_VOLUMEUP) {
-                // Go back by seeking to the beginning and dumping ct - n
-                // lines.  It's ugly, but this way we don't need to store
-                // the previous offsets.  The files we're dumping here aren't
-                // expected to be very large.
-                ct -= 2 * text_rows;
-                if (ct < 0) {
-                    ct = 0;
-                }
-                fseek(fp, 0, SEEK_SET);
-                for (int i = 0; i < ct; i++) {
-                    fgets(line, sizeof(line), fp);
-                }
-                Print("^^^^^^^^^^\n");
-            } else {
-                // Next page.
-            }
-        }
-    }
-
-    // If the user didn't abort, then give the user time to glance at
-    // the end of the log, sorry, no rewind here
-    if (key != KEY_POWER) {
-        Print("\n--END-- (press any key)\n");
-        WaitKey();
-    }
+    ShowFile(fp);
     fclose(fp);
 }
 
@@ -581,7 +599,7 @@
             menu[i][text_cols-1] = '\0';
         }
         menu_items = i - menu_top;
-        show_menu = 1;
+        show_menu = true;
         menu_sel = initial_selection;
         update_screen_locked();
     }
@@ -590,7 +608,7 @@
 
 int ScreenRecoveryUI::SelectMenu(int sel) {
     pthread_mutex_lock(&updateMutex);
-    if (show_menu > 0) {
+    if (show_menu) {
         int old_sel = menu_sel;
         menu_sel = sel;
 
@@ -607,8 +625,8 @@
 
 void ScreenRecoveryUI::EndMenu() {
     pthread_mutex_lock(&updateMutex);
-    if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
-        show_menu = 0;
+    if (show_menu && text_rows > 0 && text_cols > 0) {
+        show_menu = false;
         update_screen_locked();
     }
     pthread_mutex_unlock(&updateMutex);
diff --git a/screen_ui.h b/screen_ui.h
index ea1a95b..590e5c8 100644
--- a/screen_ui.h
+++ b/screen_ui.h
@@ -18,6 +18,7 @@
 #define RECOVERY_SCREEN_UI_H
 
 #include <pthread.h>
+#include <stdio.h>
 
 #include "ui.h"
 #include "minui/minui.h"
@@ -114,7 +115,11 @@
     static void* progress_thread(void* cookie);
     void progress_loop();
 
-    void print_no_update(const char*);
+    void ShowFile(FILE*);
+    void PutChar(char);
+    void ClearText();
+
+    void DrawHorizontalRule(int* y);
 
     void LoadBitmap(const char* filename, gr_surface* surface);
     void LoadBitmapArray(const char* filename, int* frames, gr_surface** surface);