xplr/en/lua-function-calls.html

430 lines
26 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js dark">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Lua Function Calls - xplr book</title>
<!-- Custom HTML head -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="A hackable, minimal, fast TUI file explorer">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff" />
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- EthicalAds -->
<script async src="https://media.ethicalads.io/media/client/ethicalads.min.js"></script>
</head>
<body>
<!-- Provide site root to javascript -->
<script type="text/javascript">
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "coal" : "dark";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script type="text/javascript">
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script type="text/javascript">
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('no-js')
html.classList.remove('dark')
html.classList.add(theme);
html.classList.add('js');
</script>
<!-- Hide / unhide sidebar before it is displayed -->
<script type="text/javascript">
var html = document.querySelector('html');
var sidebar = 'hidden';
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
}
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item expanded "><a href="introduction.html"><strong aria-hidden="true">1.</strong> Introduction</a></li><li class="chapter-item expanded "><a href="quickstart.html"><strong aria-hidden="true">2.</strong> Quickstart</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="install.html"><strong aria-hidden="true">2.1.</strong> Install</a></li><li class="chapter-item expanded "><a href="post-install.html"><strong aria-hidden="true">2.2.</strong> Post Install</a></li></ol></li><li class="chapter-item expanded "><a href="configuration.html"><strong aria-hidden="true">3.</strong> Configuration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="general-config.html"><strong aria-hidden="true">3.1.</strong> General</a></li><li class="chapter-item expanded "><a href="node_types.html"><strong aria-hidden="true">3.2.</strong> Node Types</a></li><li class="chapter-item expanded "><a href="layouts.html"><strong aria-hidden="true">3.3.</strong> Layouts</a></li><li class="chapter-item expanded "><a href="modes.html"><strong aria-hidden="true">3.4.</strong> Modes</a></li></ol></li><li class="chapter-item expanded "><a href="concept.html"><strong aria-hidden="true">4.</strong> Concept</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="key-bindings.html"><strong aria-hidden="true">4.1.</strong> Key Bindings</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="configure-key-bindings.html"><strong aria-hidden="true">4.1.1.</strong> Configure Key Bindings</a></li><li class="chapter-item expanded "><a href="default-key-bindings.html"><strong aria-hidden="true">4.1.2.</strong> Default Key Bindings</a></li><li class="chapter-item expanded "><a href="debug-key-bindings.html"><strong aria-hidden="true">4.1.3.</strong> Debug Key Bindings</a></li></ol></li><li class="chapter-item expanded "><a href="node-type.html"><strong aria-hidden="true">4.2.</strong> Node Type</a></li><li class="chapter-item expanded "><a href="layout.html"><strong aria-hidden="true">4.3.</strong> Layout</a></li><li class="chapter-item expanded "><a href="mode.html"><strong aria-hidden="true">4.4.</strong> Mode</a></li><li class="chapter-item expanded "><a href="message.html"><strong aria-hidden="true">4.5.</strong> Message</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="messages.html"><strong aria-hidden="true">4.5.1.</strong> Full List of Messages</a></li><li class="chapter-item expanded "><a href="input-operation.html"><strong aria-hidden="true">4.5.2.</strong> Input Operation</a></li></ol></li><li class="chapter-item expanded "><a href="borders.html"><strong aria-hidden="true">4.6.</strong> Borders</a></li><li class="chapter-item expanded "><a href="style.html"><strong aria-hidden="true">4.7.</strong> Style</a></li><li class="chapter-item expanded "><a href="searching.html"><strong aria-hidden="true">4.8.</strong> Searching</a></li><li class="chapter-item expanded "><a href="sorting.html"><strong aria-hidden="true">4.9.</strong> Sorting</a></li><li class="chapter-item expanded "><a href="filtering.html"><strong aria-hidden="true">4.10.</strong> Filtering</a></li><li class="chapter-item expanded "><a href="column-renderer.html"><strong aria-hidden="true">4.11.</strong> Column Renderer</a></li><li class="chapter-item expanded "><a href="lua-function-calls.html" class="active"><strong aria-hidden="true">4.12.</strong> Lua Function Calls</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="xplr.util.html"><strong aria-hidden="true">4.12.1.</strong> xplr.util</a></li></ol></li><li class="chapter-item expanded "><a href="environment-variables-and-pipes.html"><strong aria-hidden="true">4.13.</strong> Environment Variables and Pipes</a></li></ol></li><li class="chapter-item expanded "><a href="awesome-hacks.html"><strong aria-hidden="true">5.</strong> Awesome Hacks</a></li><li class="chapter-item expanded "><a href="plugin.html"><strong aria-hidden="true">6.</strong> Plugin</a></li><li>
<!-- EthicalAds -->
<div
id="docs-sidebar-bottom"
class="dark flat"
data-ea-publisher="xplrdev"
data-ea-type="image"
></div>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky bordered">
<div class="left-buttons">
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</button>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">xplr book</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/sayanarijit/xplr" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/sayanarijit/xplr/edit/main/docs/en/src/lua-function-calls.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script type="text/javascript">
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="lua-function-calls"><a class="header" href="#lua-function-calls">Lua Function Calls</a></h1>
<p>xplr allows you to define lua functions using the <code>xplr.fn.custom</code> Lua API.</p>
<p>These functions can be called using messages like <code>CallLua</code>, <code>CallLuaSilently</code>.</p>
<p>When called the function receives a <a href="#lua-context">special argument</a> that
contains some useful information. The function can optionally return a list of
messages which will be handled by xplr.</p>
<h2 id="example-using-lua-function-calls"><a class="header" href="#example-using-lua-function-calls">Example: Using Lua Function Calls</a></h2>
<pre><code class="language-lua">-- Define the function
xplr.fn.custom.ask_name_and_greet = function(app)
print(&quot;What's your name?&quot;)
local name = io.read()
local greeting = &quot;Hello &quot; .. name .. &quot;!&quot;
local message = greeting .. &quot; You are inside &quot; .. app.pwd
return {
{ LogSuccess = message },
}
end
-- Map the function to a key (space)
xplr.config.modes.builtin.default.key_bindings.on_key.space = {
help = &quot;ask name and greet&quot;,
messages = {
{ CallLua = &quot;custom.ask_name_and_greet&quot; }
}
}
-- Now, when you press &quot;space&quot; in default mode, you will be prompted for your
-- name. Enter your name to receive a nice greeting and to know your location.
</code></pre>
<p>Visit the <a href="xplr.util.html">xplr.util</a> API docs for some useful utility / helper functions
that you can use in your Lua function calls.</p>
<h2 id="lua-context"><a class="header" href="#lua-context">Lua Context</a></h2>
<p>This is a special argument passed to the lua functions when called using the
<code>CallLua</code>, <code>CallLuaSilently</code> messages.</p>
<p>It contains the following information:</p>
<ul>
<li><a href="#version">version</a></li>
<li><a href="#pwd">pwd</a></li>
<li><a href="#initial_pwd">initial_pwd</a></li>
<li><a href="#vroot">vroot</a></li>
<li><a href="#focused_node">focused_node</a></li>
<li><a href="#directory_buffer">directory_buffer</a></li>
<li><a href="#selection">selection</a></li>
<li><a href="#mode">mode</a></li>
<li><a href="#layout">layout</a></li>
<li><a href="#input_buffer">input_buffer</a></li>
<li><a href="#pid">pid</a></li>
<li><a href="#session_path">session_path</a></li>
<li><a href="#explorer_config">explorer_config</a></li>
<li><a href="#history">history</a></li>
<li><a href="#last_modes">last_modes</a></li>
</ul>
<h3 id="version"><a class="header" href="#version">version</a></h3>
<p>Type: string</p>
<p>xplr version. Can be used to test compatibility.</p>
<h3 id="pwd"><a class="header" href="#pwd">pwd</a></h3>
<p>Type: string</p>
<p>The present working directory.</p>
<h3 id="initial_pwd"><a class="header" href="#initial_pwd">initial_pwd</a></h3>
<p>Type: string</p>
<p>The initial working directory when xplr started.</p>
<h3 id="vroot"><a class="header" href="#vroot">vroot</a></h3>
<p>Type: nullable string</p>
<p>The current virtual root.</p>
<h3 id="focused_node"><a class="header" href="#focused_node">focused_node</a></h3>
<p>Type: nullable <a href="#node">Node</a></p>
<p>The node under focus.</p>
<h3 id="directory_buffer"><a class="header" href="#directory_buffer">directory_buffer</a></h3>
<p>Type: nullable <a href="#directory-buffer">Directory Buffer</a></p>
<p>The directory buffer being rendered.</p>
<h3 id="selection"><a class="header" href="#selection">selection</a></h3>
<p>Type: list of selected <a href="#node">Node</a>s</p>
<p>The selected nodes.</p>
<h3 id="mode"><a class="header" href="#mode">mode</a></h3>
<p>Type: <a href="modes.html#mode">Mode</a></p>
<p>Current mode.</p>
<h3 id="layout"><a class="header" href="#layout">layout</a></h3>
<p>Type: <a href="layouts.html">Layout</a></p>
<p>Current layout.</p>
<h3 id="input_buffer"><a class="header" href="#input_buffer">input_buffer</a></h3>
<p>Type: nullable string</p>
<p>The input buffer.</p>
<h3 id="pid"><a class="header" href="#pid">pid</a></h3>
<p>Type: integer</p>
<p>The xplr session PID.</p>
<h3 id="session_path"><a class="header" href="#session_path">session_path</a></h3>
<p>Type: string</p>
<p>The session path.</p>
<h3 id="explorer_config"><a class="header" href="#explorer_config">explorer_config</a></h3>
<p>Type: <a href="#explorer-config">Explorer Config</a></p>
<p>The configuration for exploring paths.</p>
<h3 id="history"><a class="header" href="#history">history</a></h3>
<p>Type: <a href="#history-1">History</a></p>
<h3 id="last_modes"><a class="header" href="#last_modes">last_modes</a></h3>
<p>Type: list of <a href="modes.html#mode">Mode</a></p>
<p>Last modes, not popped yet.</p>
<h2 id="node"><a class="header" href="#node">Node</a></h2>
<p>A node contains the following fields:</p>
<ul>
<li><a href="#parent">parent</a></li>
<li><a href="#relative_path">relative_path</a></li>
<li><a href="#absolute_path">absolute_path</a></li>
<li><a href="#extension">extension</a></li>
<li><a href="#is_symlink">is_symlink</a></li>
<li><a href="#is_broken">is_broken</a></li>
<li><a href="#is_dir">is_dir</a></li>
<li><a href="#is_file">is_file</a></li>
<li><a href="#is_readonly">is_readonly</a></li>
<li><a href="#mime_essence">mime_essence</a></li>
<li><a href="#size">size</a></li>
<li><a href="#human_size">human_size</a></li>
<li><a href="#permissions">permissions</a></li>
<li><a href="#created">created</a></li>
<li><a href="#last_modified">last_modified</a></li>
<li><a href="#uid">uid</a></li>
<li><a href="#gid">gid</a></li>
<li><a href="#canonical">canonical</a></li>
<li><a href="#symlink">symlink</a></li>
</ul>
<h3 id="parent"><a class="header" href="#parent">parent</a></h3>
<p>Type: string</p>
<p>The parent path of the node.</p>
<h3 id="relative_path"><a class="header" href="#relative_path">relative_path</a></h3>
<p>Type: string</p>
<p>The path relative to the parent, i.e. the file/directory name with extension.</p>
<h3 id="absolute_path"><a class="header" href="#absolute_path">absolute_path</a></h3>
<p>Type: string</p>
<p>The absolute path (without resolving symlinks) of the node.</p>
<h3 id="extension"><a class="header" href="#extension">extension</a></h3>
<p>Type: string</p>
<p>The extension of the node.</p>
<h3 id="is_symlink"><a class="header" href="#is_symlink">is_symlink</a></h3>
<p>Type: boolean</p>
<p><code>true</code> if the node is a symlink.</p>
<h3 id="is_broken"><a class="header" href="#is_broken">is_broken</a></h3>
<p>Type: boolean</p>
<p><code>true</code> if the node is a broken symlink.</p>
<h3 id="is_dir"><a class="header" href="#is_dir">is_dir</a></h3>
<p>Type: boolean</p>
<p><code>true</code> if the node is a directory.</p>
<h3 id="is_file"><a class="header" href="#is_file">is_file</a></h3>
<p>Type: boolean</p>
<p><code>true</code> if the node is a file.</p>
<h3 id="is_readonly"><a class="header" href="#is_readonly">is_readonly</a></h3>
<p>Type: boolean</p>
<p><code>true</code> if the node is real-only.</p>
<h3 id="mime_essence"><a class="header" href="#mime_essence">mime_essence</a></h3>
<p>Type: string</p>
<p>The mime type of the node. For e.g. <code>text/csv</code>, <code>image/jpeg</code> etc.</p>
<h3 id="size"><a class="header" href="#size">size</a></h3>
<p>Type: integer</p>
<p>The size of the exact node. The size of a directory won't be calculated
recursively.</p>
<h3 id="human_size"><a class="header" href="#human_size">human_size</a></h3>
<p>Type: string</p>
<p>Like size but in human readable format.</p>
<h3 id="permissions"><a class="header" href="#permissions">permissions</a></h3>
<p>Type: <a href="column-renderer.html#permission">Permission</a></p>
<p>The <a href="column-renderer.html#permission">permissions</a> applied to the node.</p>
<h3 id="created"><a class="header" href="#created">created</a></h3>
<p>Type: nullable integer</p>
<p>Creation time in nanosecond since UNIX epoch.</p>
<h3 id="last_modified"><a class="header" href="#last_modified">last_modified</a></h3>
<p>Type: nullable integer</p>
<p>Last modification time in nanosecond since UNIX epoch.</p>
<h3 id="uid"><a class="header" href="#uid">uid</a></h3>
<p>Type: integer</p>
<p>User ID of the file owner.</p>
<h3 id="gid"><a class="header" href="#gid">gid</a></h3>
<p>Type: integer</p>
<p>Group ID of the file owner.</p>
<h3 id="canonical"><a class="header" href="#canonical">canonical</a></h3>
<p>Type: nullable <a href="column-renderer.html#resolved-node-metadata">Resolved Node Metadata</a></p>
<p>If the node is a symlink, it will hold information about the symlink resolved
node. Else, it will hold information the actual node. It the symlink is broken,
it will be null.</p>
<h3 id="symlink"><a class="header" href="#symlink">symlink</a></h3>
<p>Type: nullable <a href="column-renderer.html#resolved-node-metadata">Resolved Node Metadata</a></p>
<p>If the node is a symlink and is not broken, it will hold information about the
symlink resolved node. However, it will never hold information about the actual
node. It will instead be null.</p>
<h2 id="directory-buffer"><a class="header" href="#directory-buffer">Directory Buffer</a></h2>
<p>Directory buffer contains the following fields:</p>
<ul>
<li><a href="#parent">parent</a></li>
<li><a href="#nodes">nodes</a></li>
<li><a href="#total">total</a></li>
<li><a href="#focus">focus</a></li>
</ul>
<h3 id="parent-1"><a class="header" href="#parent-1">parent</a></h3>
<p>Type: string</p>
<p>The parent path of the node.</p>
<h3 id="nodes"><a class="header" href="#nodes">nodes</a></h3>
<p>Type: list of <a href="#node">Node</a>s</p>
<p>A list of visible nodes.</p>
<h3 id="total"><a class="header" href="#total">total</a></h3>
<p>Type: int</p>
<p>The count of nodes being rendered.</p>
<h3 id="focus"><a class="header" href="#focus">focus</a></h3>
<p>Type: int</p>
<p>The index of the node under focus. It can be <code>0</code> even when there's no node to
focus on.</p>
<h2 id="history-1"><a class="header" href="#history-1">History</a></h2>
<p>History contains the following fields:</p>
<ul>
<li><a href="#loc">loc</a></li>
<li><a href="#paths">paths</a></li>
</ul>
<h3 id="loc"><a class="header" href="#loc">loc</a></h3>
<p>Type: int</p>
<p>Location of the current path in history.</p>
<h3 id="paths"><a class="header" href="#paths">paths</a></h3>
<p>Type: list of string</p>
<p>Visited paths.</p>
<h2 id="explorer-config"><a class="header" href="#explorer-config">Explorer Config</a></h2>
<p>Explorer config contains the following fields:</p>
<ul>
<li><a href="#filters">filters</a></li>
<li><a href="#sorters">sorters</a></li>
<li><a href="#searcher">searcher</a></li>
</ul>
<h3 id="filters"><a class="header" href="#filters">filters</a></h3>
<p>List of filters to apply.</p>
<p>Type: list of <a href="filtering.html#node-filter-applicable">Node Filter Applicable</a></p>
<h3 id="sorters"><a class="header" href="#sorters">sorters</a></h3>
<p>Add list or sorters to the pipeline.</p>
<p>Type: list of <a href="sorting.html#node-sorter-applicable">Node Sorter Applicable</a></p>
<h3 id="searcher"><a class="header" href="#searcher">searcher</a></h3>
<p>The searcher to use (if any).</p>
<p>Type: nullable <a href="searching.html#node-searcher-applicable">Node Searcher Applicable</a></p>
<h2 id="also-see"><a class="header" href="#also-see">Also See:</a></h2>
<ul>
<li><a href="xplr.util.html">xplr.util</a></li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="column-renderer.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next" href="xplr.util.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="column-renderer.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next" href="xplr.util.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script type="text/javascript">
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
<script src="book.js" type="text/javascript" charset="utf-8"></script>
<!-- Custom JS scripts -->
</body>
</html>