#text-editing #white-space #text #editing #utilities

single-whitespace-text-editing

Utilities for a special editing style, where the only allowed whitespace is the space, and it's only allowed once at a time

3 unstable releases

0.2.1 Sep 20, 2024
0.2.0 Sep 20, 2024
0.1.0 Jun 16, 2022

#136 in Text editors

Download history 1/week @ 2024-08-21 3/week @ 2024-08-28 1/week @ 2024-09-11 349/week @ 2024-09-18 79/week @ 2024-09-25 56/week @ 2024-10-02 10/week @ 2024-10-09

86 downloads per month
Used in 2 crates (via pn-editor-core)

MIT/Apache

11KB
89 lines

Single Whitespace Text Editing

This crate provides utilities for a special editing style, where the only allowed whitespace is the space, and it's only allowed once at a time.

Features

  • Ensures that there is always exactly one whitespace character in a row, automatically removing any additional whitespace when necessary.
  • Provides an enum SingleWhitespaceEditAction and its associated functions for performing text editing operations based on the input character.
  • Offers a simple and efficient way to handle text editing operations with single whitespace handling.

Dependencies

This crate depends on the text-editing crate for text manipulation functionality.


lib.rs:

A library for text editing operations with single whitespace handling.

This library provides the SingleWhitespaceEditAction enum and its associated functions for performing text editing operations based on the input character. The library ensures that there is always exactly one whitespace character in a row, automatically removing any additional whitespace when necessary.

A library for text editing operations with single whitespace handling.

This library provides the SingleWhitespaceEditAction enum and its associated functions for performing text editing operations based on the input character. The library ensures that there is always exactly one whitespace character in a row, automatically removing any additional whitespace when necessary.

The following operations are supported:

  • Most common characters: Adds a character at the cursor position.
  • Newline or carriage return: No operation is performed.
  • Whitespace characters (except tab): Adds a single space at the cursor position.
  • Delete ('\x7F'): Removes the character or word forward from the cursor position.
  • Backspace ('\x08'): Removes the character or word backward from the cursor position.
  • Any other character: No operation is performed.

Examples

Adding a Character

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World").unwrap();
let mut cursor = 11;
let edit_op = SingleWhitespaceEditAction::new('!', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World!");

Adding a Space

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("HelloWorld").unwrap();
let mut cursor = 5;
let edit_op = SingleWhitespaceEditAction::new(' ', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World");

Pressing Space Before a Space

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World").unwrap();
let mut cursor = 5;
let edit_op = SingleWhitespaceEditAction::new(' ', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World");
assert_eq!(cursor, 6);

Removing multiple Characters with Backspace

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World!").unwrap();
let mut cursor = 6;
let edit_op = SingleWhitespaceEditAction::new('\x08', true).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "World!");

Removing a Character, Resulting in Double Spaces

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello , World!").unwrap();
let mut cursor = 7;
let edit_op = SingleWhitespaceEditAction::new('\x08', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "Hello World!");

Removing a Character with Delete

use std::str::FromStr;

use single_whitespace_text_editing::SingleWhitespaceEditAction;
use text_editing::TextLine;

let mut text = TextLine::from_str("Hello World!").unwrap();
let mut cursor = 5;
let edit_op = SingleWhitespaceEditAction::new('\x7F', false).unwrap();
edit_op.apply(&mut text, &mut cursor);
assert_eq!(text.as_str(), "HelloWorld!");

Dependencies

~21KB