Thanks to visit codestin.com
Credit goes to docs.rs

Crate duat

Crate duat 

Source
Expand description

Duat is a text editor with sane defaults, while still having an incredible amount of modularity, to the point where you can replace pretty much anything.

It is written and configured in Rust, through the use of a config crate. The configuration can then be reloaded without closing Duat, by being recompiled as requested.

I know that Rust isn’t really a scripting language, but I’ve worked really hard to make this API intuitive to use, whilst still maintaining all the safety and expressiveness that Rust is known for.

Rust is also known for long compile times, but for Duat, I’ve managed to reduce the vast majority of reloads to under ~1.3 seconds, with a large chunk taking less than 800 ms (on my relatively old mid range laptop).

Do keep in mind that this is a work in progress, so there might be bugs. Any feedback on features, bugs or requests is highly appreciated 🥰.

§Installation

To install Duat, I am assuming that you have cargo installed on your system, if you don’t, install it. If you are installing it on Windows, you should additionlly follow the instructions that they give you for installing C/C++ libraries through Visual Studio.

§Note

On this section, I will be referring to duat’s configuration by ~/.config/duat/, but you should replace it with your operating system’s config path. The same also applies to ~/.local/duat/.

Next, in order to run duat, you should add ~/.cargo/bin/ to your $PATH variable. Alternatively, you can just add ~/.cargo/bin/duat, if you want to add just duat to the $PATH. Now, you can install duat:

cargo install duat

That is the recommended version, however, if you wish to install the bleeding edge version, you can call this instead:

cargo install --git https://github.com/AhoyISki/duat --features git-deps

§Configuration

When you first run duat, you will be prompted for the creation of a new configuration crate in ~/.config/duat/ (unless it already exists).

In the configuration’s src/lib.rs, there should be a setup_duat! macro, which takes in a function with no parameters.

This function is the setup for duat, and it can be empty, which is the equivalent of the default configuration for Duat.

Here’s an example configuration buffer, which makes use of the duat-kak crate, which is a plugin for Duat. This plugin, like all others, is included without the duat_ prefix, so in the config it is just kak.

setup_duat!(setup);
use duat::prelude::*;

fn setup() {
    map::<Insert>("jk", "<Esc>");

    opts::set(|opts| {
        opts.wrap_lines = true;
        opts.scrolloff.y = 5;
    });

    opts::set_lines(|opts| {
        opts.align = std::fmt::Alignment::Right;
    });

    opts::set_status(|pa| {
        let upper_mode = mode_name().map(|m| m.to_uppercase());

        status!("[mode]{upper_mode}{Spacer}{name_txt} {sels_txt} {main_txt}")
    });

    hook::add::<ModeSwitched>(|_, (_, new)| {
        match new {
            "Insert" => cursor::set_main(CursorShape::SteadyBar),
            _ => cursor::unset(),
        }
        Ok(())
    });

    form::set("mode", Form::dark_magenta());
}

This configuration does the following things:

  • Maps jk to esc in the Insert mode;
  • Sets options for the Buffer, LineNumbers and StatusLine
  • Adds hooks for mode changes in Duat, which change the shape of the cursor;
  • Changes the style of the mode printed on the status line;

These are only some of the options available to configure Duat, you can also add custom commands, place widgets around other Widgets and windows, create Parsers that can track every change on a Buffer, and many other things.

Duat also comes with a fully fledged text creation system, which significantly eases the creation of highly formatted text:

let infix = "text";

let text = txt!("This {infix} is [form1]colored and {Spacer} distant");

In the example above, [form1] will change the style of the text to the "form1" Form, while {Spacer} will place a spacer in between the two parts of the text (See the status line in the GIF, it uses spacers).

This macro works very similarly to the format! family of macros, so you also have inlining, as you can see with the {infix} part. All of this is, of course, checked at compile time.

§Troubleshooting

These issues asume that you are working with the --git-deps version of duat

§The configuration fails to compile/recompile

Try running the following:

duat --reload --clean

This will update all dependencies of the config, potentially solving compatibility issues. The problem may also be with some plugin you installed.

§It’s segfaulting as I reopen!

This is an indication that your installed version of duat became incompatible with that of your config. Rerun the installation process and call duat --reload.

§Default plugins

When you install duat, the default config crate will come with the following plugins:

  • duat-catppuccin is a just a simple colorscheme plugin, it adds the four flavors from the catppuccin colorscheme. You can pick between the four of them, you can apply its colors to other Forms and you can allow or disallow the colorscheme to set the background color.

