Skip to content

Add support for fifo based previewer#229

Merged
sayanarijit merged 1 commit intomainfrom
add/support-nnn-preview
Jun 6, 2021
Merged

Add support for fifo based previewer#229
sayanarijit merged 1 commit intomainfrom
add/support-nnn-preview

Conversation

@sayanarijit
Copy link
Owner

@sayanarijit sayanarijit commented Jun 5, 2021

Adds basic support for FIFO based previewer.

The FIFO can be managed with the following messages:

  • StartFifo: /path/to/fifo
  • StopFifo
  • ToggleFifo: /path/to/fifo

A basic nnn plugin wrapper example:

-- ~/.config/xplr/nnn_preview_wrapper/init.lua

-- Usage Example:
--
--   package.path = os.getenv("HOME") .. '/.config/xplr/?/init.lua'
-- 
--   require("nnn_preview_wrapper").setup{
--     plugin_path = os.getenv("HOME") .. "/.config/nnn/plugins/preview-tabbed",
--     fifo_path = "/tmp/xplr.fifo",
--     mode = "action",
--     key = "p",
--   }
--
-- Press `:p` to toggle preview mode.

local function setup(o)

  if o.fifo_path == nil then
    o.fifo_path = os.getenv("NNN_FIFO")
  end

  if o.mode == nil then
    o.mode = "action"
  end

  if o.key == nil then
    o.key = "p"
  end

  local enabled = false
  local messages = {}

  os.execute('[ ! -p "' .. o.fifo_path ..'" ] && mkfifo "' .. o.fifo_path .. '"')

  xplr.fn.custom.preview_toggle = function(app)

    if enabled then
      enabled = false
      messages = { "StopFifo" }
    else
      os.execute('NNN_FIFO="' .. o.fifo_path .. '" "'.. o.plugin_path .. '" & ')
      enabled = true
      messages = {
        { StartFifo = o.fifo_path },
        { BashExecSilently = 'echo $XPLR_FOCUS_PATH >> "' .. o.fifo_path .. '" &' },
      }
    end

    return messages
  end

  xplr.config.modes.builtin[o.mode].key_bindings.on_key[o.key] = {
    help = "toggle preview",
    messages = {
      "PopMode",
      { CallLuaSilently = "custom.preview_toggle" },
    },
  }
end

return { setup = setup }

Press :p to toggle preview mode.

Closes: #205

@sayanarijit sayanarijit force-pushed the add/support-nnn-preview branch from 0aa064f to 3c74d89 Compare June 5, 2021 12:48
@sayanarijit
Copy link
Owner Author

xplr-nnn-preview

@sayanarijit sayanarijit changed the title Add support for nnn-like previewer Add support for fifo based Jun 5, 2021
@sayanarijit sayanarijit changed the title Add support for fifo based Add support for fifo based previews Jun 5, 2021
@sayanarijit sayanarijit changed the title Add support for fifo based previews Add support for fifo based previewer Jun 5, 2021
@sayanarijit sayanarijit force-pushed the add/support-nnn-preview branch 3 times, most recently from d6851d1 to a1358c7 Compare June 6, 2021 06:37
Adds basic support for nnn-like FIFO based previewer.

The FIFO can be manager with the following messages:

- StartFifo: /path/to/fifo
- StopFifo
- ToggleFifo: /path/to/fifo

A basic nnn plugin wrapper example:

```lua
-- Usage Example:
--
--   require("nnn_preview_wrapper").setup{
--     plugin_path = os.getenv("HOME") .. "/.config/nnn/plugins/preview-tabbed",
--     fifo_path = "/tmp/xplr.fifo",
--     mode = "action",
--     key = "p",
--   }
--
-- Press `:p` to toggle preview mode.

local function setup(o)

  if o.fifo_path == nil then
    o.fifo_path = os.getenv("NNN_FIFO")
  end

  if o.mode == nil then
    o.mode = "action"
  end

  if o.key == nil then
    o.key = "p"
  end

  local enabled = false
  local message = nil

  os.execute('[ ! -p "' .. o.fifo_path ..'" ] && mkfifo "' .. o.fifo_path .. '"')

  xplr.fn.custom.preview_toggle = function(app)

    if enabled then
      enabled = false
      message = "StopFifo"
    else
      os.execute('NNN_FIFO="' .. o.fifo_path .. '" "'.. o.plugin_path .. '" & ')
      enabled = true
      message = { StartFifo = o.fifo_path }
    end

    return { message }
  end

  xplr.config.modes.builtin[o.mode].key_bindings.on_key[o.key] = {
    help = "search with preview",
    messages = {
      "PopMode",
      { CallLuaSilently = "custom.preview_toggle" },
    },
  }
end

return { setup = setup }
```

Press `:p` to toggle preview mode.

Closes: #205
@sayanarijit sayanarijit force-pushed the add/support-nnn-preview branch 2 times, most recently from f4ae9e9 to 63ded74 Compare June 6, 2021 06:52
@sayanarijit sayanarijit merged commit a2f42ac into main Jun 6, 2021
@sayanarijit sayanarijit deleted the add/support-nnn-preview branch June 6, 2021 06:59
@mrdgo mrdgo mentioned this pull request Jun 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement background services/fifo and/or callbacks (preview support)

1 participant