Skip to content

Commit

Permalink
feat (log): format header so it aligns with the log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-george-field committed Aug 23, 2023
1 parent 7d38488 commit dd4253a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/wfh-log-wpf/Entity/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace wfh_log_wpf.Models
{
public class LogEntry
{
public string ConnectedNetwork { get; set; }
public bool IsWorkingFromHome { get; set; } = false;
public DateTime Time { get; set; } = DateTime.Now.ToLocalTime();
public bool IsWorkingFromHome { get; set; } = false;
public string ConnectedNetwork { get; set; }

// needed for csv helper to load headers
public LogEntry() { }
Expand Down
6 changes: 2 additions & 4 deletions src/wfh-log-wpf/Entity/LogEntryPretty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public class LogEntryPretty

public LogEntryPretty(LogEntry entry)
{
IsWorkingFromHome = entry.IsWorkingFromHome
? " " + entry.IsWorkingFromHome.ToString() + " "
: " " + entry.IsWorkingFromHome.ToString();
ConnectedNetwork = " " + entry.ConnectedNetwork.ToString();
Time = entry.Time;
IsWorkingFromHome = "\t" + entry.IsWorkingFromHome.ToString();
ConnectedNetwork = "\t\t\t" + entry.ConnectedNetwork.ToString();
}
}
}
27 changes: 20 additions & 7 deletions src/wfh-log-wpf/Logger/LogReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CsvHelper;
using CsvHelper.Configuration;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand All @@ -11,20 +12,32 @@ public class LogReader : BaseLog
{
public LogReader()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
ShouldQuote = (args) => false
};

if (!File.Exists(AbsoluteFilePath))
{
using (var writer = new StreamWriter(AbsoluteFilePath))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteHeader<LogEntry>();
csv.NextRecord();
writer.WriteLine("Time\t\t\tIsWorkingFromHome\tNetwork");
}
}

using (var reader = new StreamReader(AbsoluteFilePath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
}
else
{
_logs = csv.GetRecords<LogEntry>().ToList();
using (var reader = new StreamReader(AbsoluteFilePath))
using (var csv = new CsvReader(reader, config))
{
csv.Read();

while(csv.Read())
{
_logs.Add(csv.GetRecord<LogEntry>());
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wfh-log-wpf/Logger/LogWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Log(bool isWorkingFromHome, string connectedNetworkName)

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false, // don't write the header twice
HasHeaderRecord = false,
ShouldQuote = (args) => false
};

Expand Down

0 comments on commit dd4253a

Please sign in to comment.