Files
roro9stack/src/apps/file_viewer.h
T
twislaandClaude Opus 5.5 e8a654a15f Notes: plain text notes on the SD card, with an editor that saves by itself (#19)
The Notes App lists the files of /notes by their first line, newest first:
n starts a note, Enter opens it, r renames its file, d deletes it after
asking, s sorts by name. A new note's file is named after its first line.

The editor wraps at spaces, 38 columns by 8 rows; Fn+arrows move through
the wrapped text, Ctrl+A and Ctrl+E go to the ends of the line. There is no
save key: the note is written five seconds after the last key, on Back, on
leaving the App, when the screen turns off and before the device powers
off. A save writes a temporary file and puts it in the note's place; a save
cut short is put back, or offered, the next time.

A note is up to 16 KB, held in one buffer reserved when it's opened: the
file is read straight into it and typing never makes it grow. A failed
allocation aborts on this device, and with IRC connected the largest free
block is about 31 KB: a first version that copied the note once on loading
restarted the device when a full note was opened with IRC connected.
Editing files of any size is #47.

The Storage App's text viewer gets `e`, which edits a text file up to 16 KB
with the same editor unless the file is read-only.

439 host tests. Checked on the device: docs/milestones/F1.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
2026-10-06 10:08:10 +02:00

76 lines
2.5 KiB
C++

#pragma once
#include <FS.h>
#include <memory>
#include <string>
#include <vector>
#include "dialog_model.h"
#include "key_event.h"
#include "list_model.h"
#include "services/storage_service.h"
#include "services/update_service.h"
#include "text_pager.h"
#include "ui/canvas.h"
#include "ui/theme.h"
namespace roro {
// Looks inside a file for the Storage App (F1, Q134), by type: text read as it's scrolled, a hex
// dump, a Capture's packets, a Track's summary, an Update File's version and whether it's
// genuine. Tab switches to the text or the hex of the same file. Nothing here changes a file.
class FileViewer {
public:
FileViewer(StorageService& storage, UpdateService& update) : storage_(storage), update_(update) {}
void open(const std::string& path, uint32_t size);
void close();
bool onKey(const KeyEvent& e); // false: leave the viewer
bool update(uint32_t nowMs); // true: draw again
void draw(Canvas& c);
// For `e` in the Storage App: is this a text small enough to edit (Q146)? And a line to say why not.
bool showingText() const { return mode_ == Mode::Text; }
const std::string& path() const { return path_; }
uint32_t size() const { return size_; }
void say(const std::string& text);
private:
enum class Mode { Text, Hex, Gpx, Pcap, Packet, Ota };
static constexpr int kRows = 8;
static constexpr int kCols = 38;
struct Scan; // what a storage job reads through a whole file for: shared with that job
size_t readAt(uint32_t offset, uint8_t* into, size_t len);
void show(Mode mode);
void startScan(Mode mode);
void refresh(); // reads what the screen shows, when it has moved
void openPacket(int index);
void scroll(int lines);
std::string title() const;
StorageService& storage_;
UpdateService& update_;
std::string path_;
uint32_t size_ = 0;
Mode mode_ = Mode::Text, base_ = Mode::Text;
std::shared_ptr<fs::File> file_; // opened and read on the storage task only
std::unique_ptr<files::TextPager> pager_;
uint32_t hexTop_ = 0; // in rows of eight bytes
std::vector<std::string> shown_; // the rows on screen
bool stale_ = true;
std::shared_ptr<Scan> scan_;
bool scanShown_ = false;
int shownPercent_ = -1;
ListModel packets_{kRows};
int rowsFrom_ = -1;
std::vector<std::string> details_;
int detailsTop_ = 0;
std::unique_ptr<DialogModel> confirm_;
std::string message_;
uint32_t messageMs_ = 0;
};
} // namespace roro