ConfigurationEditor.cs 8.0 KB

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