From 5c4cee37f5702a203b2a17ebcead8e73f014d2b6 Mon Sep 17 00:00:00 2001 From: Mikhail Lukashov Date: Thu, 16 Jan 2025 15:33:33 +0300 Subject: [PATCH] improvement: internal command line: edit:file: view:file: take into account the presence of special character `~` (Home directory) This makes it possible to run commands such as edit:~/.bashrc --- far2l/src/cmdline.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/far2l/src/cmdline.cpp b/far2l/src/cmdline.cpp index 7e093888d9..a22af531ee 100644 --- a/far2l/src/cmdline.cpp +++ b/far2l/src/cmdline.cpp @@ -912,6 +912,14 @@ bool CommandLine::ProcessFarCommands(const wchar_t *CmdLine) bool b_far, b_edit = false, b_view = false; std::string::size_type p; std::wstring str_command(CmdLine); + auto expandString = [](const std::wstring &Filename) -> std::wstring { + std::wstring result = Filename; + if ((result.length() >= 2) && (result[0] == '~') && (result[1] == '/')) { + const auto &strHome = CachedHomeDir(); + result.replace(result.begin(), result.begin() + 1, strHome.CPtr()); + } + return result; + }; StrTrim(str_command); @@ -953,10 +961,12 @@ bool CommandLine::ProcessFarCommands(const wchar_t *CmdLine) CP_AUTODETECT, FFILEEDIT_ENABLEF6 | FFILEEDIT_DISABLEHISTORY); } } - else // filename + else { // filename + const std::wstring filename = expandString(str_command.substr(p,std::string::npos)); new FileEditor( - std::make_shared( str_command.substr(p,std::string::npos).c_str() ), + std::make_shared( filename.c_str() ), CP_AUTODETECT, FFILEEDIT_CANNEWFILE | FFILEEDIT_ENABLEF6); + } } else // new empty file new FileEditor( @@ -980,9 +990,11 @@ bool CommandLine::ProcessFarCommands(const wchar_t *CmdLine) TRUE/*EnableSwitch*/, TRUE/*DisableHistory*/, FALSE/*DisableEdit*/); } } - else // filename - new FileViewer(std::make_shared( str_command.substr(p,std::string::npos).c_str() ), + else { // filename + const std::wstring filename = expandString(str_command.substr(p,std::string::npos)); + new FileViewer(std::make_shared( filename.c_str() ), TRUE/*EnableSwitch*/, FALSE/*DisableHistory*/, FALSE/*DisableEdit*/); + } } return true; // anyway prefix correct and was processed }