forked from Mirrors/helix
Implement a new core based on CodeMirror.
parent
240e5f4e3d
commit
44ff4d3c1f
@ -0,0 +1,18 @@
|
||||
use anyhow::Error;
|
||||
use ropey::Rope;
|
||||
use std::{env, fs::File, io::BufReader, path::PathBuf};
|
||||
|
||||
pub struct Buffer {
|
||||
pub contents: Rope,
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
pub fn load(path: PathBuf) -> Result<Self, Error> {
|
||||
let current_dir = env::current_dir()?;
|
||||
|
||||
let contents = Rope::from_reader(BufReader::new(File::open(path)?))?;
|
||||
|
||||
// TODO: create if not found
|
||||
Ok(Buffer { contents })
|
||||
}
|
||||
}
|
@ -1,5 +1,13 @@
|
||||
mod position;
|
||||
mod range;
|
||||
mod buffer;
|
||||
mod selection;
|
||||
mod state;
|
||||
mod transaction;
|
||||
|
||||
use position::Position;
|
||||
use range::Range;
|
||||
pub use buffer::Buffer;
|
||||
|
||||
pub use selection::Range as SelectionRange;
|
||||
pub use selection::Selection;
|
||||
|
||||
pub use state::State;
|
||||
|
||||
pub use transaction::{Change, Transaction};
|
||||
|
@ -1,27 +0,0 @@
|
||||
/// Represents a single point in a text buffer. Zero indexed.
|
||||
#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Position {
|
||||
pub row: usize,
|
||||
pub col: usize,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
pub fn new(row: usize, col: usize) -> Self {
|
||||
Self { row, col }
|
||||
}
|
||||
|
||||
pub fn is_zero(self) -> bool {
|
||||
self.row == 0 && self.col == 0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_ordering() {
|
||||
// (0, 5) is less than (1, 0 w v f)
|
||||
assert!(Position::new(0, 5) < Position::new(1, 0));
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
use crate::Position;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Range {
|
||||
pub start: Position,
|
||||
pub end: Position,
|
||||
}
|
||||
|
||||
// range traversal iters
|
@ -0,0 +1,222 @@
|
||||
//! Selections are the primary editing construct. Even a single cursor is defined as an empty
|
||||
//! single selection range.
|
||||
//!
|
||||
//! All positioning is done via `char` offsets into the buffer.
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
|
||||
#[inline]
|
||||
fn abs_difference(x: usize, y: usize) -> usize {
|
||||
if x < y {
|
||||
y - x
|
||||
} else {
|
||||
x - y
|
||||
}
|
||||
}
|
||||
|
||||
/// A single selection range. Anchor-inclusive, head-exclusive.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Range {
|
||||
// TODO: optimize into u32
|
||||
/// The anchor of the range: the side that doesn't move when extending.
|
||||
pub anchor: usize,
|
||||
/// The head of the range, moved when extending.
|
||||
pub head: usize,
|
||||
}
|
||||
|
||||
impl Range {
|
||||
pub fn new(anchor: usize, head: usize) -> Self {
|
||||
Self { anchor, head }
|
||||
}
|
||||
|
||||
/// Start of the range.
|
||||
#[inline]
|
||||
pub fn from(&self) -> usize {
|
||||
std::cmp::min(self.anchor, self.head)
|
||||
}
|
||||
|
||||
/// End of the range.
|
||||
#[inline]
|
||||
pub fn to(&self) -> usize {
|
||||
std::cmp::max(self.anchor, self.head)
|
||||
}
|
||||
|
||||
/// `true` when head and anchor are at the same position.
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.anchor == self.head
|
||||
}
|
||||
|
||||
/// Check two ranges for overlap.
|
||||
pub fn overlaps(&self, other: &Self) -> bool {
|
||||
// cursor overlap is checked differently
|
||||
if self.is_empty() {
|
||||
self.from() <= other.to()
|
||||
} else {
|
||||
self.from() < other.to()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: map
|
||||
|
||||
/// Extend the range to cover at least `from` `to`.
|
||||
pub fn extend(&self, from: usize, to: usize) -> Self {
|
||||
if from <= self.anchor && to >= self.anchor {
|
||||
return Range {
|
||||
anchor: from,
|
||||
head: to,
|
||||
};
|
||||
}
|
||||
|
||||
Range {
|
||||
anchor: self.anchor,
|
||||
head: if abs_difference(from, self.anchor) > abs_difference(to, self.anchor) {
|
||||
from
|
||||
} else {
|
||||
to
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// groupAt
|
||||
}
|
||||
|
||||
/// A selection consists of one or more selection ranges.
|
||||
pub struct Selection {
|
||||
// TODO: decide how many ranges to inline SmallVec<[Range; 1]>
|
||||
ranges: Vec<Range>,
|
||||
primary_index: usize,
|
||||
}
|
||||
|
||||
impl Selection {
|
||||
// map
|
||||
// eq
|
||||
pub fn primary(&self) -> Range {
|
||||
self.ranges[self.primary_index]
|
||||
}
|
||||
|
||||
/// Ensure selection containing only the primary selection.
|
||||
pub fn as_single(self) -> Self {
|
||||
if self.ranges.len() == 1 {
|
||||
self
|
||||
} else {
|
||||
Self {
|
||||
ranges: vec![self.ranges[self.primary_index]],
|
||||
primary_index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add_range // push
|
||||
// replace_range
|
||||
|
||||
/// Constructs a selection holding a single range.
|
||||
pub fn single(anchor: usize, head: usize) -> Self {
|
||||
Self {
|
||||
ranges: vec![Range { anchor, head }],
|
||||
primary_index: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(ranges: Vec<Range>, primary_index: usize) -> Self {
|
||||
fn normalize(mut ranges: Vec<Range>, primary_index: usize) -> Selection {
|
||||
let primary = ranges[primary_index];
|
||||
ranges.sort_unstable_by_key(|range| range.from());
|
||||
let mut primary_index = ranges.iter().position(|&range| range == primary).unwrap();
|
||||
|
||||
let mut result: Vec<Range> = Vec::new();
|
||||
|
||||
// TODO: we could do with one vec by removing elements as we mutate
|
||||
|
||||
for (i, range) in ranges.into_iter().enumerate() {
|
||||
// if previous value exists
|
||||
if let Some(prev) = result.last_mut() {
|
||||
// and we overlap it
|
||||
if range.overlaps(prev) {
|
||||
let from = prev.from();
|
||||
let to = std::cmp::max(range.to(), prev.to());
|
||||
|
||||
if i <= primary_index {
|
||||
primary_index -= 1
|
||||
}
|
||||
|
||||
// merge into previous
|
||||
if range.anchor > range.head {
|
||||
prev.anchor = to;
|
||||
prev.head = from;
|
||||
} else {
|
||||
prev.anchor = from;
|
||||
prev.head = to;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
result.push(range)
|
||||
}
|
||||
|
||||
Selection {
|
||||
ranges: result,
|
||||
primary_index,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: only normalize if needed (any ranges out of order)
|
||||
normalize(ranges, primary_index)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: checkSelection -> check if valid for doc length
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_create_normalizes_and_merges() {
|
||||
let sel = Selection::new(
|
||||
vec![
|
||||
Range::new(10, 12),
|
||||
Range::new(6, 7),
|
||||
Range::new(4, 5),
|
||||
Range::new(3, 4),
|
||||
Range::new(0, 6),
|
||||
Range::new(7, 8),
|
||||
Range::new(9, 13),
|
||||
Range::new(13, 14),
|
||||
],
|
||||
0,
|
||||
);
|
||||
|
||||
let res = sel
|
||||
.ranges
|
||||
.into_iter()
|
||||
.map(|range| format!("{}/{}", range.anchor, range.head))
|
||||
.collect::<Vec<String>>()
|
||||
.join(",");
|
||||
|
||||
assert_eq!(res, "0/6,6/7,7/8,9/13,13/14");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_merges_adjacent_points() {
|
||||
let sel = Selection::new(
|
||||
vec![
|
||||
Range::new(10, 12),
|
||||
Range::new(12, 12),
|
||||
Range::new(12, 12),
|
||||
Range::new(10, 10),
|
||||
Range::new(8, 10),
|
||||
],
|
||||
0,
|
||||
);
|
||||
|
||||
let res = sel
|
||||
.ranges
|
||||
.into_iter()
|
||||
.map(|range| format!("{}/{}", range.anchor, range.head))
|
||||
.collect::<Vec<String>>()
|
||||
.join(",");
|
||||
|
||||
assert_eq!(res, "8/10,10/12");
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
use crate::{Buffer, Selection};
|
||||
|
||||
/// A state represents the current editor state of a single buffer.
|
||||
pub struct State {
|
||||
// TODO: maybe doc: ?
|
||||
buffer: Buffer,
|
||||
selection: Selection,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(buffer: Buffer) -> Self {
|
||||
Self {
|
||||
buffer,
|
||||
selection: Selection::single(0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: buf/selection accessors
|
||||
|
||||
// update/transact
|
||||
// replaceSelection (transaction that replaces selection)
|
||||
// changeByRange
|
||||
// changes
|
||||
// slice
|
||||
//
|
||||
// getters:
|
||||
// tabSize
|
||||
// indentUnit
|
||||
// languageDataAt()
|
||||
//
|
||||
// config:
|
||||
// indentation
|
||||
// tabSize
|
||||
// lineUnit
|
||||
// syntax
|
||||
// foldable
|
||||
// changeFilter/transactionFilter
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
pub struct Change {
|
||||
from: usize,
|
||||
to: usize,
|
||||
insert: Option<String>,
|
||||
}
|
||||
|
||||
impl Change {
|
||||
pub fn new(from: usize, to: usize, insert: Option<String>) {
|
||||
// old_extent, new_extent, insert
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Transaction {}
|
||||
|
||||
// ChangeSpec = Change | ChangeSet | Vec<Change>
|
||||
// ChangeDesc as a ChangeSet without text: can't be applied, cheaper to store.
|
||||
// ChangeSet = ChangeDesc with Text
|
||||
pub struct ChangeSet {
|
||||
// basically Vec<ChangeDesc> where ChangeDesc = (current len, replacement len?)
|
||||
// (0, n>0) for insertion, (n>0, 0) for deletion, (>0, >0) for replacement
|
||||
sections: Vec<(usize, isize)>,
|
||||
}
|
||||
//
|
||||
// trait Transaction
|
||||
// trait StrictTransaction
|
@ -0,0 +1,281 @@
|
||||
#![allow(unused)]
|
||||
use anyhow::Error;
|
||||
use termwiz::caps::Capabilities;
|
||||
use termwiz::cell::AttributeChange;
|
||||
use termwiz::color::{AnsiColor, ColorAttribute, RgbColor};
|
||||
use termwiz::input::*;
|
||||
use termwiz::surface::Change;
|
||||
use termwiz::terminal::{buffered::BufferedTerminal, SystemTerminal, Terminal};
|
||||
use termwiz::widgets::*;
|
||||
|
||||
use crate::Args;
|
||||
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
use helix_core::Buffer;
|
||||
|
||||
/// This is a widget for our application
|
||||
pub struct MainScreen {}
|
||||
|
||||
impl MainScreen {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for MainScreen {
|
||||
fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
|
||||
true // handled it all
|
||||
}
|
||||
|
||||
/// Draw ourselves into the surface provided by RenderArgs
|
||||
fn render(&mut self, args: &mut RenderArgs) {
|
||||
// args.surface.add_change(Change::ClearScreen(
|
||||
// ColorAttribute::TrueColorWithPaletteFallback(
|
||||
// RgbColor::new(0x31, 0x1B, 0x92),
|
||||
// AnsiColor::Black.into(),
|
||||
// ),
|
||||
// ));
|
||||
// args.surface
|
||||
// .add_change(Change::Attribute(AttributeChange::Foreground(
|
||||
// ColorAttribute::TrueColorWithPaletteFallback(
|
||||
// RgbColor::new(0xB3, 0x88, 0xFF),
|
||||
// AnsiColor::Purple.into(),
|
||||
// ),
|
||||
// )));
|
||||
}
|
||||
|
||||
fn get_size_constraints(&self) -> layout::Constraints {
|
||||
let mut constraints = layout::Constraints::default();
|
||||
constraints.child_orientation = layout::ChildOrientation::Vertical;
|
||||
constraints
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BufferComponent<'a> {
|
||||
text: String,
|
||||
buffer: &'a mut Buffer,
|
||||
|
||||
first_line: usize,
|
||||
}
|
||||
|
||||
impl<'a> BufferComponent<'a> {
|
||||
/// Initialize the widget with the input text
|
||||
pub fn new(buffer: &'a mut Buffer) -> Self {
|
||||
Self {
|
||||
buffer,
|
||||
text: String::new(),
|
||||
|
||||
first_line: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Widget for BufferComponent<'a> {
|
||||
fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
|
||||
match event {
|
||||
WidgetEvent::Input(InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char('k'),
|
||||
..
|
||||
})) => {
|
||||
self.first_line = self.first_line.saturating_sub(1);
|
||||
}
|
||||
WidgetEvent::Input(InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char('j'),
|
||||
..
|
||||
})) => {
|
||||
self.first_line = self.first_line.saturating_add(1);
|
||||
}
|
||||
WidgetEvent::Input(InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Enter,
|
||||
..
|
||||
})) => {
|
||||
self.text.push_str("\r\n");
|
||||
}
|
||||
WidgetEvent::Input(InputEvent::Paste(s)) => {
|
||||
self.text.push_str(&s);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
true // handled it all
|
||||
}
|
||||
|
||||
/// Draw ourselves into the surface provided by RenderArgs
|
||||
fn render(&mut self, args: &mut RenderArgs) {
|
||||
args.surface
|
||||
.add_change(Change::ClearScreen(ColorAttribute::Default));
|
||||
|
||||
// args.surface
|
||||
// .add_change(Change::Attribute(AttributeChange::Foreground(
|
||||
// ColorAttribute::TrueColorWithPaletteFallback(
|
||||
// RgbColor::new(0x11, 0x00, 0xFF),
|
||||
// AnsiColor::Purple.into(),
|
||||
// ),
|
||||
// )));
|
||||
let (_width, height) = args.surface.dimensions();
|
||||
|
||||
for line in self.buffer.contents.lines_at(self.first_line).take(height) {
|
||||
args.surface
|
||||
.add_change(unsafe { String::from_utf8_unchecked(line.bytes().collect()) });
|
||||
args.surface.add_change("\r");
|
||||
}
|
||||
// args.surface
|
||||
// .add_change(format!("🤷 surface size is {:?}\r\n", dims));
|
||||
// args.surface.add_change(self.text.clone());
|
||||
|
||||
// Place the cursor at the end of the text.
|
||||
// A more advanced text editing widget would manage the
|
||||
// cursor position differently.
|
||||
*args.cursor = CursorShapeAndPosition {
|
||||
coords: args.surface.cursor_position().into(),
|
||||
shape: termwiz::surface::CursorShape::SteadyBar,
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
fn get_size_constraints(&self) -> layout::Constraints {
|
||||
let mut c = layout::Constraints::default();
|
||||
c.set_valign(layout::VerticalAlignment::Top);
|
||||
c
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StatusLine {}
|
||||
|
||||
impl StatusLine {
|
||||
pub fn new() -> Self {
|
||||
StatusLine {}
|
||||
}
|
||||
}
|
||||
impl Widget for StatusLine {
|
||||
fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn render(&mut self, args: &mut RenderArgs) {
|
||||
args.surface.add_change(Change::ClearScreen(
|
||||
ColorAttribute::TrueColorWithPaletteFallback(
|
||||
RgbColor::new(0xFF, 0xFF, 0xFF),
|
||||
AnsiColor::Black.into(),
|
||||
),
|
||||
));
|
||||
args.surface
|
||||
.add_change(Change::Attribute(AttributeChange::Foreground(
|
||||
ColorAttribute::TrueColorWithPaletteFallback(
|
||||
RgbColor::new(0x00, 0x00, 0x00),
|
||||
AnsiColor::Black.into(),
|
||||
),
|
||||
)));
|
||||
|
||||
args.surface.add_change(" helix");
|
||||
}
|
||||
|
||||
fn get_size_constraints(&self) -> layout::Constraints {
|
||||
*layout::Constraints::default()
|
||||
.set_fixed_height(1)
|
||||
.set_valign(layout::VerticalAlignment::Bottom)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Editor {
|
||||
terminal: BufferedTerminal<SystemTerminal>,
|
||||
|
||||
buffer: Option<Buffer>,
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub fn new(mut args: Args) -> Result<Self, Error> {
|
||||
// Create a terminal
|
||||
let caps = Capabilities::new_from_env()?;
|
||||
let mut terminal = BufferedTerminal::new(SystemTerminal::new(caps)?)?;
|
||||
|
||||
let mut editor = Editor {
|
||||
terminal,
|
||||
buffer: None,
|
||||
};
|
||||
|
||||
if let Some(file) = args.files.pop() {
|
||||
editor.open(file)?;
|
||||
}
|
||||
|
||||
Ok(editor)
|
||||
}
|
||||
|
||||
pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
|
||||
let buffer = Buffer::load(path)?;
|
||||
self.buffer = Some(buffer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> Result<(), Error> {
|
||||
// Start with an empty string; typing into the app will
|
||||
// update this string.
|
||||
let mut typed_text = String::new();
|
||||
|
||||
{
|
||||
let buf = &mut self.terminal;
|
||||
// Put the terminal in raw mode + alternate screen
|
||||
buf.terminal().enter_alternate_screen()?;
|
||||
buf.terminal().set_raw_mode()?;
|
||||
|
||||
// Set up the UI
|
||||
let mut ui = Ui::new();
|
||||
|
||||
let root_id = ui.set_root(MainScreen::new());
|
||||
let buffer_id =
|
||||
ui.add_child(root_id, BufferComponent::new(self.buffer.as_mut().unwrap()));
|
||||
// let root_id = ui.set_root(Buffer::new(&mut typed_text));
|
||||
ui.add_child(root_id, StatusLine::new());
|
||||
ui.set_focus(buffer_id);
|
||||
|
||||
loop {
|
||||
ui.process_event_queue()?;
|
||||
|
||||
// After updating and processing all of the widgets, compose them
|
||||
// and render them to the screen.
|
||||
if ui.render_to_screen(buf)? {
|
||||
// We have more events to process immediately; don't block waiting
|
||||
// for input below, but jump to the top of the loop to re-run the
|
||||
// updates.
|
||||
continue;
|
||||
}
|
||||
// Compute an optimized delta to apply to the terminal and display it
|
||||
buf.flush()?;
|
||||
|
||||
// Wait for user input
|
||||
match buf.terminal().poll_input(None) {
|
||||
Ok(Some(InputEvent::Resized { rows, cols })) => {
|
||||
// FIXME: this is working around a bug where we don't realize
|
||||
// that we should redraw everything on resize in BufferedTerminal.
|
||||
buf.add_change(Change::ClearScreen(Default::default()));
|
||||
buf.resize(cols, rows);
|
||||
}
|
||||
Ok(Some(input)) => match input {
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Escape,
|
||||
..
|
||||
}) => {
|
||||
// Quit the app when escape is pressed
|
||||
break;
|
||||
}
|
||||
input @ _ => {
|
||||
// Feed input into the Ui
|
||||
ui.queue_event(WidgetEvent::Input(input));
|
||||
}
|
||||
},
|
||||
Ok(None) => {}
|
||||
Err(e) => {
|
||||
print!("{:?}\r\n", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// After we've stopped the full screen raw terminal,
|
||||
// print out the final edited value of the input text.
|
||||
println!("The text you entered: {}", typed_text);
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -1,227 +1,24 @@
|
||||
//! This example shows how to make a basic widget that accumulates
|
||||
//! text input and renders it to the screen
|
||||
#![allow(unused)]
|
||||
use anyhow::Error;
|
||||
use termwiz::caps::Capabilities;
|
||||
use termwiz::cell::AttributeChange;
|
||||
use termwiz::color::{AnsiColor, ColorAttribute, RgbColor};
|
||||
use termwiz::input::*;
|
||||
use termwiz::surface::Change;
|
||||
use termwiz::terminal::buffered::BufferedTerminal;
|
||||
use termwiz::terminal::{new_terminal, Terminal};
|
||||
use termwiz::widgets::*;
|
||||
|
||||
/// This is a widget for our application
|
||||
struct MainScreen {}
|
||||
|
||||
impl MainScreen {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for MainScreen {
|
||||
fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
|
||||
true // handled it all
|
||||
}
|
||||
|
||||
/// Draw ourselves into the surface provided by RenderArgs
|
||||
fn render(&mut self, args: &mut RenderArgs) {
|
||||
// args.surface.add_change(Change::ClearScreen(
|
||||
// ColorAttribute::TrueColorWithPaletteFallback(
|
||||
// RgbColor::new(0x31, 0x1B, 0x92),
|
||||
// AnsiColor::Black.into(),
|
||||
// ),
|
||||
// ));
|
||||
// args.surface
|
||||
// .add_change(Change::Attribute(AttributeChange::Foreground(
|
||||
// ColorAttribute::TrueColorWithPaletteFallback(
|
||||
// RgbColor::new(0xB3, 0x88, 0xFF),
|
||||
// AnsiColor::Purple.into(),
|
||||
// ),
|
||||
// )));
|
||||
}
|
||||
|
||||
fn get_size_constraints(&self) -> layout::Constraints {
|
||||
let mut constraints = layout::Constraints::default();
|
||||
constraints.child_orientation = layout::ChildOrientation::Vertical;
|
||||
constraints
|
||||
}
|
||||
}
|
||||
|
||||
struct Buffer<'a> {
|
||||
/// Holds the input text that we wish the widget to display
|
||||
text: &'a mut String,
|
||||
}
|
||||
|
||||
impl<'a> Buffer<'a> {
|
||||
/// Initialize the widget with the input text
|
||||
pub fn new(text: &'a mut String) -> Self {
|
||||
Self { text }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Widget for Buffer<'a> {
|
||||
fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
|
||||
match event {
|
||||
WidgetEvent::Input(InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Char(c),
|
||||
..
|
||||
})) => self.text.push(*c),
|
||||
WidgetEvent::Input(InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Enter,
|
||||
..
|
||||
})) => {
|
||||
self.text.push_str("\r\n");
|
||||
}
|
||||
WidgetEvent::Input(InputEvent::Paste(s)) => {
|
||||
self.text.push_str(&s);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
true // handled it all
|
||||
}
|
||||
mod editor;
|
||||
|
||||
/// Draw ourselves into the surface provided by RenderArgs
|
||||
fn render(&mut self, args: &mut RenderArgs) {
|
||||
args.surface
|
||||
.add_change(Change::ClearScreen(ColorAttribute::Default));
|
||||
use editor::Editor;
|
||||
|
||||
// args.surface
|
||||
// .add_change(Change::Attribute(AttributeChange::Foreground(
|
||||
// ColorAttribute::TrueColorWithPaletteFallback(
|
||||
// RgbColor::new(0x11, 0x00, 0xFF),
|
||||
// AnsiColor::Purple.into(),
|
||||
// ),
|
||||
// )));
|
||||
let dims = args.surface.dimensions();
|
||||
args.surface
|
||||
.add_change(format!("🤷 surface size is {:?}\r\n", dims));
|
||||
args.surface.add_change(self.text.clone());
|
||||
use argh::FromArgs;
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
// Place the cursor at the end of the text.
|
||||
// A more advanced text editing widget would manage the
|
||||
// cursor position differently.
|
||||
*args.cursor = CursorShapeAndPosition {
|
||||
coords: args.surface.cursor_position().into(),
|
||||
shape: termwiz::surface::CursorShape::SteadyBar,
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
fn get_size_constraints(&self) -> layout::Constraints {
|
||||
let mut c = layout::Constraints::default();
|
||||
c.set_valign(layout::VerticalAlignment::Top);
|
||||
c
|
||||
}
|
||||
}
|
||||
|
||||
struct StatusLine {}
|
||||
|
||||
impl StatusLine {
|
||||
pub fn new() -> Self {
|
||||
StatusLine {}
|
||||
}
|
||||
}
|
||||
impl Widget for StatusLine {
|
||||
fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn render(&mut self, args: &mut RenderArgs) {
|
||||
args.surface.add_change(Change::ClearScreen(
|
||||
ColorAttribute::TrueColorWithPaletteFallback(
|
||||
RgbColor::new(0xFF, 0xFF, 0xFF),
|
||||
AnsiColor::Black.into(),
|
||||
),
|
||||
));
|
||||
args.surface
|
||||
.add_change(Change::Attribute(AttributeChange::Foreground(
|
||||
ColorAttribute::TrueColorWithPaletteFallback(
|
||||
RgbColor::new(0x00, 0x00, 0x00),
|
||||
AnsiColor::Black.into(),
|
||||
),
|
||||
)));
|
||||
|
||||
args.surface.add_change(" helix");
|
||||
}
|
||||
use anyhow::Error;
|
||||
|
||||
fn get_size_constraints(&self) -> layout::Constraints {
|
||||
*layout::Constraints::default()
|
||||
.set_fixed_height(1)
|
||||
.set_valign(layout::VerticalAlignment::Bottom)
|
||||
}
|
||||
#[derive(FromArgs)]
|
||||
/// A post-modern text editor.
|
||||
pub struct Args {
|
||||
#[argh(positional)]
|
||||
files: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
// Start with an empty string; typing into the app will
|
||||
// update this string.
|
||||
let mut typed_text = String::new();
|
||||
|
||||
{
|
||||
// Create a terminal and put it into full screen raw mode
|
||||
let caps = Capabilities::new_from_env()?;
|
||||
let mut buf = BufferedTerminal::new(new_terminal(caps)?)?;
|
||||
buf.terminal().enter_alternate_screen()?;
|
||||
buf.terminal().set_raw_mode()?;
|
||||
|
||||
// Set up the UI
|
||||
let mut ui = Ui::new();
|
||||
|
||||
let root_id = ui.set_root(MainScreen::new());
|
||||
let buffer_id = ui.add_child(root_id, Buffer::new(&mut typed_text));
|
||||
// let root_id = ui.set_root(Buffer::new(&mut typed_text));
|
||||
ui.add_child(root_id, StatusLine::new());
|
||||
ui.set_focus(buffer_id);
|
||||
|
||||
loop {
|
||||
ui.process_event_queue()?;
|
||||
|
||||
// After updating and processing all of the widgets, compose them
|
||||
// and render them to the screen.
|
||||
if ui.render_to_screen(&mut buf)? {
|
||||
// We have more events to process immediately; don't block waiting
|
||||
// for input below, but jump to the top of the loop to re-run the
|
||||
// updates.
|
||||
continue;
|
||||
}
|
||||
// Compute an optimized delta to apply to the terminal and display it
|
||||
buf.flush()?;
|
||||
|
||||
// Wait for user input
|
||||
match buf.terminal().poll_input(None) {
|
||||
Ok(Some(InputEvent::Resized { rows, cols })) => {
|
||||
// FIXME: this is working around a bug where we don't realize
|
||||
// that we should redraw everything on resize in BufferedTerminal.
|
||||
buf.add_change(Change::ClearScreen(Default::default()));
|
||||
buf.resize(cols, rows);
|
||||
}
|
||||
Ok(Some(input)) => match input {
|
||||
InputEvent::Key(KeyEvent {
|
||||
key: KeyCode::Escape,
|
||||
..
|
||||
}) => {
|
||||
// Quit the app when escape is pressed
|
||||
break;
|
||||
}
|
||||
input @ _ => {
|
||||
// Feed input into the Ui
|
||||
ui.queue_event(WidgetEvent::Input(input));
|
||||
}
|
||||
},
|
||||
Ok(None) => {}
|
||||
Err(e) => {
|
||||
print!("{:?}\r\n", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let args: Args = argh::from_env();
|
||||
let mut editor = Editor::new(args)?;
|
||||
|
||||
// After we've stopped the full screen raw terminal,
|
||||
// print out the final edited value of the input text.
|
||||
println!("The text you entered: {}", typed_text);
|
||||
editor.run()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue