From 59b177dfb2fee342c32103026a342371431a72ea Mon Sep 17 00:00:00 2001 From: Shy Date: Tue, 27 Aug 2024 22:40:09 +0200 Subject: [PATCH] Added log size option --- README.md | 2 +- c4mate | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2d0a903..a97930c 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ value of the $USER environment variable as username. Buy by item id (non-interactive): c4mate -b|--buy Show log: - c4mate -l|--log + c4mate -l|--log [] Add or subtract credits: c4mate Give credits: diff --git a/c4mate b/c4mate index 85366cc..5439b19 100755 --- a/c4mate +++ b/c4mate @@ -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 Show log: - c4mate -l|--log + c4mate -l|--log [] Add or subtract credits: c4mate +/- 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