mirror of
https://github.com/beefytech/Beef.git
synced 2025-07-04 15:26:00 +02:00
Moving LogViewer into BeefTools
This commit is contained in:
parent
65bf1915af
commit
8a02874b51
10 changed files with 2 additions and 232 deletions
249
BeefTools/LogViewer/src/Board.bf
Normal file
249
BeefTools/LogViewer/src/Board.bf
Normal file
|
@ -0,0 +1,249 @@
|
|||
using Beefy.widgets;
|
||||
using Beefy.gfx;
|
||||
using Beefy.theme.dark;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Beefy.utils;
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace LogViewer
|
||||
{
|
||||
class Board : Widget
|
||||
{
|
||||
public struct Match
|
||||
{
|
||||
public int32 mFilterIdx;
|
||||
public int32 mTextIdx;
|
||||
}
|
||||
|
||||
public DarkEditWidget mDocEdit;
|
||||
public DarkEditWidget mFilterEdit;
|
||||
public String mContent ~ delete _;
|
||||
public StatusBar mStatusBar;
|
||||
|
||||
public String mFilter = new String() ~ delete _;
|
||||
public List<int32> mFilterLengths = new List<int32>() ~ delete _;
|
||||
public int mFilterDirtyCountdown = 1;
|
||||
|
||||
public String mNewContent ~ delete _;
|
||||
public List<Match> mNewMatches ~ delete _;
|
||||
public bool mRefreshing;
|
||||
|
||||
public uint32[] mColors = new .(
|
||||
0xFFFFFFFF,
|
||||
0xFFFC5858,
|
||||
0xFF00FF00,
|
||||
0xFF6E67FF,
|
||||
0xFFFF00FF,
|
||||
0xFFFFFF00,
|
||||
0xFF00FFFF,
|
||||
0XFFFFBD67
|
||||
) ~ delete _;
|
||||
|
||||
public this()
|
||||
{
|
||||
|
||||
mStatusBar = new StatusBar();
|
||||
AddWidget(mStatusBar);
|
||||
|
||||
mDocEdit = new DarkEditWidget();
|
||||
var ewc = (DarkEditWidgetContent)mDocEdit.mEditWidgetContent;
|
||||
ewc.mIsMultiline = true;
|
||||
ewc.mFont = gApp.mFont;
|
||||
ewc.mWordWrap = false;
|
||||
ewc.mTextColors = mColors;
|
||||
mDocEdit.InitScrollbars(true, true);
|
||||
AddWidget(mDocEdit);
|
||||
|
||||
mFilterEdit = new DarkEditWidget();
|
||||
ewc = (DarkEditWidgetContent)mFilterEdit.mEditWidgetContent;
|
||||
ewc.mIsMultiline = true;
|
||||
ewc.mFont = gApp.mFont;
|
||||
ewc.mWordWrap = false;
|
||||
ewc.mTextColors = mColors;
|
||||
mFilterEdit.InitScrollbars(true, true);
|
||||
AddWidget(mFilterEdit);
|
||||
}
|
||||
|
||||
int GetColorIdx(int idx)
|
||||
{
|
||||
return (idx % (mColors.Count - 1)) + 1;
|
||||
}
|
||||
|
||||
uint32 GetColor(int idx)
|
||||
{
|
||||
return mColors[(idx % (mColors.Count - 1)) + 1];
|
||||
}
|
||||
|
||||
public void Load(StringView filePath)
|
||||
{
|
||||
scope AutoBeefPerf("Board.Load");
|
||||
|
||||
delete mContent;
|
||||
mContent = new String();
|
||||
|
||||
let result = File.ReadAllText(filePath, mContent, false);
|
||||
if (result case .Err)
|
||||
{
|
||||
gApp.Fail("Failed to open file '{0}'", filePath);
|
||||
}
|
||||
//mDocEdit.SetText(mContent);
|
||||
}
|
||||
|
||||
void Refresh()
|
||||
{
|
||||
scope AutoBeefPerf("Board.Refresh");
|
||||
|
||||
//let profileId = Profiler.StartSampling(Thread.CurrentThread).GetValueOrDefault();
|
||||
|
||||
delete mNewContent;
|
||||
mNewContent = new String();
|
||||
delete mNewMatches;
|
||||
mNewMatches = new List<Match>();
|
||||
|
||||
let filters = mFilter.Split!('\n');
|
||||
|
||||
for (var line in mContent.Split('\n'))
|
||||
{
|
||||
bool hadMatch = false;
|
||||
bool hadFilter = false;
|
||||
|
||||
for (var filter in filters)
|
||||
{
|
||||
if (filter.Length == 0)
|
||||
continue;
|
||||
hadFilter = true;
|
||||
int lastIdx = -1;
|
||||
while (true)
|
||||
{
|
||||
int findIdx = line.IndexOf(filter, lastIdx + 1);
|
||||
if (findIdx == -1)
|
||||
break;
|
||||
|
||||
hadMatch = true;
|
||||
lastIdx = findIdx + filter.Length - 1;
|
||||
|
||||
Match match;
|
||||
match.mFilterIdx = (.)@filter;
|
||||
match.mTextIdx = (.)(mNewContent.Length + findIdx);
|
||||
mNewMatches.Add(match);
|
||||
}
|
||||
}
|
||||
|
||||
if ((hadMatch) || (!hadFilter))
|
||||
{
|
||||
mNewContent.Append(line);
|
||||
mNewContent.Append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
mRefreshing = false;
|
||||
|
||||
/*if (profileId != 0)
|
||||
profileId.Dispose();*/
|
||||
}
|
||||
|
||||
public override void Draw(Graphics g)
|
||||
{
|
||||
base.Draw(g);
|
||||
|
||||
//using (g.PushColor(0xFFE02040))
|
||||
//g.FillRect(0, 0, mWidth, mHeight);
|
||||
|
||||
g.DrawBox(DarkTheme.sDarkTheme.GetImage(.Bkg), 0, 0, mWidth, mHeight);
|
||||
}
|
||||
|
||||
public override void DrawAll(Graphics g)
|
||||
{
|
||||
base.DrawAll(g);
|
||||
|
||||
/*g.SetFont(gApp.mFont);
|
||||
g.DrawString(scope String()..AppendF("FPS: {0}", gApp.mLastFPS), 0, 0);*/
|
||||
}
|
||||
|
||||
void ResizeComponents()
|
||||
{
|
||||
float statusBarHeight = 20;
|
||||
|
||||
float findHeight = 140;
|
||||
mDocEdit.Resize(0, 0, mWidth, mHeight - findHeight - statusBarHeight);
|
||||
mFilterEdit.Resize(0, mHeight - findHeight - statusBarHeight, mWidth, findHeight);
|
||||
|
||||
mStatusBar.Resize(0, mHeight - statusBarHeight, mWidth, statusBarHeight);
|
||||
}
|
||||
|
||||
public override void Resize(float x, float y, float width, float height)
|
||||
{
|
||||
base.Resize(x, y, width, height);
|
||||
ResizeComponents();
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
if (mRefreshing)
|
||||
{
|
||||
// Just wait...
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mNewContent != null)
|
||||
{
|
||||
var ewc = (DarkEditWidgetContent)mFilterEdit.mEditWidgetContent;
|
||||
for (int lineIdx < ewc.GetLineCount())
|
||||
{
|
||||
int colorIdx = GetColorIdx(lineIdx);
|
||||
ewc.GetLinePosition(lineIdx, let lineStart, let lineEnd);
|
||||
for (int i = lineStart; i < lineEnd; i++)
|
||||
{
|
||||
ewc.mData.mText[i].mDisplayTypeId = (.)colorIdx;
|
||||
}
|
||||
}
|
||||
|
||||
ewc = (DarkEditWidgetContent)mDocEdit.mEditWidgetContent;
|
||||
mDocEdit.SetText(mNewContent);
|
||||
for (let match in mNewMatches)
|
||||
{
|
||||
int colorIdx = GetColorIdx(match.mFilterIdx);
|
||||
int matchLength = mFilterLengths[match.mFilterIdx];
|
||||
|
||||
for (int i = match.mTextIdx; i < match.mTextIdx + matchLength; i++)
|
||||
{
|
||||
ewc.mData.mText[i].mDisplayTypeId = (.)colorIdx;
|
||||
}
|
||||
}
|
||||
|
||||
DeleteAndNullify!(mNewContent);
|
||||
DeleteAndNullify!(mNewMatches);
|
||||
}
|
||||
|
||||
var filter = scope String();
|
||||
mFilterEdit.GetText(filter);
|
||||
|
||||
if (filter != mFilter)
|
||||
{
|
||||
mFilter.Set(filter);
|
||||
mFilterDirtyCountdown = 5;
|
||||
}
|
||||
|
||||
mFilterLengths.Clear();
|
||||
for (let filterLine in mFilter.Split('\n'))
|
||||
{
|
||||
mFilterLengths.Add((int32)filterLine.Length);
|
||||
}
|
||||
|
||||
if ((mFilterDirtyCountdown > 0) && (gApp.mIsUpdateBatchStart))
|
||||
{
|
||||
if (--mFilterDirtyCountdown == 0)
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(new => Refresh);
|
||||
mRefreshing = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
109
BeefTools/LogViewer/src/LVApp.bf
Normal file
109
BeefTools/LogViewer/src/LVApp.bf
Normal file
|
@ -0,0 +1,109 @@
|
|||
using Beefy;
|
||||
using Beefy.widgets;
|
||||
using Beefy.theme.dark;
|
||||
using Beefy.theme;
|
||||
using Beefy.gfx;
|
||||
using System;
|
||||
using Beefy.utils;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
|
||||
namespace LogViewer
|
||||
{
|
||||
class LVApp : BFApp
|
||||
{
|
||||
public WidgetWindow mMainWindow;
|
||||
public Board mBoard;
|
||||
public Font mFont ~ delete _;
|
||||
|
||||
public this()
|
||||
{
|
||||
gApp = this;
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
|
||||
var dialog = scope OpenFileDialog();
|
||||
dialog.SetFilter("All files (*.*)|*.*");
|
||||
dialog.InitialDirectory = mInstallDir;
|
||||
dialog.Title = "Open Log";
|
||||
let result = dialog.ShowDialog();
|
||||
if ((result case .Err) || (dialog.FileNames.Count == 0))
|
||||
{
|
||||
Stop();
|
||||
return;
|
||||
}
|
||||
|
||||
BeefPerf.Init("127.0.0.1", "LogViewer");
|
||||
|
||||
DarkTheme darkTheme = new DarkTheme();
|
||||
darkTheme.Init();
|
||||
ThemeFactory.mDefault = darkTheme;
|
||||
|
||||
BFWindow.Flags windowFlags = BFWindow.Flags.Border | //BFWindow.Flags.SysMenu | //| BFWindow.Flags.CaptureMediaKeys |
|
||||
BFWindow.Flags.Caption | BFWindow.Flags.Minimize | BFWindow.Flags.QuitOnClose | BFWindowBase.Flags.Resizable |
|
||||
BFWindow.Flags.SysMenu;
|
||||
|
||||
mFont = new Font();
|
||||
float fontSize = 12;
|
||||
mFont.Load(scope String(BFApp.sApp.mInstallDir, "fonts/SourceCodePro-Regular.ttf"), fontSize);
|
||||
mFont.AddAlternate("Segoe UI Symbol", fontSize);
|
||||
mFont.AddAlternate("Segoe UI Historic", fontSize);
|
||||
mFont.AddAlternate("Segoe UI Emoji", fontSize);
|
||||
|
||||
mBoard = new Board();
|
||||
mBoard.Load(dialog.FileNames[0]);
|
||||
mMainWindow = new WidgetWindow(null, "LogViewer", 0, 0, 1600, 1200, windowFlags, mBoard);
|
||||
//mMainWindow.mWindowKeyDownDelegate.Add(new => SysKeyDown);
|
||||
mMainWindow.SetMinimumSize(480, 360);
|
||||
mMainWindow.mIsMainWindow = true;
|
||||
}
|
||||
|
||||
public void Fail(String str, params Object[] paramVals)
|
||||
{
|
||||
var errStr = scope String();
|
||||
errStr.AppendF(str, paramVals);
|
||||
Fail(errStr);
|
||||
}
|
||||
|
||||
public void Fail(String text)
|
||||
{
|
||||
#if CLI
|
||||
Console.WriteLine("ERROR: {0}", text);
|
||||
return;
|
||||
#endif
|
||||
|
||||
#unwarn
|
||||
//Debug.Assert(Thread.CurrentThread == mMainThread);
|
||||
|
||||
if (mMainWindow == null)
|
||||
{
|
||||
//Internal.FatalError(StackStringFormat!("FAILED: {0}", text));
|
||||
Windows.MessageBoxA(0, text, "FATAL ERROR", 0);
|
||||
return;
|
||||
}
|
||||
|
||||
//Beep(MessageBeepType.Error);
|
||||
|
||||
Dialog dialog = ThemeFactory.mDefault.CreateDialog("ERROR", text, DarkTheme.sDarkTheme.mIconError);
|
||||
dialog.mDefaultButton = dialog.AddButton("OK");
|
||||
dialog.mEscButton = dialog.mDefaultButton;
|
||||
dialog.PopupWindow(mMainWindow);
|
||||
|
||||
/*if (addWidget != null)
|
||||
{
|
||||
dialog.AddWidget(addWidget);
|
||||
addWidget.mY = dialog.mHeight - 60;
|
||||
addWidget.mX = 90;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
{
|
||||
static LVApp gApp;
|
||||
}
|
||||
}
|
22
BeefTools/LogViewer/src/Program.bf
Normal file
22
BeefTools/LogViewer/src/Program.bf
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
|
||||
namespace LogViewer
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static int32 Main(String[] args)
|
||||
{
|
||||
/*String commandLine = scope String();
|
||||
commandLine.JoinInto(" ", args);*/
|
||||
|
||||
LVApp app = new .();
|
||||
app.ParseCommandLine(args);
|
||||
app.Init();
|
||||
app.Run();
|
||||
app.Shutdown();
|
||||
delete app;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
26
BeefTools/LogViewer/src/StatusBar.bf
Normal file
26
BeefTools/LogViewer/src/StatusBar.bf
Normal file
|
@ -0,0 +1,26 @@
|
|||
using Beefy.widgets;
|
||||
using Beefy.gfx;
|
||||
|
||||
namespace LogViewer
|
||||
{
|
||||
class StatusBar : Widget
|
||||
{
|
||||
public override void Draw(Graphics g)
|
||||
{
|
||||
base.Draw(g);
|
||||
|
||||
g.SetFont(gApp.mFont);
|
||||
|
||||
uint32 bkgColor = 0xFF404040;
|
||||
using (g.PushColor(bkgColor))
|
||||
g.FillRect(0, 0, mWidth, mHeight);
|
||||
|
||||
var lineAndColumn = gApp.mBoard.mDocEdit.mEditWidgetContent.CursorLineAndColumn;
|
||||
g.DrawString(StackStringFormat!("Ln {0}", lineAndColumn.mLine + 1), mWidth - 160, 2);
|
||||
g.DrawString(StackStringFormat!("Col {0}", lineAndColumn.mColumn + 1), mWidth - 80, 2);
|
||||
|
||||
if ((gApp.mBoard.mFilterDirtyCountdown != 0) || (gApp.mBoard.mRefreshing))
|
||||
g.DrawString("Refreshing", 8, 2);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue