Explorar o código

find: Print hyperlinks when standard output is attached to a terminal

Tim Ledbetter hai 1 ano
pai
achega
71ddc33fbf
Modificáronse 2 ficheiros con 20 adicións e 2 borrados
  1. 1 1
      Userland/Utilities/CMakeLists.txt
  2. 19 1
      Userland/Utilities/find.cpp

+ 1 - 1
Userland/Utilities/CMakeLists.txt

@@ -92,7 +92,7 @@ target_link_libraries(disasm PRIVATE LibX86)
 target_link_libraries(expr PRIVATE LibRegex)
 target_link_libraries(fdtdump PRIVATE LibDeviceTree)
 target_link_libraries(file PRIVATE LibGfx LibIPC LibArchive LibCompress LibAudio)
-target_link_libraries(find PRIVATE LibRegex)
+target_link_libraries(find PRIVATE LibFileSystem LibRegex)
 target_link_libraries(functrace PRIVATE LibDebug LibX86)
 target_link_libraries(glsl-compiler PRIVATE LibGLSL)
 target_link_libraries(gml-format PRIVATE LibGUI)

+ 19 - 1
Userland/Utilities/find.cpp

@@ -10,9 +10,11 @@
 #include <AK/NonnullOwnPtr.h>
 #include <AK/OwnPtr.h>
 #include <AK/Time.h>
+#include <AK/URL.h>
 #include <AK/Vector.h>
 #include <LibCore/DirIterator.h>
 #include <LibCore/System.h>
+#include <LibFileSystem/FileSystem.h>
 #include <LibMain/Main.h>
 #include <LibRegex/Regex.h>
 #include <dirent.h>
@@ -30,6 +32,7 @@
 bool g_follow_symlinks = false;
 bool g_there_was_an_error = false;
 bool g_have_seen_action_command = false;
+bool g_print_hyperlinks = false;
 
 template<typename... Parameters>
 [[noreturn]] static void fatal_error(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
@@ -409,7 +412,20 @@ public:
 private:
     virtual bool evaluate(FileData& file_data) const override
     {
-        out("{}{}", file_data.full_path, m_terminator);
+        auto printed = false;
+        if (g_print_hyperlinks) {
+            auto full_path_or_error = FileSystem::real_path(file_data.full_path.string());
+            if (!full_path_or_error.is_error()) {
+                auto fullpath = full_path_or_error.release_value();
+                auto url = URL::create_with_file_scheme(fullpath.to_deprecated_string());
+                out("\033]8;;{}\033\\{}{}\033]8;;\033\\", url.serialize(), file_data.full_path, m_terminator);
+                printed = true;
+            }
+        }
+
+        if (!printed)
+            out("{}{}", file_data.full_path, m_terminator);
+
         return true;
     }
 
@@ -739,6 +755,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
         }
     }
 
+    g_print_hyperlinks = TRY(Core::System::isatty(STDOUT_FILENO));
+
     if (!command)
         command = make<PrintCommand>();