Added terminal color detection

Added configuration
Cleaned up context args
This commit is contained in:
2023-08-17 10:10:38 -07:00
parent fc4c28543f
commit e0ca8be79f
11 changed files with 282 additions and 103 deletions

View File

@ -1,4 +1,3 @@
use std::io::Write;
use std::io::stdout;
use std::io::stdin;
use std::env;
@ -6,19 +5,33 @@ use std::env;
use termion::{
event::Key,
input::TermRead,
raw::IntoRawMode
raw::IntoRawMode,
color::DetectColors
};
use super::promptbuffer::PromptBuffer;
use crate::command;
use crate::context::Context;
use crate::FormattedText;
#[inline(always)]
pub fn main() -> Result<(), std::io::Error> {
let mut stdout = stdout().into_raw_mode().unwrap();
let mut pb: PromptBuffer = PromptBuffer::new(64);
let mut context: Context = Context::new();
let mut context = Context::new();
// Set color compatibilty
let term_colors = stdout.available_colors().unwrap_or(0);
if term_colors >= 256 {
context.config.term_color_type = 2
} else if term_colors >= 8 {
context.config.term_color_type = 1
} else {
context.config.term_color_type = 0
}
context.config.check();
// Handle command-line arguments
@ -28,7 +41,21 @@ pub fn main() -> Result<(), std::io::Error> {
t.write(&context, &mut stdout)?;
return Ok(());
} else if args.iter().any(|s| s == "--version") {
write!(stdout, "Daisy v{}\r\n", env!("CARGO_PKG_VERSION"))?;
let t = FormattedText::new(format!(
"Daisy v{}\n", env!("CARGO_PKG_VERSION")
));
t.write(&context, &mut stdout)?;
return Ok(());
} else if args.iter().any(|s| s == "--debug") {
let t = FormattedText::new(format!(
concat!(
"Daisy v{}\n",
"Your terminal supports {} colors.\n"
),
env!("CARGO_PKG_VERSION"),
term_colors
));
t.write(&context, &mut stdout)?;
return Ok(());
}
@ -45,7 +72,7 @@ pub fn main() -> Result<(), std::io::Error> {
// while inside a substitution
pb.write_prompt_nocursor(&mut context, &mut stdout)?;
let in_str = pb.enter();
write!(stdout, "\r\n")?;
FormattedText::newline(&mut stdout)?;
if in_str == "" { break; }
if in_str.trim() == "quit" {
@ -83,6 +110,6 @@ pub fn main() -> Result<(), std::io::Error> {
}
}
write!(stdout, "\r\n")?;
FormattedText::newline(&mut stdout)?;
return Ok(());
}