1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 17:28:11 +00:00

TextEditor: Specify the starting line and column number using colons

This allows the user to specify a specific line and column number to
start at when opening a file in TextEditor through the terminal, by
adding a colon after the file name.

For example, `TextEditor ReadMe.md:10:5` will open ReadMe.md and put
the cursor on line 10 at column 5.

To ensure that the user isn't trying to open a file that actually has
colons in its name, it checks if the file exists before parsing.

Replaces the feature added in b474f49164
Closes #5589
This commit is contained in:
ry755 2021-03-05 23:13:22 -08:00 committed by Linus Groh
parent 68a542623f
commit 8af7cda17a
4 changed files with 94 additions and 8 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "FileArgument.h"
#include "MainWidget.h"
#include <LibCore/ArgsParser.h>
#include <LibGUI/Menubar.h>
@ -27,12 +28,10 @@ int main(int argc, char** argv)
}
const char* preview_mode = "auto";
int initial_line_number = 0;
const char* file_to_edit = nullptr;
Core::ArgsParser parser;
parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode");
parser.add_option(initial_line_number, "Start at line number", "line-number", 'l', "line");
parser.add_positional_argument(file_to_edit, "File to edit", "file", Core::ArgsParser::Required::No);
parser.add_positional_argument(file_to_edit, "File to edit, with optional starting line and column number", "file[:line[:column]]", Core::ArgsParser::Required::No);
parser.parse(argc, argv);
@ -71,15 +70,14 @@ int main(int argc, char** argv)
window->set_menubar(menubar);
if (file_to_edit) {
if (!text_widget.open_file(file_to_edit))
// A file name was passed, parse any possible line and column numbers included.
FileArgument parsed_argument(file_to_edit);
if (!text_widget.open_file(parsed_argument.file_name()))
return 1;
text_widget.editor().set_cursor_and_focus_line(parsed_argument.line().value_or(1) - 1, parsed_argument.column().value_or(0));
}
text_widget.update_title();
if (initial_line_number != 0)
text_widget.editor().set_cursor_and_focus_line(initial_line_number - 1, 0);
window->show();
window->set_icon(app_icon.bitmap_for_size(16));