ConfigurationEditor.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #nullable enable
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("Configuration Editor", "Edits Terminal.Gui Config Files.")]
  9. [ScenarioCategory ("TabView")]
  10. [ScenarioCategory ("Colors")]
  11. [ScenarioCategory ("Files and IO")]
  12. [ScenarioCategory ("TextView")]
  13. public class ConfigurationEditor : Scenario
  14. {
  15. private static ColorScheme _editorColorScheme = new ()
  16. {
  17. Normal = new Attribute (Color.Red, Color.White),
  18. Focus = new Attribute (Color.Red, Color.Black),
  19. HotFocus = new Attribute (Color.BrightRed, Color.Black),
  20. HotNormal = new Attribute (Color.Magenta, Color.White)
  21. };
  22. private static Action? _editorColorSchemeChanged;
  23. private TabView? _tabView;
  24. private Shortcut? _lenShortcut;
  25. [SerializableConfigurationProperty (Scope = typeof (AppScope))]
  26. public static ColorScheme EditorColorScheme
  27. {
  28. get => _editorColorScheme;
  29. set
  30. {
  31. _editorColorScheme = value;
  32. _editorColorSchemeChanged?.Invoke ();
  33. }
  34. }
  35. public override void Main ()
  36. {
  37. Application.Init ();
  38. Toplevel top = new ();
  39. _lenShortcut = new Shortcut ()
  40. {
  41. Title = "",
  42. };
  43. var quitShortcut = new Shortcut ()
  44. {
  45. Key = Application.QuitKey,
  46. Title = $"Quit",
  47. Action = Quit
  48. };
  49. var reloadShortcut = new Shortcut ()
  50. {
  51. Key = Key.F5.WithShift,
  52. Title = "Reload",
  53. };
  54. reloadShortcut.Accepting += (s, e) => { Reload (); };
  55. var saveShortcut = new Shortcut ()
  56. {
  57. Key = Key.F4,
  58. Title = "Save",
  59. Action = Save
  60. };
  61. var statusBar = new StatusBar ([quitShortcut, reloadShortcut, saveShortcut, _lenShortcut]);
  62. _tabView = new ()
  63. {
  64. Width = Dim.Fill (),
  65. Height = Dim.Fill (Dim.Func (() => statusBar.Frame.Height))
  66. };
  67. top.Add (_tabView, statusBar);
  68. top.Loaded += (s, a) =>
  69. {
  70. Open ();
  71. _editorColorSchemeChanged?.Invoke ();
  72. };
  73. void OnEditorColorSchemeChanged ()
  74. {
  75. if (Application.Top is { })
  76. {
  77. return;
  78. }
  79. foreach (ConfigTextView t in _tabView.Subviews.Where (v => v is ConfigTextView).Cast<ConfigTextView> ())
  80. {
  81. t.ColorScheme = EditorColorScheme;
  82. }
  83. }
  84. _editorColorSchemeChanged += OnEditorColorSchemeChanged;
  85. Application.Run (top);
  86. _editorColorSchemeChanged -= OnEditorColorSchemeChanged;
  87. top.Dispose ();
  88. Application.Shutdown ();
  89. }
  90. public void Save ()
  91. {
  92. if (Application.Navigation?.GetFocused () is ConfigTextView editor)
  93. {
  94. editor.Save ();
  95. }
  96. }
  97. private void Open ()
  98. {
  99. foreach (var config in ConfigurationManager.Settings!.Sources)
  100. {
  101. var homeDir = $"{Environment.GetFolderPath (Environment.SpecialFolder.UserProfile)}";
  102. var fileInfo = new FileInfo (config.Value.Replace ("~", homeDir));
  103. var editor = new ConfigTextView
  104. {
  105. Title = config.Value.StartsWith ("resource://") ? fileInfo.Name : config.Value,
  106. Width = Dim.Fill (),
  107. Height = Dim.Fill(),
  108. FileInfo = fileInfo,
  109. };
  110. Tab tab = new Tab ()
  111. {
  112. View = editor,
  113. DisplayText = config.Key.ToString ()
  114. };
  115. _tabView!.AddTab (tab, false);
  116. editor.Read ();
  117. editor.ContentsChanged += (sender, args) =>
  118. {
  119. _lenShortcut!.Title = _lenShortcut!.Title.Replace ("*", "");
  120. if (editor.IsDirty)
  121. {
  122. _lenShortcut!.Title += "*";
  123. }
  124. };
  125. _lenShortcut!.Title = $"{editor.Title}";
  126. }
  127. _tabView!.SelectedTabChanged += (sender, args) =>
  128. {
  129. _lenShortcut!.Title = $"{args.NewTab.View!.Title}";
  130. };
  131. }
  132. private void Quit ()
  133. {
  134. foreach (ConfigTextView editor in _tabView!.Tabs.Select(v =>
  135. {
  136. if (v.View is ConfigTextView ctv)
  137. {
  138. return ctv;
  139. }
  140. return null;
  141. }).Cast<ConfigTextView> ())
  142. {
  143. if (editor.IsDirty)
  144. {
  145. int result = MessageBox.Query (
  146. "Save Changes",
  147. $"Save changes to {editor.FileInfo!.Name}",
  148. "_Yes",
  149. "_No",
  150. "_Cancel"
  151. );
  152. if (result == -1 || result == 2)
  153. {
  154. // user cancelled
  155. }
  156. if (result == 0)
  157. {
  158. editor.Save ();
  159. }
  160. }
  161. }
  162. Application.RequestStop ();
  163. }
  164. private void Reload ()
  165. {
  166. if (Application.Navigation?.GetFocused () is ConfigTextView editor)
  167. {
  168. editor.Read ();
  169. }
  170. }
  171. private class ConfigTextView : TextView
  172. {
  173. internal ConfigTextView ()
  174. {
  175. TabStop = TabBehavior.TabGroup;
  176. }
  177. internal FileInfo? FileInfo { get; set; }
  178. internal void Read ()
  179. {
  180. Assembly? assembly = null;
  181. if (FileInfo!.FullName.Contains ("[Terminal.Gui]"))
  182. {
  183. // Library resources
  184. assembly = typeof (ConfigurationManager).Assembly;
  185. }
  186. else if (FileInfo.FullName.Contains ("[UICatalog]"))
  187. {
  188. assembly = Assembly.GetEntryAssembly ();
  189. }
  190. if (assembly != null)
  191. {
  192. string? name = assembly
  193. .GetManifestResourceNames ()
  194. .FirstOrDefault (x => x.EndsWith ("config.json"));
  195. if (!string.IsNullOrEmpty (name))
  196. {
  197. using Stream? stream = assembly.GetManifestResourceStream (name);
  198. using var reader = new StreamReader (stream!);
  199. Text = reader.ReadToEnd ();
  200. ReadOnly = true;
  201. Enabled = true;
  202. }
  203. return;
  204. }
  205. if (FileInfo!.FullName.Contains ("RuntimeConfig"))
  206. {
  207. Text = ConfigurationManager.RuntimeConfig!;
  208. } else if (!FileInfo.Exists)
  209. {
  210. // Create empty config file
  211. Text = ConfigurationManager.GetEmptyJson ();
  212. }
  213. else
  214. {
  215. Text = File.ReadAllText (FileInfo.FullName);
  216. }
  217. }
  218. internal void Save ()
  219. {
  220. if (FileInfo!.FullName.Contains ("RuntimeConfig"))
  221. {
  222. ConfigurationManager.RuntimeConfig = Text;
  223. IsDirty = false;
  224. return;
  225. }
  226. if (!Directory.Exists (FileInfo.DirectoryName))
  227. {
  228. // Create dir
  229. Directory.CreateDirectory (FileInfo.DirectoryName!);
  230. }
  231. using StreamWriter writer = File.CreateText (FileInfo.FullName);
  232. writer.Write (Text);
  233. writer.Close ();
  234. IsDirty = false;
  235. }
  236. }
  237. }