Skip to content

State Tracking

Josh Peterson edited this page Jul 7, 2024 · 1 revision

Typewriter.nvim now includes robust state tracking functionality, allowing users and other plugins to easily check and manipulate the Typewriter mode's state. This feature enhances the plugin's flexibility and integration capabilities.

Core Functions

is_typewriter_active()

This function allows you to check whether Typewriter mode is currently active.

Usage:

local utils = require("typewriter.utils")
if utils.is_typewriter_active() then
    print("Typewriter mode is active")
else
    print("Typewriter mode is inactive")
end

set_typewriter_active(boolean)

Use this function to explicitly set the state of Typewriter mode.

Usage:

local utils = require("typewriter.utils")
utils.set_typewriter_active(true)  -- Enables Typewriter mode
utils.set_typewriter_active(false) -- Disables Typewriter mode

toggle_typewriter_active()

This function toggles the state of Typewriter mode and returns the new state.

Usage:

local utils = require("typewriter.utils")
local new_state = utils.toggle_typewriter_active()
print("Typewriter mode is now: " .. (new_state and "active" or "inactive"))

TypewriterStateChanged Event

Whenever the state of Typewriter mode changes (either through the API or via user commands), a TypewriterStateChanged event is triggered. You can use this event to perform actions in response to state changes.

Example: Updating statusline when Typewriter mode changes

vim.api.nvim_create_autocmd("User", {
    pattern = "TypewriterStateChanged",
    callback = function()
        -- Update your statusline here
        require('lualine').refresh()
    end
})

Integration Examples

With Lualine

You can use the state tracking to display the current state of Typewriter mode in your statusline:

require('lualine').setup {
    sections = {
        lualine_x = {
            {
                function()
                    return require("typewriter.utils").is_typewriter_active() and "TW" or ""
                end,
                color = { fg = "#ff9e64" }
            }
        }
    }
}

Custom Keybindings

You can create custom keybindings that react to the current state:

vim.api.nvim_set_keymap('n', '<leader>tw', function()
    if require("typewriter.utils").is_typewriter_active() then
        -- Do something when Typewriter is active
    else
        -- Do something else when it's inactive
    end
end, { noremap = true, silent = true })

Best Practices

  1. Always use the provided API functions to check or change the state, rather than trying to access or modify the state directly.
  2. If you're creating a plugin that interacts with Typewriter.nvim, consider using the TypewriterStateChanged event to react to state changes, rather than polling the state continuously.
  3. When toggling the state programmatically, use toggle_typewriter_active() rather than checking the state and then setting it manually, as this ensures that all necessary side effects (like triggering the state changed event) occur.

By leveraging these state tracking features, you can create more dynamic and responsive integrations with Typewriter.nvim, enhancing your Neovim editing experience.