Skip to content

Commit

Permalink
Merge pull request #44 from TimonPost/screen_refactor
Browse files Browse the repository at this point in the history
Putted `Screen` behind an `Option`. Now when you call the functions: color, cursor, terminal, input you won't need to provide a screen for it anymore. 

When you want to work with the alternate screen you can call the following functions: `terminal::from_screen` etc. Which will give you an instance to the back of the module you are calling it in.

So instead of:

```
let color = color(Screen::default());
let cursor = cursor(Screen::default());
let input = input(Screen::default());
let terminal = terminal(Screen::default());
```
You can do: 
```
let color = color();
let cursor = cursor();
let input = input();
let terminal = terminal();
```

which looks much better too.
  • Loading branch information
TimonPost committed Nov 21, 2018
2 parents 3dfa98c + b717d30 commit 8ac61db
Show file tree
Hide file tree
Showing 45 changed files with 681 additions and 505 deletions.
5 changes: 5 additions & 0 deletions docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Changes crossterm 0.5.0
- Implemented Display for styled object.
- Removed `Screen` from the following functions: `crossterm::cursor(), crossterm::color(), crossterm::terminal()`, you won't need to care about `Screen` unless you are working with alternate or raw screen.
- more to come ...

# Changes crossterm to 0.4.3
- Fixed bug [issue 41](https://github.com/TimonPost/crossterm/issues/41)

Expand Down
130 changes: 58 additions & 72 deletions examples/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,125 +8,111 @@ use self::crossterm::{terminal, Screen};

/// print some red font | demonstration.
pub fn paint_foreground() {


let screen = Screen::default();
// Pass an string to the `paint()` method with you want to paint.
// This will give you an object back wits can be styled and displayed.
// Create a styled object.
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum.
let mut styledobject = style("Red foreground").with(Color::Red);

// Print the object to the given screen and.
styledobject.paint(&screen);

style("Some colored text").with(Color::Blue).on(Color::Black).paint(&screen);
println!("Colored text: {}", styledobject);

// Crossterm provides method chaining for coloring so that the above points can be inlined.
style(format!("Red foreground color : \t {}", "■")).with(Color::Red).paint(&screen);
// Or print inline
println!("Colored text: {}", style("Red foreground").with(Color::Blue));
}

/// print some font on red background | demonstration.
pub fn paint_background() {
let screen = Screen::default();
// Pass an string to the `paint()` method with you want to paint.
// This will give you an object back wits can be styled and displayed.
// Call the method `on()` on the object given by `style()` and pass in any Color from the Color enum.
let mut styledobject = style("Red background color").on(Color::Red);
// Create a styled object.
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum.
let mut styledobject = style("Red foreground").on(Color::Red);

// Print the object to the given screen and.
styledobject.paint(&screen);
println!("Colored text: {}", styledobject);

// Crossterm provides method chaining for coloring so that the above points can be inlined.
style(format!("Red background color : \t {}", "■")).with(Color::Red).paint(&screen);
// Or print inline
println!("Colored text: {}", style("Red foreground").on(Color::Blue));
}

/// Print all available foreground colors | demonstration.
pub fn print_all_foreground_colors() {
let screen = Screen::default();

style(format!("Black : \t\t {} \n", "■")).with(Color::Black).paint(&screen);
style(format!("Red : \t\t {} \n", "■")).with(Color::Red).paint(&screen);
style(format!("Cyan : \t\t {} \n", "■")).with(Color::Cyan).paint(&screen);
style(format!("DarkCyan : \t {} \n", "■")).with(Color::DarkCyan).paint(&screen);
style(format!("DarkRed : \t {} \n", "■")).with(Color::DarkRed).paint(&screen);
style(format!("Green : \t {} \n", "■")).with(Color::Green).paint(&screen);
style(format!("DarkGreen : \t {} \n", "■")).with(Color::DarkGreen).paint(&screen);
style(format!("Blue : \t\t {} \n", "■")).with(Color::Blue).paint(&screen);
style(format!("DarkBlue : \t {} \n", "■")).with(Color::DarkBlue).paint(&screen);
style(format!("Magenta : \t {} \n", "■")).with(Color::Magenta).paint(&screen);
style(format!("DarkMagenta : \t {} \n", "■")).with(Color::DarkMagenta).paint(&screen);
style(format!("Yellow : \t {} \n", "■")).with(Color::Yellow).paint(&screen);
style(format!("DarkYellow : \t {} \n", "■")).with(Color::DarkYellow).paint(&screen);
style(format!("Grey : \t\t {} \n", "■")).with(Color::Grey).paint(&screen);
style(format!("White : \t {} \n", "■")).with(Color::White).paint(&screen);
println!("{}", style(format!("Black : \t\t {} \n", "■")).with(Color::Black));
println!("{}", style(format!("Red : \t\t {} \n", "■")).with(Color::Red));
println!("{}", style(format!("Cyan : \t\t {} \n", "■")).with(Color::Cyan));
println!("{}", style(format!("DarkCyan : \t {} \n", "■")).with(Color::DarkCyan));
println!("{}", style(format!("DarkRed : \t {} \n", "■")).with(Color::DarkRed));
println!("{}", style(format!("Green : \t {} \n", "■")).with(Color::Green));
println!("{}", style(format!("DarkGreen : \t {} \n", "■")).with(Color::DarkGreen));
println!("{}", style(format!("Blue : \t\t {} \n", "■")).with(Color::Blue));
println!("{}", style(format!("DarkBlue : \t {} \n", "■")).with(Color::DarkBlue));
println!("{}", style(format!("Magenta : \t {} \n", "■")).with(Color::Magenta));
println!("{}", style(format!("DarkMagenta : \t {} \n", "■")).with(Color::DarkMagenta));
println!("{}", style(format!("Yellow : \t {} \n", "■")).with(Color::Yellow));
println!("{}", style(format!("DarkYellow : \t {} \n", "■")).with(Color::DarkYellow));
println!("{}", style(format!("Grey : \t\t {} \n", "■")).with(Color::Grey));
println!("{}", style(format!("White : \t {} \n", "■")).with(Color::White));

#[cfg(unix)]
style("RGB color (10,10,10) ").with(Color::Rgb {
println!("{}", style("RGB color (10,10,10) ").with(Color::Rgb {
r: 10,
g: 10,
b: 10
}).paint(&screen);
}));

#[cfg(unix)]
style("RGB color (10,10,10) ").with(Color::AnsiValue(50)).paint(&screen);
println!("{}",style("RGB color (10,10,10) ")).with(Color::AnsiValue(50));
}

/// Print all available foreground colors | demonstration.
pub fn print_all_background_colors() {
let screen = Screen::default();

style(format!("Black : \t {} \n", "■")).on(Color::Black).paint(&screen);
style(format!("Red : \t\t {} \n", "■")).on(Color::Red).paint(&screen);
style(format!("Cyan : \t\t {} \n", "■")).on(Color::Cyan).paint(&screen);
style(format!("DarkCyan : \t {} \n", "■")).on(Color::DarkCyan).paint(&screen);
style(format!("DarkRed : \t {} \n", "■")).on(Color::DarkRed).paint(&screen);
style(format!("Green : \t {} \n", "■")).on(Color::Green).paint(&screen);
style(format!("DarkGreen : \t {} \n", "■")).on(Color::DarkGreen).paint(&screen);
style(format!("Blue : \t\t {} \n", "■")).on(Color::Blue).paint(&screen);
style(format!("DarkBlue : \t {} \n", "■")).on(Color::DarkBlue).paint(&screen);
style(format!("Magenta : \t {} \n", "■")).on(Color::Magenta).paint(&screen);
style(format!("DarkMagenta : \t {} \n", "■")).on(Color::DarkMagenta).paint(&screen);
style(format!("Yellow : \t {} \n", "■")).on(Color::Yellow).paint(&screen);
style(format!("DarkYellow : \t {} \n", "■")).on(Color::DarkYellow).paint(&screen);
style(format!("Grey : \t\t {} \n", "■")).on(Color::Grey).paint(&screen);
style(format!("White : \t {} \n", "■")).on(Color::White).paint(&screen);
println!("{}", style(format!("Black : \t {} \n", "■")).on(Color::Black));
println!("{}", style(format!("Red : \t\t {} \n", "■")).on(Color::Red));
println!("{}", style(format!("Cyan : \t\t {} \n", "■")).on(Color::Cyan));
println!("{}", style(format!("DarkCyan : \t {} \n", "■")).on(Color::DarkCyan));
println!("{}", style(format!("DarkRed : \t {} \n", "■")).on(Color::DarkRed));
println!("{}", style(format!("Green : \t {} \n", "■")).on(Color::Green));
println!("{}", style(format!("DarkGreen : \t {} \n", "■")).on(Color::DarkGreen));
println!("{}", style(format!("Blue : \t\t {} \n", "■")).on(Color::Blue));
println!("{}", style(format!("DarkBlue : \t {} \n", "■")).on(Color::DarkBlue));
println!("{}", style(format!("Magenta : \t {} \n", "■")).on(Color::Magenta));
println!("{}", style(format!("DarkMagenta : \t {} \n", "■")).on(Color::DarkMagenta));
println!("{}", style(format!("Yellow : \t {} \n", "■")).on(Color::Yellow));
println!("{}", style(format!("DarkYellow : \t {} \n", "■")).on(Color::DarkYellow));
println!("{}", style(format!("Grey : \t\t {} \n", "■")).on(Color::Grey));
println!("{}", style(format!("White : \t {} \n", "■")).on(Color::White));

#[cfg(unix)]
style("RGB color (10,10,10) ").on(Color::Rgb {
println!("{}", style("RGB color (10,10,10) ").on(Color::Rgb {
r: 10,
g: 10,
b: 10
}).paint(&screen);
}));

#[cfg(unix)]
style("RGB color (10,10,10) ").on(Color::AnsiValue(50)).paint(&screen);
println!("{}",style("RGB color (10,10,10) ").on(Color::AnsiValue(50)));
}

/// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely | demonstration..
#[cfg(unix)]
pub fn print_font_with_attributes() {
let screen = Screen::default();
style("Normal text").paint(&screen);
style("Bold text").bold().paint(&screen);
style("Italic text").italic().paint(&screen);
style("Slow blinking text").slow_blink().paint(&screen);
style("Rapid blinking text").rapid_blink().paint(&screen);
style("Hidden text").hidden().paint(&screen);
style("Underlined text").underlined().paint(&screen);
style("Reversed text").reverse().paint(&screen);
style("Dim text").dim().paint(&screen);
style("Crossed out font").crossed_out().paint(&screen);
println!("{}",style("Normal text"));
println!("{}",style("Bold text").bold());
println!("{}",style("Italic text").italic());
println!("{}",style("Slow blinking text").slow_blink());
println!("{}",style("Rapid blinking text").rapid_blink());
println!("{}",style("Hidden text").hidden());
println!("{}",style("Underlined text").underlined());
println!("{}",style("Reversed text").reverse());
println!("{}",style("Dim text").dim());
println!("{}",style("Crossed out font").crossed_out());
}

/// Print all supported RGB colors | demonstration.
#[cfg(unix)]
pub fn print_supported_colors() {
let screen = Screen::default();
let count = color(&screen)
let count = color()
.get_available_color_count()
.unwrap();

for i in 0..count {
style(format!("White : \t {}", i)).on(Color::AnsiValue(i as u8)).paint(&screen);
println!("{}", style(format!("White : \t {}", i)).on(Color::AnsiValue(i as u8)));
}
}
30 changes: 10 additions & 20 deletions examples/cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,45 @@ use self::crossterm::Screen;
/// Set the cursor to position X: 10, Y: 5 in the terminal.
pub fn goto() {
// Get the cursor
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();
// Set the cursor to position X: 10, Y: 5 in the terminal
cursor.goto(10, 5);
}

/// get the cursor position
pub fn pos() {
// Get the cursor
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();
// get the cursor position.
let (x, y) = cursor.pos();
}

/// Move the cursor 3 up | demonstration.
pub fn move_up() {
// Get the cursor
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();

// Move the cursor to position 3 times to the up in the terminal
cursor.move_up(10);
}

/// Move the cursor 3 to the right | demonstration.
pub fn move_right() {
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();
// Move the cursor to position 3 times to the right in the terminal
cursor.move_right(3);
}

/// Move the cursor 3 down | demonstration.
pub fn move_down() {
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();
// Move the cursor to position 3 times to the down in the terminal
cursor.move_down(3);
}

/// Move the cursor 3 to the left | demonstration.
pub fn move_left() {
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();

// Move the cursor to position 3 times to the left in the terminal
cursor.move_left(3);
Expand Down Expand Up @@ -87,8 +81,7 @@ pub fn move_left() {

/// Save and reset cursor position | demonstration..
pub fn safe_and_reset_position() {
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();

// Goto X: 5 Y: 5
cursor.goto(5, 5);
Expand All @@ -108,22 +101,19 @@ pub fn safe_and_reset_position() {

/// Hide cursor display | demonstration.
pub fn hide_cursor() {
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();
cursor.hide();
}

/// Show cursor display | demonstration.
pub fn show_cursor() {
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();
cursor.show();
}

/// Show cursor display, only works on certain terminals.| demonstration
pub fn blink_cursor() {
let screen = Screen::default();
let mut cursor = cursor(&screen);
let mut cursor = cursor();
cursor.blink(false);
cursor.blink(false);
}
2 changes: 1 addition & 1 deletion examples/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern crate crossterm;

// modules that could be test
//mod terminal;
mod terminal;
mod color;
mod cursor;
mod some_types;
Expand Down
10 changes: 4 additions & 6 deletions examples/input/keyboard/async_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use std::time::Duration;
/// this will capture the input until the given key.
pub fn read_async_until() {
// create raw screen
let screen = Screen::default();
let crossterm = Crossterm::new(&screen);
let crossterm = Crossterm::new();

// init some modules we use for this demo
let input = crossterm.input();
Expand Down Expand Up @@ -44,8 +43,7 @@ pub fn read_async_until() {

/// this will read pressed characters async until `x` is typed.
pub fn read_async() {
let screen = Screen::default();
let input = input(&screen);
let input = input();

let mut stdin = input.read_async().bytes();

Expand All @@ -65,7 +63,7 @@ pub fn read_async() {

pub fn read_async_demo() {
let screen = Screen::new(true);
let crossterm = Crossterm::new(&screen);
let crossterm = Crossterm::from_screen(&screen);

// init some modules we use for this demo
let input = crossterm.input();
Expand Down Expand Up @@ -104,7 +102,7 @@ pub fn async_reading_on_alternate_screen() {
// switch to alternate screen
if let Ok(alternate) = screen.enable_alternate_modes(true)
{
let crossterm = Crossterm::new(&alternate.screen);
let crossterm = Crossterm::from_screen(&alternate.screen);
// init some modules we use for this demo
let input = crossterm.input();
let terminal = crossterm.terminal();
Expand Down
6 changes: 2 additions & 4 deletions examples/input/keyboard/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use self::crossterm::input::input;
use self::crossterm::Screen;

pub fn read_char() {
let screen = Screen::default();
let input = input(&screen);
let input = input();

match input.read_char() {
Ok(s) => println!("char typed: {}", s),
Expand All @@ -14,8 +13,7 @@ pub fn read_char() {
}

pub fn read_line() {
let screen = Screen::default();
let input = input(&screen);
let input = input();

match input.read_line() {
Ok(s) => println!("string typed: {}", s),
Expand Down
8 changes: 4 additions & 4 deletions examples/program_examples/command_bar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate crossterm;

use crossterm::{Screen, Crossterm};
use crossterm::terminal::{terminal,Terminal, ClearType};
use crossterm::terminal::{from_screen,Terminal, ClearType};
use crossterm::cursor::{TerminalCursor, cursor};
use crossterm::input::input;
use std::sync::{Arc,Mutex};
Expand All @@ -12,7 +12,7 @@ fn main() {
use crossterm::color;

let screen = Screen::new(true);
let crossterm = Crossterm::new(&screen);
let crossterm = Crossterm::from_screen(&screen);
let cursor = crossterm.cursor();
cursor.hide();

Expand All @@ -23,7 +23,7 @@ fn main() {
let mut count = 0;

thread::spawn(move || {
let input = input(&screen);
let input = input();
let mut stdin = input.read_async().bytes();

loop
Expand Down Expand Up @@ -60,7 +60,7 @@ fn log(input_buf: Arc<Mutex<String>>, screen: &Screen) -> Vec<thread::JoinHandle
{
let mut threads = Vec::with_capacity(10);

let (_, term_height) = terminal(&screen).terminal_size();
let (_, term_height) = from_screen(screen).terminal_size();

for i in 0..1
{
Expand Down
Loading

0 comments on commit 8ac61db

Please sign in to comment.