From ccd42b30a20586d1d971cd322c95b5b617c865ac Mon Sep 17 00:00:00 2001 From: f0ldspace Date: Sat, 4 Oct 2025 12:02:00 +0100 Subject: [PATCH] init --- README.md | 47 ++++++++++++++++++++++- lua/fatebook/init.lua | 89 +++++++++++++++++++++++++++++++++++++++++++ plugin/fatebook.lua | 28 ++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 lua/fatebook/init.lua create mode 100644 plugin/fatebook.lua diff --git a/README.md b/README.md index 2a067b8..7b38dbd 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lua/fatebook/init.lua b/lua/fatebook/init.lua new file mode 100644 index 0000000..e9ab63f --- /dev/null +++ b/lua/fatebook/init.lua @@ -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 diff --git a/plugin/fatebook.lua b/plugin/fatebook.lua new file mode 100644 index 0000000..19c729d --- /dev/null +++ b/plugin/fatebook.lua @@ -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", +})