Luna/utils/edit.cpp
apio 140910763e
All checks were successful
Build and test / build (push) Successful in 1m56s
all: Reorder directory structure
Why are command-line utilities stored in "apps"?
And why are apps like "editor" or "terminal" top-level directories?
Command-line utilities now go in "utils".
GUI stuff now goes in "gui".
This includes: libui -> gui/libui, wind -> gui/wind, GUI apps -> gui/apps, editor&terminal -> gui/apps...
System services go in "system".
2024-07-21 13:24:46 +02:00

42 lines
1.2 KiB
C++

#include <luna/String.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <os/FileSystem.h>
#include <os/Prompt.h>
using os::File;
Result<int> luna_main(int argc, char** argv)
{
StringView pathname;
os::ArgumentParser parser;
parser.add_description("Edit a file using basic line-based shell editing."_sv);
parser.add_system_program_info("edit"_sv);
parser.add_positional_argument(pathname, "path"_sv, true);
parser.parse(argc, argv);
if (os::FileSystem::exists(pathname))
{
String prompt = TRY(String::format("File %s already exists. Overwrite?"_sv, pathname.chars()));
bool overwrite = os::conditional_prompt(prompt.chars(), os::DefaultNo);
if (!overwrite) return 0;
}
auto file = TRY(File::open_or_create(pathname, File::WriteOnly));
auto input = File::standard_input();
os::println("- Editing %s. Press Enter to start a new line, or Ctrl+D at the start of a line to save and exit. -",
pathname.chars());
while (1)
{
String line = TRY(input->read_line());
if (line.is_empty()) break;
TRY(file->write(line.view()));
}
return 0;
}