Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to display line numbers #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SnakeTail/EventLogForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ partial class EventLogForm : Form, ITailForm
bool _displayTabIcon;
bool _topItemIndexHack = false;
bool _formMinimizedAtBottom = false;
bool _displayLineNumbers = false;

public EventLogForm()
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -161,6 +163,7 @@ public void LoadConfig(TailFileConfig tailConfig, string configPath)

_filterActive = tailConfig.ColumnFilterActive;
_displayTabIcon = tailConfig.DisplayTabIcon;
_displayLineNumbers = tailConfig.DisplayLineNumbers;

if (Visible)
{
Expand Down
1 change: 1 addition & 0 deletions SnakeTail/TailConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
23 changes: 18 additions & 5 deletions SnakeTail/TailConfigApplyAllForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions SnakeTail/TailConfigForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions SnakeTail/TailConfigForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
36 changes: 32 additions & 4 deletions SnakeTail/TailForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ partial class TailForm : Form, ITailForm
List<TailKeywordConfig> _keywordHighlight;
int _loghitCounter = -1;
bool _displayTabIcon = false;
bool _displayLineNumbers = false;
List<ExternalToolConfig> _externalTools;
Color _bookmarkTextColor = Color.Yellow; // Default bookmark text color
Color _bookmarkBackColor = Color.DarkGreen; // Default bookmark background color
List<int> _bookmarks = new List<int>();
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();
Expand Down Expand Up @@ -285,6 +291,7 @@ public void LoadConfig(TailFileConfig tailConfig, string configPath)
UpdateFormTitle(true);

_displayTabIcon = tailConfig.DisplayTabIcon;
_displayLineNumbers = tailConfig.DisplayLineNumbers;

if (!string.IsNullOrEmpty(tailConfig.IconFile))
{
Expand Down Expand Up @@ -547,6 +554,7 @@ public void SaveConfig(TailFileConfig tailConfig)
tailConfig.ServiceName = "";

tailConfig.DisplayTabIcon = _displayTabIcon;
tailConfig.DisplayLineNumbers = _displayLineNumbers;
}

public void CopySelectionToClipboard()
Expand Down Expand Up @@ -940,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);
}

/// <summary>
/// Constructs a single line for drawing purposes
/// Optionally allows display of line number prefix
/// </summary>
/// <param name="rawText">A single line for rendering</param>
/// <param name="lineNumber">Line number in monitored file (1-indexed)</param>
/// <param name="bDisplayLineNumber">Include line number in text rendering string</param>
/// <returns></returns>
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)
Expand Down Expand Up @@ -1547,6 +1573,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)
Expand Down