From f1cccbdd4601c3c0b1a7bffb6f9d752a3ebc80d6 Mon Sep 17 00:00:00 2001 From: Mitch Capper Date: Sun, 22 May 2022 13:48:28 -0700 Subject: [PATCH] Add OSC 8 link generation support and file link helpers --- TrueColorConsole/VTConsole.cs | 63 ++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/TrueColorConsole/VTConsole.cs b/TrueColorConsole/VTConsole.cs index 75ff896..314ef67 100644 --- a/TrueColorConsole/VTConsole.cs +++ b/TrueColorConsole/VTConsole.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; @@ -405,6 +405,61 @@ public static void WriteConcat(params object[] objects) Console.Write(string.Concat(objects)); } - #endregion - } -} \ No newline at end of file + + /// + /// Writes a file hyperlink to the console (if it supports OSC 8 sequences) + /// + /// + /// Title to show for the link + /// + /// + /// The link, normally https:// http:// file:// + /// + [PublicAPI] + public static void WriteFileLink(String title, String file_path) { + Console.Write(GetFileLinkStr(title, file_path)); + } + /// + /// Writes a hyperlink to the console (if it supports OSC 8 sequences) + /// + /// + /// Title to show for the link + /// + /// + /// The link, normally https:// http:// file:// + /// + [PublicAPI] + public static void WriteLink(String title, String link) { + Console.Write(GetLinkStr(title,link)); + } + /// + /// Get a hyperlink string sequence in OSC 8 format + /// + /// + /// Title to show for the link + /// + /// + /// Path to the file + /// + [PublicAPI] + public static string GetFileLinkStr(String title, String file_path) { + return $"{LINK}{new Uri(new Uri("file://"), file_path).AbsoluteUri}{ST}{title}{LINK}{ST}";//this file:// hinting is required for linux + } + /// + /// Get a hyperlink string sequence in OSC 8 format + /// + /// + /// Title to show for the link + /// + /// + /// The link, normally https:// http:// file:// + /// + [PublicAPI] + public static string GetLinkStr(String title, String link) { + return $"{LINK}{link}{ST}{title}{LINK}{ST}"; + } + private const string LINK = ESC + "]8;;"; + private const string ST = ESC + "\\"; + #endregion + } +}