From e16173cbcc2e07a13aaf426a6447161631ef50ff Mon Sep 17 00:00:00 2001 From: Justaphf Date: Tue, 10 Apr 2018 23:15:36 -0400 Subject: [PATCH 1/2] Add configuration option to display line number prefix 1. UI implementation for specifying the option per-session 2. UI implementation for specifying this option should apply to all open sessions 3. Persistence for saving this option per-session and as the global default --- SnakeTail/EventLogForm.cs | 3 +++ SnakeTail/TailConfig.cs | 1 + SnakeTail/TailConfigApplyAllForm.Designer.cs | 23 +++++++++++++++----- SnakeTail/TailConfigForm.Designer.cs | 16 +++++++++++--- SnakeTail/TailConfigForm.cs | 2 ++ SnakeTail/TailForm.cs | 5 +++++ 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/SnakeTail/EventLogForm.cs b/SnakeTail/EventLogForm.cs index ba48f1d..2a4ada1 100644 --- a/SnakeTail/EventLogForm.cs +++ b/SnakeTail/EventLogForm.cs @@ -38,6 +38,7 @@ partial class EventLogForm : Form, ITailForm bool _displayTabIcon; bool _topItemIndexHack = false; bool _formMinimizedAtBottom = false; + bool _displayLineNumbers = false; public EventLogForm() { @@ -89,6 +90,7 @@ public void SaveConfig(TailFileConfig tailConfig) } tailConfig.ColumnFilterActive = _filterActive; tailConfig.DisplayTabIcon = _displayTabIcon; + tailConfig.DisplayLineNumbers = _displayLineNumbers; } public void LoadConfig(TailFileConfig tailConfig, string configPath) @@ -161,6 +163,7 @@ public void LoadConfig(TailFileConfig tailConfig, string configPath) _filterActive = tailConfig.ColumnFilterActive; _displayTabIcon = tailConfig.DisplayTabIcon; + _displayLineNumbers = tailConfig.DisplayLineNumbers; if (Visible) { diff --git a/SnakeTail/TailConfig.cs b/SnakeTail/TailConfig.cs index 17eff5d..c2d7f87 100644 --- a/SnakeTail/TailConfig.cs +++ b/SnakeTail/TailConfig.cs @@ -191,6 +191,7 @@ public class TailFileConfig public string ServiceName { get; set; } public string IconFile { get; set; } public bool DisplayTabIcon { get; set; } + public bool DisplayLineNumbers { get; set; } public bool ColumnFilterActive { get; set; } [XmlArray("ColumnFilters")] [XmlArrayItem("Filters")] diff --git a/SnakeTail/TailConfigApplyAllForm.Designer.cs b/SnakeTail/TailConfigApplyAllForm.Designer.cs index 1e43a6c..241bf27 100644 --- a/SnakeTail/TailConfigApplyAllForm.Designer.cs +++ b/SnakeTail/TailConfigApplyAllForm.Designer.cs @@ -49,6 +49,7 @@ private void InitializeComponent() this._buttonOk = new System.Windows.Forms.Button(); this._buttonCancel = new System.Windows.Forms.Button(); this._checkboxTools = new System.Windows.Forms.CheckBox(); + this._checkBoxLineNumbers = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // _checkBoxColors @@ -74,7 +75,7 @@ private void InitializeComponent() // _checkboxKeywords // this._checkboxKeywords.AutoSize = true; - this._checkboxKeywords.Location = new System.Drawing.Point(12, 58); + this._checkboxKeywords.Location = new System.Drawing.Point(12, 81); this._checkboxKeywords.Name = "_checkboxKeywords"; this._checkboxKeywords.Size = new System.Drawing.Size(111, 17); this._checkboxKeywords.TabIndex = 2; @@ -85,7 +86,7 @@ private void InitializeComponent() // this._buttonOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this._buttonOk.DialogResult = System.Windows.Forms.DialogResult.OK; - this._buttonOk.Location = new System.Drawing.Point(75, 107); + this._buttonOk.Location = new System.Drawing.Point(75, 136); this._buttonOk.Name = "_buttonOk"; this._buttonOk.Size = new System.Drawing.Size(75, 23); this._buttonOk.TabIndex = 4; @@ -96,7 +97,7 @@ private void InitializeComponent() // this._buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this._buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this._buttonCancel.Location = new System.Drawing.Point(165, 107); + this._buttonCancel.Location = new System.Drawing.Point(165, 136); this._buttonCancel.Name = "_buttonCancel"; this._buttonCancel.Size = new System.Drawing.Size(75, 23); this._buttonCancel.TabIndex = 5; @@ -106,20 +107,31 @@ private void InitializeComponent() // _checkboxTools // this._checkboxTools.AutoSize = true; - this._checkboxTools.Location = new System.Drawing.Point(12, 81); + this._checkboxTools.Location = new System.Drawing.Point(12, 104); this._checkboxTools.Name = "_checkboxTools"; this._checkboxTools.Size = new System.Drawing.Size(93, 17); this._checkboxTools.TabIndex = 3; this._checkboxTools.Text = "External Tools"; this._checkboxTools.UseVisualStyleBackColor = true; // + // _checkBoxLineNumbers + // + this._checkBoxLineNumbers.AutoSize = true; + this._checkBoxLineNumbers.Location = new System.Drawing.Point(12, 58); + this._checkBoxLineNumbers.Name = "_checkBoxLineNumbers"; + this._checkBoxLineNumbers.Size = new System.Drawing.Size(128, 17); + this._checkBoxLineNumbers.TabIndex = 6; + this._checkBoxLineNumbers.Text = "Display Line Numbers"; + this._checkBoxLineNumbers.UseVisualStyleBackColor = true; + // // TailConfigApplyAllForm // this.AcceptButton = this._buttonOk; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this._buttonCancel; - this.ClientSize = new System.Drawing.Size(252, 142); + this.ClientSize = new System.Drawing.Size(252, 171); + this.Controls.Add(this._checkBoxLineNumbers); this.Controls.Add(this._checkboxTools); this.Controls.Add(this._buttonCancel); this.Controls.Add(this._buttonOk); @@ -147,5 +159,6 @@ private void InitializeComponent() private System.Windows.Forms.Button _buttonOk; private System.Windows.Forms.Button _buttonCancel; public System.Windows.Forms.CheckBox _checkboxTools; + public System.Windows.Forms.CheckBox _checkBoxLineNumbers; } } \ No newline at end of file diff --git a/SnakeTail/TailConfigForm.Designer.cs b/SnakeTail/TailConfigForm.Designer.cs index bf264c0..0866dcc 100644 --- a/SnakeTail/TailConfigForm.Designer.cs +++ b/SnakeTail/TailConfigForm.Designer.cs @@ -96,6 +96,7 @@ private void InitializeComponent() this._cancelBtn = new System.Windows.Forms.Button(); this._applyAllBtn = new System.Windows.Forms.Button(); this._saveDefaultBtn = new System.Windows.Forms.Button(); + this._displayLineNumbersChk = new System.Windows.Forms.CheckBox(); label2 = new System.Windows.Forms.Label(); label1 = new System.Windows.Forms.Label(); label3 = new System.Windows.Forms.Label(); @@ -254,6 +255,7 @@ private void InitializeComponent() // // _tabPageView // + this._tabPageView.Controls.Add(this._displayLineNumbersChk); this._tabPageView.Controls.Add(this._bookmarkBackColorBtn); this._tabPageView.Controls.Add(this._bookmarkTextColorBtn); this._tabPageView.Controls.Add(this._displayIconChk); @@ -652,6 +654,16 @@ private void InitializeComponent() this._saveDefaultBtn.UseVisualStyleBackColor = true; this._saveDefaultBtn.Click += new System.EventHandler(this._saveDefaultBtn_Click); // + // _displayLineNumbersChk + // + this._displayLineNumbersChk.AutoSize = true; + this._displayLineNumbersChk.Location = new System.Drawing.Point(11, 161); + this._displayLineNumbersChk.Name = "_displayLineNumbersChk"; + this._displayLineNumbersChk.Size = new System.Drawing.Size(122, 17); + this._displayLineNumbersChk.TabIndex = 17; + this._displayLineNumbersChk.Text = "Display line numbers"; + this._displayLineNumbersChk.UseVisualStyleBackColor = true; + // // TailConfigForm // this.AcceptButton = this._acceptBtn; @@ -723,8 +735,6 @@ private void InitializeComponent() private System.Windows.Forms.Button _bookmarkTextColorBtn; private System.Windows.Forms.Button _bookmarkBackColorBtn; private System.Windows.Forms.Button _saveDefaultBtn; - - - + private System.Windows.Forms.CheckBox _displayLineNumbersChk; } } \ No newline at end of file diff --git a/SnakeTail/TailConfigForm.cs b/SnakeTail/TailConfigForm.cs index 3d6582e..8c2b158 100644 --- a/SnakeTail/TailConfigForm.cs +++ b/SnakeTail/TailConfigForm.cs @@ -63,6 +63,7 @@ private void TailConfigForm_Load(object sender, EventArgs e) _windowTitleEdt.Text = TailFileConfig.Title; _windowIconEdt.Text = TailFileConfig.IconFile; _displayIconChk.Checked = TailFileConfig.DisplayTabIcon; + _displayLineNumbersChk.Checked = TailFileConfig.DisplayLineNumbers; if (_displayFileTab) { @@ -121,6 +122,7 @@ private void TailConfigForm_Save(object sender, EventArgs e) TailFileConfig.Title = _windowTitleEdt.Text; TailFileConfig.IconFile = _windowIconEdt.Text; TailFileConfig.DisplayTabIcon = _displayIconChk.Checked; + TailFileConfig.DisplayLineNumbers = _displayLineNumbersChk.Checked; if (_displayFileTab) { diff --git a/SnakeTail/TailForm.cs b/SnakeTail/TailForm.cs index fb9c0ee..c8bea67 100644 --- a/SnakeTail/TailForm.cs +++ b/SnakeTail/TailForm.cs @@ -96,6 +96,7 @@ partial class TailForm : Form, ITailForm List _keywordHighlight; int _loghitCounter = -1; bool _displayTabIcon = false; + bool _displayLineNumbers = false; List _externalTools; Color _bookmarkTextColor = Color.Yellow; // Default bookmark text color Color _bookmarkBackColor = Color.DarkGreen; // Default bookmark background color @@ -285,6 +286,7 @@ public void LoadConfig(TailFileConfig tailConfig, string configPath) UpdateFormTitle(true); _displayTabIcon = tailConfig.DisplayTabIcon; + _displayLineNumbers = tailConfig.DisplayLineNumbers; if (!string.IsNullOrEmpty(tailConfig.IconFile)) { @@ -547,6 +549,7 @@ public void SaveConfig(TailFileConfig tailConfig) tailConfig.ServiceName = ""; tailConfig.DisplayTabIcon = _displayTabIcon; + tailConfig.DisplayLineNumbers = _displayLineNumbers; } public void CopySelectionToClipboard() @@ -1547,6 +1550,8 @@ private void configureViewToolStripMenuItem_Click(object sender, EventArgs e) } if (configFormApply._checkBoxFont.Checked) configFileOther.FontInvariant = configFile.FontInvariant; + if (configFormApply._checkBoxLineNumbers.Checked) + configFileOther.DisplayLineNumbers = configFile.DisplayLineNumbers; if (configFormApply._checkboxKeywords.Checked) configFileOther.KeywordHighlight = configFile.KeywordHighlight; if (configFormApply._checkboxTools.Checked) From 3b609698341f92b8f579c2eb7ab150acfc504bae Mon Sep 17 00:00:00 2001 From: Justaphf Date: Tue, 10 Apr 2018 23:25:56 -0400 Subject: [PATCH 2/2] Implement drawing of line numbers Optionally draws left padded line numbers based on user configuration to display line numbers --- SnakeTail/TailForm.cs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/SnakeTail/TailForm.cs b/SnakeTail/TailForm.cs index c8bea67..ca6f526 100644 --- a/SnakeTail/TailForm.cs +++ b/SnakeTail/TailForm.cs @@ -103,6 +103,11 @@ partial class TailForm : Form, ITailForm List _bookmarks = new List(); ThreadPoolQueue _threadPoolQueue = null; + const int MAX_LINE_WIDTH = 1000; + const int LINE_NUMBER_WIDTH = 10; // Support maximum Int32 representable number 2^32 (~2 billion) + const string LINE_NUMBER_SEPARATOR = ": "; // Divider between line number and line content + readonly int MAX_LINE_WIDTH_WITH_LINE_NUMBER = MAX_LINE_WIDTH - (LINE_NUMBER_WIDTH + LINE_NUMBER_SEPARATOR.Length); + public TailForm() { InitializeComponent(); @@ -943,10 +948,28 @@ private void _tailListView_DrawSubItem(object sender, DrawListViewSubItemEventAr e.DrawFocusRectangle(e.Bounds); TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.ExpandTabs | TextFormatFlags.SingleLine | TextFormatFlags.NoPrefix; - if (e.Item.Text.Length > 1000) - TextRenderer.DrawText(e.Graphics, e.Item.Text.Substring(0, 1000), e.Item.ListView.Font, e.Bounds, e.SubItem.ForeColor, flags); - else - TextRenderer.DrawText(e.Graphics, e.Item.Text, e.Item.ListView.Font, e.Bounds, e.SubItem.ForeColor, flags); + // Integer line number (+1 for non-zero index) + string text = ConstructLineForRendering(e.Item.Text, e.ItemIndex + 1); + TextRenderer.DrawText(e.Graphics, text, e.Item.ListView.Font, e.Bounds, e.SubItem.ForeColor, flags); + } + + /// + /// Constructs a single line for drawing purposes + /// Optionally allows display of line number prefix + /// + /// A single line for rendering + /// Line number in monitored file (1-indexed) + /// Include line number in text rendering string + /// + private string ConstructLineForRendering(string rawText, int lineNumber) + { + int maxLength = _displayLineNumbers ? MAX_LINE_WIDTH_WITH_LINE_NUMBER : MAX_LINE_WIDTH; + string truncText = rawText.Length > maxLength ? rawText.Substring(0, maxLength) : rawText; + + if (_displayLineNumbers) + // Left pad 10, coverage of 2^32 potential line numbers (~2 billion for Int32) + return string.Format("{0,10:N0}{1}{2}", lineNumber, LINE_NUMBER_SEPARATOR, truncText); + return truncText; } private bool MatchesBookmark(int lineNumber)