It also comes with the following built-in plugins, which I will later on add the ability to disable:

  • duatmode is the default mode for editing in Duat. It is heavily inspired by the Kakoune text editor in its design, with some light differences.
  • duat-treesitter brings tree-sitter to Duat in the form of syntax highlighting and indentation calculation, which can be used by Modes (such as those from duat-kak) in order to give better feedback when editing buffers.
  • duat-match-pairs adds matched parentheses highlighting to duat. Has some ntegration with duat-treesitter.
  • duat-base adds all of the default plugins that you see, like the line numbers, status line, prompt line, etc.

§Features

Duat provides a lot of features, trying to be as configurable as possible, here are some of the things that Duat is capable of:

  • Completely custom modes, with full Vim style remapping;
  • Completely custom widgets, with user created modes;
  • Arbitrary concealment of text, and arbitrary ghost text;
  • Custom hooks, whose activation is up to the creator;
  • Custom commands, with customizable parameters supported by; Rust’s robust type system;
  • Multi UI adaptability, although for now, only a terminal UI has been made;
  • And many others still being planned;

Additionally, by choosing Rust as its configuration language, Duat also gains the following features:

  • Complete type safety;
  • A very functional programming language, with lots of native features;
  • Cargo is the plugin manager;

§Roadmap

These are the goals that have been accomplished or are on their way:

  • Implement basic visual functionality (printing, scrolling, etc);
  • Implement wrapping;
  • Implement editing;
  • Create a kak mode;
  • Implement the use of multiple cursors;
  • Implement a history system;
  • Implement colors;
  • Implement widgets and designated areas;
  • Make all of these things easy to use on a public interface;
  • Create a number line and a separator line;
  • Create a status line;
  • Buffer switching;
  • Create a command creation interface and a command line;
  • Add the ability to frame areas;
  • Implement concealment;
  • Implement hot reloading of configuration;
  • Create a “normal editing” mode;
  • Add the ability to create hooks;
  • Create a more generalized plugin system;
  • Implement incremental Regex searching;
  • Implement tree-sitter;
  • Add floating widgets, not tied to the session layout;
  • Implement autocompletion lists;
  • Create an LSP plugin;
  • Create a vim mode;

  • Create an gui frontend;

An internal (and more detailed) TODO list, which might hard to understand, can be found in TODO. This list will is not a comprehensive roadmap, as I will ocasionally remove entries from it, particularly those in the FOR NEXT UPDATE section, when said update comes out.

NOTE: These are not set in stone, and may be done out of order.

§Why should I use this?

I don’t know what your personal reasoning would be, but in my case, I really like Kakoune’s editing model, but was frustrated with the lack of some features, like folding, multiple buffer editing, the general barebonesness of the configuration, etc.

I know that Neovim has all of these features, and Helix supposedly tries to solve some of these issues. But I don’t really like either of their editing styles to be honest.

And so I thought, why not make my own text editor?

I thought, why not make a text editor that is as modular as possible, while still having a sensible default configuration? That I could modify however I wanted, and with a language that I love?

That’s why I decided to create Duat.

§Why the name

idk, cool sounding word that I got from Spelunky 2.

Re-exports§

pub use duat_core::lender;

Modules§

buffer
The primary Widget of Duat, used to display buffers.
clipboard
Clipboard interaction for Duat
cmd
Creation and execution of commands.
context
Access to widgets and other other parts of the state of Duat
cursor
Functions to alter the Cursors of Duat
data
Duat’s way of sharing and updating state
form
Functions to alter the Forms of Duat
hook
Utilities for hooks in Duat
mode
Commands for the manipulation of Modes
opts
General options for Duat
prelude
The prelude of Duat
state
Common items in a StatusLine
text
The primary data structure in Duat
ui
Ui structs and functions
utils
General utility functions for Duat
widgets
Duat’s builtin widgets

Macros§

setup_duat
Pre and post setup for Duat

Structs§

Plugins
A struct for Plugins to declare dependencies on other Plugins

Traits§

DoubleEndedLender
Documentation is incomplete. Refer to core::iter::DoubleEndedIterator for more information
ExactSizeLender
Documentation is incomplete. Refer to core::iter::ExactSizeIterator for more information
Lender
A trait for dealing with lending iterators.
Plugin
A plugin for Duat