Skip to content

Commit

Permalink
Split flightlog export functionality from the gui code and move it to…
Browse files Browse the repository at this point in the history
… the module

Only export personal information when writing a log, don't show it on the gui ever.

Remove the utils set class, it didn't add value
  • Loading branch information
JonBooth78 committed Oct 6, 2023
1 parent 786928a commit 334718b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 192 deletions.
38 changes: 0 additions & 38 deletions data/libs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -780,44 +780,6 @@ utils.getFromIntervals = function(array, value)
return array[utils.getIndexFromIntervals(array, value)][1]
end

-- a simple set class
utils.set = utils.class( "utils.set" )

-- Constructor
-- Build a set
-- Parameters:
--
-- array - an array, every element of which will be added to the set. If not provided the set starts empty
-- number - some value in intervals
function utils.set:Constructor( array )
self.set = {}
if nil ~= array then
for _, value in pairs( array ) do
self.set[value] = true
end
end
end

function utils.set:add( value )
self.set[value] = true
end

function utils.set:remove( value )
self.set[value] = nil
end

function utils.set:contains( value )
return self.set[value] ~= nil
end

function utils.set:to_array()
local array = {}
for v, _ in pairs( self.set ) do
array[#array+1] = v
end
return array
end

return utils


48 changes: 14 additions & 34 deletions data/modules/FlightLog/FlightLog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,44 +104,24 @@ FlightLog = {
function FlightLog.SkipFirstDocking()
skip_first_docking = true
end
--
-- Method: GetLogEntries
--
-- Parameters:
--
-- types - An array of the types we want to fetch, nil if all of them
-- maximum - the maximum number of results to return
-- Return:
--
-- iterator - A function which will generate the entries from the
-- log, returning one each time it is called until it
-- runs out, after which it returns nil. Each entry is
-- a child class of LogEntry
--
-- Example:
--
-- > for entry in FlightLog.GetLogEntries( { "Custom", "System", "Station" ) do
-- > print( entry.GetType(), entry.entry )
-- > end
function FlightLog:GetLogEntries(types, maximum, earliest_first)

-- TODO: actually just store a list of all of them as they are at startup
local type_set = utils.set.New(types)

-- note regardless of sort order, current status always comes first.
local currentStatus = nil
if nil == types or type_set:contains( "CurrentStatus" ) then
currentStatus = FlightLogEntry.CurrentStatus.New()
end
--- Method: GetLogEntries
---@param types table[string,boolean]|nil Keys are log types to include, set the boolean to true for the ones you want
---@param maximum integer|nil Maximum number of entries to include
---@param earliest_first boolean|nil Should the log start with the oldest entry or the most recent
---
---@return function():FlightLogEntry.Base An iterator function that when called repeatedly returns the next entry or nil when complete
---
--- Example:
---
--- > for entry in FlightLog.GetLogEntries( { "Custom", "System", "Station" ) do
--- > print( entry.GetType(), entry.entry )
--- > end
function FlightLog:GetLogEntries(types, maximum, earliest_first)

local counter = 0
maximum = maximum or #FlightLogData
return function ()
if currentStatus then
local t = currentStatus
currentStatus = nil
return t
end
while counter < maximum do
counter = counter + 1

Expand All @@ -153,7 +133,7 @@ function FlightLog:GetLogEntries(types, maximum, earliest_first)
end
-- TODO: Can we map the types to serialization indexes and check these
-- as they may be faster than the string manipulation comapare stuff.
if nil == types or type_set:contains( v:GetType() ) then
if nil == types or types[ v:GetType() ] then
return v
end
end
Expand Down
126 changes: 6 additions & 120 deletions data/pigui/modules/info-view/06-flightlog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ local ui = require 'pigui'
local InfoView = require 'pigui.views.info-view'
local Lang = require 'Lang'
local FlightLog = require 'modules.FlightLog.FlightLog'
local FlightLogExporter = require 'modules.FlightLog.FlightLogExporter'
local Format = require 'Format'
local Color = _G.Color
local Vector2 = _G.Vector2
local FileSystem = require 'FileSystem'
local Character = require 'Character'

local pionillium = ui.fonts.pionillium
local icons = ui.theme.icons
Expand All @@ -21,21 +20,17 @@ local l = Lang.GetResource("ui-core")
local iconSize = ui.rescaleUI(Vector2(28, 28))
local buttonSpaceSize = iconSize


local include_player_info = true
local include_custom_log = true
local include_station_log = true
local include_system_log = true
local earliest_first = false
local export_html = true

local function getIncludedSet()
o = {}
if include_player_info then table.insert(o, "CurrentStatus") end
if include_custom_log then table.insert(o, "Custom") end
if include_station_log then table.insert(o, "Station") end
if include_system_log then table.insert(o, "System") end

local o = {}
if include_custom_log then o["Custom"] = true end
if include_station_log then o["Station"] = true end
if include_system_log then o["System"] = true end
return o;
end

Expand Down Expand Up @@ -74,78 +69,6 @@ function ui_formatter:newline()
return self
end

local text_formatter = {}

function text_formatter:open( file )
self.file = file
return self
end

function text_formatter:write( string )
self.file:write( string )
return self
end

function text_formatter:newline()
self.file:write( "\n" )
return self
end

function text_formatter:close()
self.file:close()
end

function text_formatter:headerText(title, text, wrap)
-- Title text is gray, followed by the variable text:
if not text then return end
self:write( string.gsub(title, ":", "") ):write( ": " )

-- TODO wrap?
self:write( text ):newline()
return self
end

function text_formatter:separator()
self.file:write( "\n----------------------------------------------\n\n" )
end

local html_formatter = {}

function html_formatter:write( string )
self.file:write( string )
return self
end

function html_formatter:open( file )
self.file = file
self.file:write( "<html>\n" )
return self
end

function html_formatter:newline()
self.file:write( "<br>\n" )
return self
end

function html_formatter:close()
self.file:write( "</html>" )
self.file:close()
end

function html_formatter:headerText(title, text, wrap)
-- Title text is gray, followed by the variable text:
if not text then return end
self:write( "<b>" ):write( string.gsub(title, ":", "") ):write( ": </b>" )

-- TODO wrap?
self:write( text ):newline()
return self
end

function html_formatter:separator()
self.file:write( "\n<hr>\n" )
end

entering_text = false

-- Display Entry text, and Edit button, to update flightlog
Expand Down Expand Up @@ -236,48 +159,11 @@ local function checkbox(label, checked, tooltip)
return changed, ret
end

local function exportLogs()
-- TODO localize?
local foldername = FileSystem.MakeUserDataDirectory( "player_logs" )

local player = Character.persistent.player

local base_save_name = player.name

local formatter
local extension
if export_html then
formatter = html_formatter
extension = '.html'
else
formatter = text_formatter
extension = '.log'
end

local log_filename = FileSystem.JoinPath( foldername, base_save_name .. extension )

formatter:open( io.open( log_filename, "w" ) )

for entry in FlightLog:GetLogEntries(getIncludedSet(),nil, earliest_first) do

writeLogEntry(entry, formatter, true)

if (entry:HasEntry()) then
formatter:headerText(l.ENTRY, entry:GetEntry(), true)
end
formatter:separator()
end

formatter:close()

end

local function displayFilterOptions()
ui.spacing()
local c
local flight_log = true;

c,include_player_info = checkbox(l.PERSONAL_INFORMATION, include_player_info)
c,include_custom_log = checkbox(l.LOG_CUSTOM, include_custom_log)
c,include_station_log = checkbox(l.LOG_STATION, include_station_log)
c,include_system_log = checkbox(l.LOG_SYSTEM, include_system_log)
Expand All @@ -290,7 +176,7 @@ local function displayFilterOptions()
ui.spacing()

if ui.button(l.SAVE) then
exportLogs()
FlightLogExporter.Export( getIncludedSet(), earliest_first, true, export_html)
end

end
Expand Down

0 comments on commit 334718b

Please sign in to comment.