Crate unsegen_pager

Crate unsegen_pager 

Source
Expand description

An unsegen widget for viewing files with additional features.

§Examples:

extern crate unsegen;

use std::io::{stdin, stdout};
use unsegen::base::Terminal;
use unsegen::input::{Input, Key, ScrollBehavior};
use unsegen::widget::{RenderingHints, Widget};

use unsegen_pager::{Pager, PagerContent, SyntaxSet, SyntectHighlighter, ThemeSet};

fn main() {
    let stdout = stdout();
    let stdin = stdin();
    let stdin = stdin.lock();

    let file = "path/to/some/file";

    let syntax_set = SyntaxSet::load_defaults_nonewlines();
    let syntax = syntax_set
        .find_syntax_for_file(&file)
        .unwrap()
        .unwrap_or(syntax_set.find_syntax_plain_text());

    let theme_set = ThemeSet::load_defaults();
    let theme = &theme_set.themes["base16-ocean.dark"];

    let highlighter = SyntectHighlighter::new(syntax, theme);
    let mut pager = Pager::new();
    pager.load(
        PagerContent::from_file(&file)
            .unwrap()
            .with_highlighter(&highlighter),
    );

    let mut term = Terminal::new(stdout.lock()).unwrap();

    for input in Input::read_all(stdin) {
        let input = input.unwrap();
        input.chain(
            ScrollBehavior::new(&mut pager)
                .forwards_on(Key::Down)
                .forwards_on(Key::Char('j'))
                .backwards_on(Key::Up)
                .backwards_on(Key::Char('k'))
                .to_beginning_on(Key::Home)
                .to_end_on(Key::End),
        );
        // Put more application logic here...

        {
            let win = term.create_root_window();
            pager.as_widget().draw(win, RenderingHints::default());
        }
        term.present();
    }
}

Structs§

HighlightInfo
Result of a highlighting operation (i.e., a call to Highlighter::highlight).
LineNumberDecorator
Draw line numbers next to every line.
NoDecorator
Do not draw line decoration.
Pager
Main Widget, may (or may not) store content, but defines static types for content and decoration.
PagerContent
A collection of PagerLines including information about the highlighting state and (if present) a LineDecorator.
SyntaxDefinition
The main data structure representing a syntax definition loaded from a .sublime-syntax file. You’ll probably only need these as references to be passed around to parsing code.
SyntaxSet
A syntax set holds a bunch of syntaxes and manages loading them and the crucial operation of linking.
SyntectHighlighter
A Highlighter using the syntect library as a backend.
Theme
A theme parsed from a .tmTheme file. Contains fields useful for a theme list as well as settings for styling your editor.
ThemeSet

Enums§

PagerError
All errors that can occur when operating on a Pager or its contents.

Traits§

Highlighter
Interface for anything that highlights the content of Pagers.
LineDecorator
Interface for anything that is able to decorate lines, i.e., to draw something next to the left of a pager line, given some information about the line.
PagerLine
Anything that represents a single line in a pager. Other than the main content (something string-like) it may also store additional information that can be used by a Highlighter.