Skip to content

Commit

Permalink
[IT-24] add example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Shvyryalkin committed Dec 14, 2024
1 parent d31c54c commit 6f5c449
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions examples/custom_cmd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "custom_cmd"
version = "0.5.1"
edition = "2021"
publish = false

[dependencies]
iced = "0.13.1"
iced_term = { path = "../../" }
5 changes: 5 additions & 0 deletions examples/custom_cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Fullscreen example

Example of basic ICED app with using iced_term widget.

![screenshot](./assets/screenshot.png)
Binary file not shown.
Binary file added examples/custom_cmd/assets/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions examples/custom_cmd/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use iced::advanced::graphics::core::Element;
use iced::font::{Family, Stretch, Weight};
use iced::widget::container;
use iced::{Font, Length, Size, Subscription, Task, Theme};
use iced_term::TerminalView;

fn main() -> iced::Result {
iced::application(App::title, App::update, App::view)
.antialiasing(false)
.window_size(Size {
width: 1280.0,
height: 720.0,
})
.subscription(App::subscription)
.run_with(App::new)
}

#[derive(Debug, Clone)]
pub enum Event {
Terminal(iced_term::Event),
}

struct App {
title: String,
term: iced_term::Terminal,
}

impl App {
fn new() -> (Self, Task<Event>) {
let system_shell = std::env::var("SHELL")
.expect("SHELL variable is not defined")
.to_string();
let term_id = 0;
let term_settings = iced_term::settings::Settings {
font: iced_term::settings::FontSettings {
size: 14.0,
font_type: Font {
weight: Weight::Bold,
family: Family::Name("JetBrainsMono Nerd Font Mono"),
stretch: Stretch::Normal,
..Default::default()
},
..Default::default()
},
theme: iced_term::settings::ThemeSettings::default(),
backend: iced_term::settings::BackendSettings {
cmd: "echo".to_string(),
args: vec!["101".to_string()]
},
};

(
Self {
title: String::from("custom_cmd"),
term: iced_term::Terminal::new(term_id, term_settings),
},
Task::none(),
)
}

fn title(&self) -> String {
self.title.clone()
}

fn subscription(&self) -> Subscription<Event> {
let term_subscription = iced_term::Subscription::new(self.term.id);
let term_event_stream = term_subscription.event_stream();
Subscription::run_with_id(self.term.id, term_event_stream)
.map(Event::Terminal)
}

fn update(&mut self, event: Event) -> Task<Event> {
println!("{:?}", event);
match event {
Event::Terminal(iced_term::Event::CommandReceived(_, cmd)) => {
match self.term.update(cmd) {
iced_term::actions::Action::ChangeTitle(title) => {
self.title = title;
Task::none()
},
_ => Task::none(),
}
},
}
}

fn view(&self) -> Element<Event, Theme, iced::Renderer> {
container(TerminalView::show(&self.term).map(Event::Terminal))
.width(Length::Fill)
.height(Length::Fill)
.into()
}
}
10 changes: 10 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,17 @@ impl Backend {
self.internal_sync(&mut term);
action = Action::Redraw;
},
Event::ChildExit(_) => {
self.internal_sync(&mut term);
for c in self.last_content.grid.display_iter() {
if c.c != ' ' && c.c != '\t' {
println!("{:?}", c);
}
}
action = Action::Redraw;
}
Event::Exit => {
self.internal_sync(&mut term);
action = Action::Shutdown;
},
Event::Title(title) => {
Expand Down
2 changes: 1 addition & 1 deletion src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Terminal {
Command::ProcessBackendCommand(c) => {
if let Some(ref mut backend) = self.backend {
action = backend.process_command(c);
if action == Action::Redraw {
if action == Action::Redraw || action == Action::Shutdown {
self.redraw();
}
}
Expand Down

0 comments on commit 6f5c449

Please sign in to comment.