docs/refactor: add AI disclosure and clean up code style

This commit is contained in:
2026-04-18 00:00:45 +02:00
parent 4f2ce0349c
commit 56ccfe63b6
5 changed files with 50 additions and 31 deletions

View File

@@ -53,3 +53,10 @@ Place a `.jasper_config.json` file at the root of the project (next to `.git`):
4. The timer is **started**. A libuv countdown is armed; after `inactivity_timeout` minutes of silence the timer is **paused**.
5. Any cursor movement or keystroke resets the countdown (and resumes the timer if it was paused).
6. On `VimLeave` the timer is **stopped**.
$
## AI Disclosure
This project was written entirely with the assistance of
[GitHub Copilot](https://github.com/features/copilot) using the
**Claude Sonnet 4.6** model.

View File

@@ -12,9 +12,6 @@ local JASPER_URL = "https://jasper.4sigma.it"
--- @return string|nil error message
local function curl(args)
-- Append -w to get the HTTP status on a separate last line
local full_args = vim.list_extend(vim.list_slice(args, 1), { "-s", "-w", "\n%{http_code}" })
-- args already contains the URL; rebuild properly:
-- Actually we receive the full arg list already. Add -w trick.
table.insert(args, "-w")
table.insert(args, "\n%{http_code}")
@@ -41,7 +38,8 @@ end
local function get(path, token)
return curl({
"curl",
"-H", "Authorization: Token " .. token,
"-H",
"Authorization: Token " .. token,
JASPER_URL .. path,
})
end
@@ -52,8 +50,11 @@ end
--- @return table|nil, number, string|nil
local function post(path, token, form)
local args = {
"curl", "-X", "POST",
"-H", "Authorization: Token " .. token,
"curl",
"-X",
"POST",
"-H",
"Authorization: Token " .. token,
}
if form then
for k, v in pairs(form) do
@@ -84,6 +85,9 @@ function M.get_timers(token)
if status ~= 200 then
return nil, "unexpected HTTP status " .. status
end
if not data then
return nil, "empty response"
end
local timers_dict = data.data and data.data.timers
if type(timers_dict) ~= "table" then
return nil, "unexpected response shape"
@@ -108,6 +112,9 @@ function M.start_timer(token, timer_id)
if status ~= 200 then
return false, "HTTP " .. status
end
if not data then
return true, nil
end
if data.error ~= nil and data.error ~= vim.NIL then
return false, tostring(data.error)
end
@@ -127,6 +134,9 @@ function M.stop_timer(token, timer_id)
if status ~= 200 then
return false, "HTTP " .. status
end
if not data then
return true, nil
end
if data.error ~= nil and data.error ~= vim.NIL then
return false, tostring(data.error)
end
@@ -140,17 +150,16 @@ end
--- @return boolean ok
--- @return string|nil error
function M.create_timer(token, timer_id, attivita_id)
local data, status, err = post(
"/api/v1/timer/" .. timer_id .. "/attivita/",
token,
{ attivita_id = attivita_id }
)
local data, status, err = post("/api/v1/timer/" .. timer_id .. "/attivita/", token, { attivita_id = attivita_id })
if err then
return false, err
end
if status ~= 200 then
return false, "HTTP " .. status
end
if not data then
return true, nil
end
if data.error ~= nil and data.error ~= vim.NIL then
return false, tostring(data.error)
end

View File

@@ -47,9 +47,12 @@ end
--- @return string|nil error message
function M.login(username, password)
local result = vim.system({
"curl", "-s",
"--data-urlencode", "username=" .. username,
"--data-urlencode", "password=" .. password,
"curl",
"-s",
"--data-urlencode",
"username=" .. username,
"--data-urlencode",
"password=" .. password,
JASPER_URL .. "/api/v1/token/login/",
}, { text = true }):wait()

View File

@@ -36,19 +36,14 @@ function M.setup(opts)
end
if not project_cfg.attivita_id then
vim.notify(
"[Jasper] .jasper_config.json found but 'attivita_id' is missing",
vim.log.levels.WARN
)
vim.notify("[Jasper] .jasper_config.json found but 'attivita_id' is missing", vim.log.levels.WARN)
return
end
-- Merge: project file values < global setup() opts (global opts take priority)
local effective_opts = {
attivita_id = project_cfg.attivita_id,
inactivity_timeout = opts.inactivity_timeout
or project_cfg.inactivity_timeout
or 10,
inactivity_timeout = opts.inactivity_timeout or project_cfg.inactivity_timeout or 10,
}
timer.setup(effective_opts)

View File

@@ -14,7 +14,7 @@ local state = {
running = false,
-- inactivity_ms: milliseconds of silence before the timer is auto-paused
inactivity_ms = 10 * 60 * 1000,
uv_timer = nil, ---@type uv_timer_t|nil
uv_timer = nil, ---@type uv.uv_timer_t|nil
augroup = nil, ---@type number|nil
}
@@ -170,6 +170,11 @@ local function on_token_ready(token)
return
end
if not timers then
notify("Cannot fetch timers: empty response", vim.log.levels.ERROR)
return
end
local timer_id, terr = find_or_create_timer(token, state.attivita_id, timers)
if not timer_id then
notify(terr or "Cannot get timer", vim.log.levels.ERROR)