mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
Add basic "write to file" support.
This commit is contained in:
parent
d2bb139c46
commit
ae6c183475
Notes:
sideshowbarker
2024-07-19 16:08:31 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/ae6c183475d
4 changed files with 45 additions and 4 deletions
|
@ -3,7 +3,7 @@
|
|||
|
||||
OwnPtr<Document> Document::create_from_file(const std::string& path)
|
||||
{
|
||||
auto document = make<Document>();
|
||||
auto document = make<Document>(path);
|
||||
|
||||
FileReader reader(path);
|
||||
while (reader.can_read()) {
|
||||
|
|
|
@ -8,9 +8,11 @@
|
|||
|
||||
class Document {
|
||||
public:
|
||||
Document() { }
|
||||
explicit Document(const std::string& path) : m_path(path) { }
|
||||
~Document() { }
|
||||
|
||||
std::string path() const { return m_path; }
|
||||
|
||||
Line& line(size_t index) { return *m_lines[index]; }
|
||||
const Line& line(size_t index) const { return *m_lines[index]; }
|
||||
size_t line_count() const { return m_lines.size(); }
|
||||
|
@ -25,4 +27,5 @@ public:
|
|||
|
||||
private:
|
||||
std::deque<OwnPtr<Line>> m_lines;
|
||||
std::string m_path;
|
||||
};
|
||||
|
|
|
@ -3,15 +3,14 @@
|
|||
#include "InsertOperation.h"
|
||||
|
||||
#define _XOPEN_SOURCE_EXTENDED
|
||||
#include <locale.h>
|
||||
#include <ncurses.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
static int statusbar_attributes;
|
||||
static int ruler_attributes;
|
||||
|
||||
Editor::Editor()
|
||||
{
|
||||
setlocale(LC_ALL, "");
|
||||
initscr();
|
||||
start_color();
|
||||
use_default_colors();
|
||||
|
@ -160,6 +159,28 @@ int Editor::exec()
|
|||
return 0;
|
||||
}
|
||||
|
||||
void Editor::write_to_file()
|
||||
{
|
||||
FILE* fp = fopen(m_document->path().c_str(), "w");
|
||||
if (!fp) {
|
||||
set_status_text("Failed to open %s for writing", m_document->path().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
size_t bytes = 0;
|
||||
for (size_t i = 0; i < m_document->line_count(); ++i) {
|
||||
fwrite(m_document->line(i).data().c_str(), sizeof(char), m_document->line(i).length(), fp);
|
||||
bytes += m_document->line(i).length();
|
||||
if (i != m_document->line_count() - 1) {
|
||||
fputc('\n', fp);
|
||||
++bytes;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
set_status_text("Wrote %zu bytes across %zu lines", bytes, m_document->line_count());
|
||||
}
|
||||
|
||||
void Editor::move_left()
|
||||
{
|
||||
if (m_cursor.column() == 0)
|
||||
|
@ -329,6 +350,16 @@ void Editor::set_status_text(const std::string& text)
|
|||
m_status_text = text;
|
||||
}
|
||||
|
||||
void Editor::set_status_text(const char* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
char buf[128];
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
m_status_text = buf;
|
||||
}
|
||||
|
||||
void Editor::exec_command()
|
||||
{
|
||||
if (m_command == "q") {
|
||||
|
@ -336,6 +367,11 @@ void Editor::exec_command()
|
|||
return;
|
||||
}
|
||||
|
||||
if (m_command == "w") {
|
||||
write_to_file();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_command == "about") {
|
||||
set_status_text("cuki editor!");
|
||||
return;
|
||||
|
|
|
@ -30,6 +30,7 @@ public:
|
|||
bool is_idle() const { return m_mode == Idle; }
|
||||
|
||||
void set_status_text(const std::string&);
|
||||
void set_status_text(const char* fmt, ...);
|
||||
|
||||
bool insert_text_at_cursor(const std::string&);
|
||||
bool remove_text_at_cursor(const std::string&);
|
||||
|
@ -57,6 +58,7 @@ private:
|
|||
|
||||
void exec_command();
|
||||
void coalesce_current_line();
|
||||
void write_to_file();
|
||||
|
||||
OwnPtr<Document> m_document;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue