• R/O
  • SSH
  • HTTPS

ktx: Commit


Commit MetaInfo

Revision33 (tree)
Zeit2018-06-07 19:16:15
Autorbananajinn

Log Message

キーワードハイライト機能を追加

Ändern Zusammenfassung

Diff

--- trunk/ktx/Settings.cs (revision 32)
+++ trunk/ktx/Settings.cs (revision 33)
@@ -2,6 +2,7 @@
22 * Copyright (C) 2017 BananaJinn
33 */
44 using System;
5+using System.Drawing;
56 using System.Text.RegularExpressions;
67
78 namespace ktox
@@ -14,6 +15,15 @@
1415 public string Editor { get; set; }
1516 public string Font { get; set; }
1617 public double OpacityValue { get; set; }
18+ public string HighlightKeywords { get; set; }
19+ public string[] HighlightKeywordArray {
20+ get {
21+ return !string.IsNullOrEmpty(HighlightKeywords)
22+ ? HighlightKeywords.Split(' ')
23+ : null;
24+ }
25+ }
26+ public Color HighlightColor { get; set; }
1727
1828 public Settings()
1929 {
@@ -21,10 +31,12 @@
2131
2232 public override string ToString()
2333 {
24- return string.Format("editor:'{0}',font:'{1}',opacity:{2}",
34+ return string.Format("editor:'{0}',font:'{1}',opacity:{2},highlightKeywords:'{3}',highlightColor:'{4}'",
2535 Editor,
2636 Font,
27- (int)(OpacityValue*100));
37+ (int)(OpacityValue*100),
38+ HighlightKeywords.Trim(),
39+ ColorTranslator.ToHtml(HighlightColor));
2840 }
2941
3042 public static Settings Parse(string value)
@@ -37,6 +49,10 @@
3749 if(match.Success) result.Font = match.Groups[1].Value;
3850 match = Regex.Match(value, "opacity:([0-9]+)");
3951 if(match.Success) result.OpacityValue = (double)(int.Parse(match.Groups[1].Value))/100;
52+ match = Regex.Match(value, "highlightKeywords:'(.*?)'");
53+ if(match.Success) result.HighlightKeywords = match.Groups[1].Value;
54+ match = Regex.Match(value, "highlightColor:'(.*?)'");
55+ if(match.Success) result.HighlightColor = ColorTranslator.FromHtml(match.Groups[1].Value);
4056 }
4157 return result;
4258 }
@@ -46,7 +62,9 @@
4662 return new Settings {
4763 Editor = "notepad",
4864 Font = "MS UI Gothic",
49- OpacityValue = 1.0
65+ OpacityValue = 1.0,
66+ HighlightKeywords = "ERROR",
67+ HighlightColor = Color.Red
5068 };
5169 }
5270
--- trunk/ktx/Views/MainForm.cs (revision 32)
+++ trunk/ktx/Views/MainForm.cs (revision 33)
@@ -22,7 +22,7 @@
2222 /// </summary>
2323 public partial class MainForm : Form
2424 {
25- [DllImport("user32.dll", CharSet=CharSet.Auto)]
25+ [DllImport("user32.dll", CharSet = CharSet.Auto)]
2626 public static extern Int32 SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
2727
2828 [DllImport("gdi32.dll")]
@@ -50,6 +50,7 @@
5050 private Bitmap _miniMap;
5151 private bool _miniMapMouseDown = false;
5252 private string _lastError;
53+ private Settings _settings;
5354
5455 public string Path {
5556 get { return _path; }
@@ -109,7 +110,7 @@
109110 {
110111 _keyHandlers.Clear();
111112 _keyHandlers.Add('o', OpenRecent);
112- if(Path != null){
113+ if (Path != null) {
113114 _keyHandlers.Add('e', Edit);
114115 _keyHandlers.Add('f', OpenFolder);
115116 }
@@ -122,9 +123,10 @@
122123
123124 public void ApplySettings(Settings settings)
124125 {
125- if(settings != null){
126+ if (settings != null) {
126127 richTextBox1.Font = new Font(settings.Font, 9);
127128 Opacity = settings.OpacityValue;
129+ _settings = settings;
128130 }
129131 }
130132
@@ -139,28 +141,26 @@
139141
140142 private void Read()
141143 {
142- if(_watcher != null){
144+ if (_watcher != null) {
143145 try {
144146 var lines = _watcher.Read();
145- if(lines == null){
147+ if (lines == null) {
146148 richTextBox1.Clear();
147149 LineCount = 0;
148150 lines = _watcher.Read();
149151 }
150- if(lines != null && lines.Count > 0){
152+ if (lines != null && lines.Count > 0) {
151153 PrintLines(lines.ToArray());
152154 richTextBox1.Invalidate();
153- if(checkBoxFollow.Checked)
155+ if (checkBoxFollow.Checked)
154156 richTextBox1.ScrollToCaret();
155157 _waitMS = 100;
156158 _lastError = string.Empty;
159+ } else {
160+ _waitMS = Math.Min(_waitMS * 2, 1000);
157161 }
158- else{
159- _waitMS = Math.Min(_waitMS*2, 1000);
160- }
161- }
162- catch(Exception ex){
163- if(!ex.Message.Equals(_lastError)){
162+ } catch (Exception ex) {
163+ if (!ex.Message.Equals(_lastError)) {
164164 PrintLines(new string[]{ ex.Message });
165165 richTextBox1.Invalidate();
166166 _lastError = ex.Message;
@@ -172,8 +172,7 @@
172172 public void PrintLines(string[] lines)
173173 {
174174 BeginUpdate(richTextBox1);
175- if(richTextBox1.Lines.Length + lines.Length >= MaxLine + 100)
176- {
175+ if (richTextBox1.Lines.Length + lines.Length >= MaxLine + 100) {
177176 int count = richTextBox1.Lines.Length + lines.Length - MaxLine;
178177 richTextBox1.ReadOnly = false;
179178 richTextBox1.Focus();
@@ -181,7 +180,17 @@
181180 richTextBox1.SelectedText = "";
182181 richTextBox1.ReadOnly = true;
183182 }
184- richTextBox1.AppendText(string.Join("\n", lines)+"\n");
183+ string[] keywords = _settings.HighlightKeywordArray;
184+ foreach (var line in lines) {
185+ if (keywords.Any(line.Contains)) {
186+ richTextBox1.SelectionLength = 0;
187+ richTextBox1.SelectionColor = _settings.HighlightColor;
188+ richTextBox1.AppendText(line + "\n");
189+ richTextBox1.SelectionColor = richTextBox1.ForeColor;
190+ } else {
191+ richTextBox1.AppendText(line + "\n");
192+ }
193+ }
185194 EndUpdate(richTextBox1);
186195 UpdateMiniMap(lines);
187196 LineCount += lines.Length;
@@ -189,9 +198,9 @@
189198
190199 void UpdateMiniMap(string[] lines)
191200 {
192- if(pictureBox1.Visible && lines.Length > 0){
201+ if (pictureBox1.Visible && lines.Length > 0) {
193202 //DrawMiniMap(lines, LineCount);
194- var offset = richTextBox1.Lines.Length > 1000 ? richTextBox1.Lines.Length-1000 : 0;
203+ var offset = richTextBox1.Lines.Length > 1000 ? richTextBox1.Lines.Length - 1000 : 0;
195204 DrawMiniMap(richTextBox1.Lines.Skip(offset).ToArray(), 0);
196205 pictureBox1.Refresh();
197206 }
@@ -201,11 +210,11 @@
201210 {
202211 const int width = 100;
203212 const int height = 1000;
204- if(_miniMap == null) {
213+ if (_miniMap == null) {
205214 _miniMap = new Bitmap(width, height);
206215 pictureBox1.Image = _miniMap;
207216 }
208- var offset = richTextBox1.Lines.Length > 1000 ? richTextBox1.Lines.Length-1000 : 0;
217+ var offset = richTextBox1.Lines.Length > 1000 ? richTextBox1.Lines.Length - 1000 : 0;
209218 DrawMiniMap(richTextBox1.Lines.Skip(offset).ToArray(), 0);
210219 }
211220
@@ -212,26 +221,32 @@
212221 void DrawMiniMap(string[] lines, int offset)
213222 {
214223 char[] spaces = { ' ', '\t' };
215- using(var g = Graphics.FromImage(_miniMap)){
224+ string[] keywords = _settings.HighlightKeywordArray;
225+ var highlightBrush = new SolidBrush(_settings.HighlightColor);
226+ var highlightPen = new Pen(_settings.HighlightColor);
227+ using (var g = Graphics.FromImage(_miniMap)) {
216228 offset = Math.Min(offset, _miniMap.Height);
217229 var scrollLine = offset + lines.Length - _miniMap.Height;
218- if(scrollLine > 0){
230+ if (scrollLine > 0) {
219231 // TODO 画像をシフトさせるような何か
220232 offset -= scrollLine;
221233 }
222- g.FillRectangle(Brushes.Black, 0, offset, _miniMap.Width, _miniMap.Height-offset);
223- for(var index=0; index<lines.Length; index++){
234+ g.FillRectangle(Brushes.Black, 0, offset, _miniMap.Width, _miniMap.Height - offset);
235+ for (var index = 0; index < lines.Length; index++) {
224236 var line = lines[index];
237+ var highlight = keywords.Any(line.Contains);
225238 int spacePos = 0;
226- while(spacePos < line.Length && spacePos < _miniMap.Width){
239+ while (spacePos < line.Length && spacePos < _miniMap.Width) {
227240 var newPos = line.IndexOfAny(spaces, spacePos);
228- if(newPos < 0) newPos = line.Length;
229- if(newPos - 1 <= spacePos)
230- g.FillRectangle(Brushes.Gray, spacePos, offset+index, 1, 1);
241+ if (newPos < 0)
242+ newPos = line.Length;
243+ if (newPos - 1 <= spacePos)
244+ g.FillRectangle(highlight ? highlightBrush : Brushes.Gray, spacePos, offset + index, 1, 1);
231245 else
232- g.DrawLine(Pens.Gray, spacePos, offset+index, newPos-1, offset+index);
246+ g.DrawLine(highlight ? highlightPen : Pens.Gray, spacePos, offset + index, newPos - 1, offset + index);
233247 spacePos = newPos + 1;
234- while(spacePos < line.Length && line[spacePos]==' ') spacePos++;
248+ while (spacePos < line.Length && line[spacePos] == ' ')
249+ spacePos++;
235250 }
236251 }
237252 }
@@ -240,7 +255,7 @@
240255 void ScrollTo(double percent)
241256 {
242257 var line = (int)(MaxLine * percent);
243- if(line >= 0 && line < richTextBox1.Lines.Length) {
258+ if (line >= 0 && line < richTextBox1.Lines.Length) {
244259 var index = richTextBox1.GetFirstCharIndexFromLine(line);
245260 richTextBox1.Select(index, 0);
246261 richTextBox1.ScrollToCaret();
@@ -260,7 +275,7 @@
260275
261276 MainForm GetNewForm()
262277 {
263- if(timer1.Enabled){
278+ if (timer1.Enabled) {
264279 var form = new MainForm();
265280 form.Show();
266281 return form;
@@ -270,7 +285,7 @@
270285
271286 void NewFileTail(string file, Encoding encoding = null)
272287 {
273- if(encoding == null)
288+ if (encoding == null)
274289 encoding = FileEncodingDetector.GetEncoding(file) ?? Encoding.UTF8;
275290 var form = GetNewForm();
276291 form.FileEncoding = encoding;
@@ -285,10 +300,10 @@
285300
286301 void mainForm_DragDrop(object sender, DragEventArgs e)
287302 {
288- if(e.Data.GetDataPresent(DataFormats.FileDrop)){
303+ if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
289304 bool shiftKey = (e.KeyState & 0x04) != 0;
290305 var files = (string[])e.Data.GetData(DataFormats.FileDrop);
291- foreach(var file in files){
306+ foreach (var file in files) {
292307 NewFileTail(file, shiftKey ? Encoding.GetEncoding(932) : null);
293308 }
294309 }
@@ -302,7 +317,7 @@
302317 void MainFormFormClosed(object sender, FormClosedEventArgs e)
303318 {
304319 WindowManager.Remove(this);
305- if(WindowManager.Count() == 0){
320+ if (WindowManager.Count() == 0) {
306321 Application.Exit();
307322 }
308323 }
@@ -310,10 +325,10 @@
310325 {
311326 var files = Registries.GetRecentFiles();
312327 var dlg = new RecentFiles { Files = files };
313- if(dlg.ShowDialog() == DialogResult.OK && dlg.SelectedFile != null){
328+ if (dlg.ShowDialog() == DialogResult.OK && dlg.SelectedFile != null) {
314329 NewFileTail(dlg.SelectedFile.FilePath, dlg.SelectedFile.Encoding);
315330 }
316- if(dlg.DeletedFiles != null){
331+ if (dlg.DeletedFiles != null) {
317332 Registries.DeleteRecentFiles(dlg.DeletedFiles);
318333 }
319334 }
@@ -320,7 +335,7 @@
320335 void OpenEventLog()
321336 {
322337 var dlg = new EventLogSelector();
323- if(dlg.ShowDialog() != DialogResult.Cancel){
338+ if (dlg.ShowDialog() != DialogResult.Cancel) {
324339 NewEventLogTail(dlg.Selected.ToArray());
325340 }
326341 }
@@ -334,12 +349,16 @@
334349 var v = new Views.Settings {
335350 Editor = settings.Editor,
336351 FontName = settings.Font,
337- OpacityValue = settings.OpacityValue
352+ OpacityValue = settings.OpacityValue,
353+ HighlightKeywords = settings.HighlightKeywords,
354+ HighlightColor = settings.HighlightColor,
338355 };
339- if(v.ShowDialog() != DialogResult.Cancel){
356+ if (v.ShowDialog() != DialogResult.Cancel) {
340357 settings.Editor = v.Editor;
341358 settings.Font = v.FontName;
342359 settings.OpacityValue = v.OpacityValue;
360+ settings.HighlightKeywords = v.HighlightKeywords;
361+ settings.HighlightColor = v.HighlightColor;
343362 Registries.PutSettings(settings);
344363 WindowManager.ApplyNewSettings(settings);
345364 }
@@ -354,7 +373,7 @@
354373 }
355374 void ToggleMap()
356375 {
357- if(!pictureBox1.Visible){
376+ if (!pictureBox1.Visible) {
358377 CreateMiniMap();
359378 }
360379 pictureBox1.Visible = !pictureBox1.Visible;
@@ -385,17 +404,17 @@
385404 }
386405 void RichTextBox1KeyPress(object sender, KeyPressEventArgs e)
387406 {
388- if(HandleKey(e.KeyChar))
407+ if (HandleKey(e.KeyChar))
389408 e.Handled = true;
390409 }
391410 void MainFormKeyPress(object sender, KeyPressEventArgs e)
392411 {
393- if(HandleKey(e.KeyChar))
412+ if (HandleKey(e.KeyChar))
394413 e.Handled = true;
395414 }
396415 bool HandleKey(char keyChar)
397416 {
398- if(_keyHandlers.ContainsKey(keyChar)){
417+ if (_keyHandlers.ContainsKey(keyChar)) {
399418 _keyHandlers[keyChar]();
400419 return true;
401420 }
@@ -408,7 +427,7 @@
408427 }
409428 void PictureBox1MouseMove(object sender, MouseEventArgs e)
410429 {
411- if(_miniMapMouseDown)
430+ if (_miniMapMouseDown)
412431 ScrollTo((double)e.Y / pictureBox1.Height);
413432 }
414433 void PictureBox1MouseUp(object sender, MouseEventArgs e)
--- trunk/ktx/Views/Settings.Designer.cs (revision 32)
+++ trunk/ktx/Views/Settings.Designer.cs (revision 33)
@@ -19,6 +19,9 @@
1919 private System.Windows.Forms.LinkLabel linkClearRegistry;
2020 private System.Windows.Forms.TrackBar trackBar1;
2121 private System.Windows.Forms.Label label3;
22+ private System.Windows.Forms.Label label4;
23+ private System.Windows.Forms.TextBox textBox3;
24+ private System.Windows.Forms.Button buttonSelectHighlightColor;
2225
2326 /// <summary>
2427 /// Disposes resources used by the form.
@@ -51,6 +54,9 @@
5154 this.linkClearRegistry = new System.Windows.Forms.LinkLabel();
5255 this.trackBar1 = new System.Windows.Forms.TrackBar();
5356 this.label3 = new System.Windows.Forms.Label();
57+ this.label4 = new System.Windows.Forms.Label();
58+ this.textBox3 = new System.Windows.Forms.TextBox();
59+ this.buttonSelectHighlightColor = new System.Windows.Forms.Button();
5460 ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
5561 this.SuspendLayout();
5662 //
@@ -89,7 +95,7 @@
8995 // buttonOK
9096 //
9197 this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
92- this.buttonOK.Location = new System.Drawing.Point(279, 144);
98+ this.buttonOK.Location = new System.Drawing.Point(279, 198);
9399 this.buttonOK.Name = "buttonOK";
94100 this.buttonOK.Size = new System.Drawing.Size(75, 23);
95101 this.buttonOK.TabIndex = 8;
@@ -101,7 +107,7 @@
101107 //
102108 this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
103109 this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
104- this.buttonCancel.Location = new System.Drawing.Point(360, 144);
110+ this.buttonCancel.Location = new System.Drawing.Point(360, 198);
105111 this.buttonCancel.Name = "buttonCancel";
106112 this.buttonCancel.Size = new System.Drawing.Size(75, 23);
107113 this.buttonCancel.TabIndex = 9;
@@ -120,7 +126,7 @@
120126 //
121127 // linkClearRegistry
122128 //
123- this.linkClearRegistry.Location = new System.Drawing.Point(99, 111);
129+ this.linkClearRegistry.Location = new System.Drawing.Point(99, 141);
124130 this.linkClearRegistry.Name = "linkClearRegistry";
125131 this.linkClearRegistry.Size = new System.Drawing.Size(137, 15);
126132 this.linkClearRegistry.TabIndex = 7;
@@ -148,6 +154,31 @@
148154 this.label3.Text = "opacity";
149155 this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
150156 //
157+ // label4
158+ //
159+ this.label4.Location = new System.Drawing.Point(13, 98);
160+ this.label4.Name = "label4";
161+ this.label4.Size = new System.Drawing.Size(80, 23);
162+ this.label4.TabIndex = 10;
163+ this.label4.Text = "highlight";
164+ this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
165+ //
166+ // textBox3
167+ //
168+ this.textBox3.Location = new System.Drawing.Point(99, 100);
169+ this.textBox3.Name = "textBox3";
170+ this.textBox3.Size = new System.Drawing.Size(284, 19);
171+ this.textBox3.TabIndex = 11;
172+ //
173+ // buttonSelectHighlightColor
174+ //
175+ this.buttonSelectHighlightColor.Location = new System.Drawing.Point(389, 98);
176+ this.buttonSelectHighlightColor.Name = "buttonSelectHighlightColor";
177+ this.buttonSelectHighlightColor.Size = new System.Drawing.Size(30, 23);
178+ this.buttonSelectHighlightColor.TabIndex = 12;
179+ this.buttonSelectHighlightColor.UseVisualStyleBackColor = false;
180+ this.buttonSelectHighlightColor.Click += new System.EventHandler(this.ButtonSelectHighlightColorClick);
181+ //
151182 // Settings
152183 //
153184 this.AcceptButton = this.buttonOK;
@@ -154,7 +185,10 @@
154185 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
155186 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
156187 this.CancelButton = this.buttonCancel;
157- this.ClientSize = new System.Drawing.Size(447, 179);
188+ this.ClientSize = new System.Drawing.Size(447, 233);
189+ this.Controls.Add(this.buttonSelectHighlightColor);
190+ this.Controls.Add(this.textBox3);
191+ this.Controls.Add(this.label4);
158192 this.Controls.Add(this.label3);
159193 this.Controls.Add(this.trackBar1);
160194 this.Controls.Add(this.linkClearRegistry);
--- trunk/ktx/Views/Settings.cs (revision 32)
+++ trunk/ktx/Views/Settings.cs (revision 33)
@@ -26,6 +26,16 @@
2626 set { trackBar1.Value = (int)(value * trackBar1.Maximum); }
2727 }
2828
29+ public string HighlightKeywords {
30+ get { return textBox3.Text; }
31+ set { textBox3.Text = value; }
32+ }
33+
34+ public Color HighlightColor {
35+ get { return buttonSelectHighlightColor.BackColor; }
36+ set { buttonSelectHighlightColor.BackColor = value; }
37+ }
38+
2939 public Settings()
3040 {
3141 //
@@ -56,5 +66,14 @@
5666 Registries.ClearAllEntries();
5767 }
5868 }
69+
70+ void ButtonSelectHighlightColorClick(object sender, EventArgs e)
71+ {
72+ var cd = new ColorDialog();
73+ cd.Color = buttonSelectHighlightColor.BackColor;
74+ if(cd.ShowDialog() != DialogResult.Cancel){
75+ buttonSelectHighlightColor.BackColor = cd.Color;
76+ }
77+ }
5978 }
6079 }
Show on old repository browser