From 65834ff491829f4622ad226741f0874bf1c1e680 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 22 Aug 2023 15:17:13 +0200 Subject: [PATCH] edit: Make it more user-friendly --- apps/edit.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apps/edit.cpp b/apps/edit.cpp index 2af1d36b..d1afd90e 100644 --- a/apps/edit.cpp +++ b/apps/edit.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include using os::File; @@ -14,10 +16,20 @@ Result luna_main(int argc, char** argv) 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());