@@ -15,6 +15,7 @@ pub struct Options {
1515 pub output_is_terminal : bool ,
1616
1717 /// If true, _(default: true)_ we will display color. You should use `output_is_terminal && crosstermion::should_colorize()`
18+ /// to determine this value.
1819 ///
1920 /// Please note that you can enforce color even if the output stream is not connected to a terminal by setting
2021 /// this field to true.
@@ -62,6 +63,51 @@ pub struct Options {
6263 pub keep_running_if_progress_is_empty : bool ,
6364}
6465
66+ /// The kind of stream to use for auto-configuration.
67+ pub enum StreamKind {
68+ /// Standard output
69+ Stdout ,
70+ /// Standard error
71+ Stderr ,
72+ }
73+
74+ #[ cfg( feature = "render-line-autoconfigure" ) ]
75+ impl From < StreamKind > for atty:: Stream {
76+ fn from ( s : StreamKind ) -> Self {
77+ match s {
78+ StreamKind :: Stdout => atty:: Stream :: Stdout ,
79+ StreamKind :: Stderr => atty:: Stream :: Stderr ,
80+ }
81+ }
82+ }
83+
84+ /// Convenience
85+ impl Options {
86+ /// Automatically configure (and overwrite) the following fields based on terminal configuration.
87+ ///
88+ /// * output_is_terminal
89+ /// * colored
90+ /// * terminal_dimensions
91+ /// * hide-cursor (based on presence of 'ctrlc' feature.
92+ #[ cfg( feature = "render-line-autoconfigure" ) ]
93+ pub fn auto_configure ( mut self , output : StreamKind ) -> Self {
94+ self . output_is_terminal = atty:: is ( output. into ( ) ) ;
95+ self . colored = self . output_is_terminal && crosstermion:: color:: allowed ( ) ;
96+ self . terminal_dimensions = crosstermion:: terminal:: size ( ) . unwrap_or ( ( 80 , 20 ) ) ;
97+ self . auto_hide_cursor ( ) ;
98+ self
99+ }
100+ #[ cfg( all( feature = "render-line-autoconfigure" , feature = "ctrlc" ) ) ]
101+ fn auto_hide_cursor ( & mut self ) {
102+ self . hide_cursor = true ;
103+ }
104+ #[ cfg( not( feature = "render-line-autoconfigure" ) ) ]
105+ /// No-op - only available with the `render-line-autoconfigure` feature toggle.
106+ pub fn auto_configure ( self , _output : StreamKind ) -> Self {
107+ self
108+ }
109+ }
110+
65111impl Default for Options {
66112 fn default ( ) -> Self {
67113 Options {
0 commit comments