Added log size option

This commit is contained in:
Shy 2024-08-27 22:40:09 +02:00
parent 3c2ddf20e1
commit 59b177dfb2
2 changed files with 12 additions and 10 deletions

View file

@ -20,7 +20,7 @@ value of the $USER environment variable as username.
Buy by item id (non-interactive):
c4mate -b|--buy <id>
Show log:
c4mate -l|--log
c4mate -l|--log [<size>]
Add or subtract credits:
c4mate <difference>
Give credits:

20
c4mate
View file

@ -92,15 +92,15 @@ function MatePad:update_balance()
end
-- Parse history.
function MatePad:parse_log()
function MatePad:parse_log(size)
local pipe, ok, exit, signal
local datefile = os.tmpname()
local log = {}
-- sed #1: Insert newlines between log entries.
-- sed #2: Extract time and write it into datefile. Then extract and print
-- oldbalance, newbalance, parameter and method. Depending on method, print
-- either reason or item_name. If any extraction fails we fail with exit
-- code 1.
-- sed #2: Quit after $size lines. Extract time and write it into datefile.
-- Then extract and print oldbalance, newbalance, parameter and method.
-- Depending on method, print either reason or item_name. If any extraction
-- fails we fail with exit code 1.
pipe = io.popen(string.format([=[
curl %s --header "Accept: application/json" "%s/api/user/%s/log?type=json" | \
sed -n --sandbox '
@ -109,6 +109,7 @@ sed -n --sandbox '
s/} *,/&\n/gp
}' | \
sed -n '
%d q
h
s/.*"time" *: *"\([^"]\+\)".*/\1UTC/
T fail
@ -136,7 +137,7 @@ sed -n '
T fail
b
:fail
q 1 ']=], self.CURL_FLAGS, self.HOST, url_encode(self.USER), datefile))
q 1 ']=], self.CURL_FLAGS, self.HOST, url_encode(self.USER), size+1, datefile))
while true do
local entry = {}
@ -273,7 +274,7 @@ end
-- Return id of last bought item in log.
function MatePad:last_bought()
local log = self:parse_log()
local log = self:parse_log(50)
for i = 1, #log do
if log[i].method == "buy" then
return log[i].parameter
@ -315,7 +316,7 @@ Find and buy item (interactive):
Buy by item id (non-interactive, special id '0' will re-buy last item in log):
c4mate -b|--buy <id>
Show log:
c4mate -l|--log
c4mate -l|--log [<size>]
Add or subtract credits:
c4mate +/-<amount>
Give credits:
@ -351,7 +352,8 @@ if arg[1] == nil then
elseif arg[1] == "-l" or
arg[1] == "--log" then
-- Print log.
pad:print_log(pad:parse_log())
local size = math.max(0, math.tointeger(arg[2]) or 10)
pad:print_log(pad:parse_log(size))
elseif arg[1] == "-g" or
arg[1] == "--give" then