This commit is contained in:
f0ldspace
2025-10-04 12:02:00 +01:00
parent cf43844a50
commit ccd42b30a2
3 changed files with 163 additions and 1 deletions

View File

@@ -1,2 +1,47 @@
# fatebook.nvim
# Fatebook.nvim
A Neovim plugin for creating predictions on [Fatebook](https://fatebook.io).
## Features
- Create predictions directly from Neovim with a simple command
- Automatic date format conversion (DD-MM-YYYY)
- Percentage-based forecasts (0-100)
- Success/error notifications
## Installation
### LazyVim / lazy.nvim
Add this to your LazyVim config (e.g., `~/.config/nvim/lua/plugins/fatebook.lua`):
```lua
return {
"f0ldspace/fatebook.nvim"
config = function()
require("fatebook").setup({
api_key = "your-api-key-here"
})
end,
}
```
## Usage
Use the `:Predict` command to create predictions:
```vim
:Predict "Will this work?", 55, 04-10-2025
```
### Format
```vim
:Predict "question", forecast, DD-MM-YYYY
```
- **Question**: Text in quotes (or without quotes if no commas in the question)
- **Forecast**: Number between 0-100 (percentage probability)
- **Date**: DD-MM-YYYY format (e.g., 31-12-2025)

89
lua/fatebook/init.lua Normal file
View File

@@ -0,0 +1,89 @@
local M = {}
M.config = {
api_key = nil,
}
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
if not M.config.api_key then
vim.notify("Fatebook: no api", vim.log.levels.ERROR)
end
end
local function url_encode(str)
if str then
str = string.gsub(str, "\n", "\r\n")
str = string.gsub(str, "([^%w%-%.%_%~ ])", function(c)
return string.format("%%%02X", string.byte(c))
end)
str = string.gsub(str, " ", "+")
end
return str
end
local function parse_date(date_str)
-- Parse DD-MM-YYYY to YYYY-MM-DD
local day, month, year = date_str:match("(%d+)%-(%d+)%-(%d+)")
if not day or not month or not year then
return nil, "Invalid date format. Use DD-MM-YYYY"
end
return string.format("%s-%s-%s", year, month, day)
end
function M.create_prediction(question, forecast, date)
if not M.config.api_key then
vim.notify("Fatebook: No API", vim.log.levels.ERROR)
return
end
local forecast_num = tonumber(forecast)
if not forecast_num or forecast_num < 0 or forecast_num > 100 then
vim.notify("Fatebook: Forecast issue", vim.log.levels.ERROR)
return
end
-- percent to decimal
local forecast_decimal = forecast_num / 100
-- Parse date
local resolve_by, err = parse_date(date)
if not resolve_by then
vim.notify("Fatebook: " .. err, vim.log.levels.ERROR)
return
end
-- all to one
local url = string.format(
"https://fatebook.io/api/v0/createQuestion?apiKey=%s&title=%s&resolveBy=%s&forecast=%s",
url_encode(M.config.api_key),
url_encode(question),
url_encode(resolve_by),
url_encode(tostring(forecast_decimal))
)
-- api call
local cmd = string.format("curl -s -X GET '%s'", url)
vim.fn.jobstart(cmd, {
on_exit = function(_, exit_code)
if exit_code == 0 then
vim.schedule(function()
vim.notify(
string.format("Fatebook: Created prediction '%s' (%d%%)", question, forecast_num),
vim.log.levels.INFO
)
end)
else
vim.schedule(function()
vim.notify("Fatebook: Failed to create prediction", vim.log.levels.ERROR)
end)
end
end,
stdout_buffered = true,
stderr_buffered = true,
})
end
return M

28
plugin/fatebook.lua Normal file
View File

@@ -0,0 +1,28 @@
if vim.g.loaded_fatebook then
return
end
vim.g.loaded_fatebook = true
vim.api.nvim_create_user_command("Predict", function(opts)
local fatebook = require("fatebook")
local args = opts.args
local question, forecast, date = args:match('^"([^"]+)"%s*,%s*(%d+)%s*,%s*([%d%-]+)$')
if not question or not forecast or not date then
question, forecast, date = args:match("^([^,]+),%s*(%d+)%s*,%s*([%d%-]+)$")
end
if not question or not forecast or not date then
vim.notify('Fatebook: Invalid format. Use :Predict "question", 55, DD-MM-YYYY', vim.log.levels.ERROR)
return
end
question = question:match("^%s*(.-)%s*$")
fatebook.create_prediction(question, forecast, date)
end, {
nargs = 1,
desc = "Create a Fatebook prediction",
})