Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add random verse querying #20

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ echo 'export SWORD_PATH="${HOME}/.sword" >> ~/.zshrc'
| `:BibleVerse` | `require("bible-verse").cmd()` | Execute default behaviour set as per `config.default_behaviour`.|
| `:BibleVerseQuery` or `:BibleVerse query` | `require("bible-verse").cmd("query")` | Query Bible verse and display it on the screen as a popup. |
| `:BibleVerseInsert` or `:BibleVerse insert` | `require("bible-verse").cmd("insert")` | Query Bible verse and insert it below the cursor in the current buffer. |
| `:BibleVerseRandom` | `:BibleVerse random` | `require("bible-verse").cmd("random")` | Query random Bible verse and returns a table containing the Book with chapter, the verse and translation name. |
| `:BibleVerseQueryRandom` or `:BibleVerse queryRandom` | `require("bible-verse").cmd("queryRandom")` | Query random Bible verse and display it on the screen as a popup. |

#### Key Bindings

Expand Down
15 changes: 11 additions & 4 deletions lua/bible-verse/commands.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local Config = require("bible-verse.config")
local Core = require("bible-verse.core")

---@alias BibleVerseCmd "query"|"insert"
---@alias BibleVerseCmd "query"|"insert"|"random"|"queryRandom"
---@alias BibleVerseCmdFunc fun(): nil

local M = {}
Expand All @@ -21,11 +22,17 @@ end
function M.setup()
M.commands = {
query = function()
require("bible-verse.core").query_and_show()
Core.query_and_show()
end,
insert = function()
require("bible-verse.core").query_and_insert()
Core.query_and_insert()
end,
random = function()
Core.random_query()
end,
queryRandom = function()
Core.query_and_show_random()
end
}

-- Check that config is sane
Expand All @@ -39,7 +46,7 @@ function M.setup()
local command = vim.trim(args.args or "")
M.cmd(command)
end, {
nargs = "?",
nargs = "*",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem that any of the new commands need to accept >1 arguments 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I forgot to change it back

desc = "Query bible verses",
complete = function(_, line)
-- Completed word
Expand Down
29 changes: 29 additions & 0 deletions lua/bible-verse/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local Utils = require("bible-verse.utils")
local Diatheke = require("bible-verse.core.diatheke")
local Ui = require("bible-verse.core.ui")
local Highlight = require("bible-verse.core.highlight")
local random_verse = require("bible-verse.core.random-verse")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to RandomVerse


local M = {}

Expand Down Expand Up @@ -110,4 +111,32 @@ function M.query_and_insert()
Ui:input(input_conf, on_submit)
end

--- Returns a random verse
function M.random_query()
return process_query(random_verse(), Config.options.query_format, 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether it would be easy to use M.random_query() if the format is using Config.options.query_format 🤔

A user who has set Config.options.query_format=bibleverse to get highlighting on the pop up will get difficult to read/use output from M.random_query().

Do you think it would be better to create a separate Config.options?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true, I haven't played enough with the config to know about this. I'll let you be the judge, it's probably a good idea :)

end

--- Query and shows a random verse
function M.query_and_show_random()
local popup_conf = vim.deepcopy(Config.options.ui.popup)
local relative_size = Utils.get_relative_size(popup_conf.relative)

local popup_width = math.ceil(
math.min(
relative_size.width * popup_conf.size.max_width_percentage,
relative_size.width * popup_conf.size.width_percentage
)
)
local popup_max_height = math.ceil(relative_size.height * popup_conf.size.max_height_percentage)

local query_result = M.random_query()
Copy link
Owner

@anthony-halim anthony-halim Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to pass in the popup_width such that the outputted format is correctly shown. Prefer to simply call process_query(...) instead of calling M.random_query() to avoid dependencies between public functions.


popup_conf.size.width = popup_width
popup_conf.size.height = Utils.clamp(#query_result, 1, popup_max_height)

Ui:popup(popup_conf, query_result, function(bufnr, first, last)
Highlight.highlight_buf(bufnr, Config.options.highlighter[Config.options.query_format], first, last)
end)
end

return M
Loading