Skip to content

Commit

Permalink
Upgrade rustyline to 10.0.0
Browse files Browse the repository at this point in the history
Signed-off-by: Bin.H <H_H_bin@163.com>
  • Loading branch information
H-H-bin committed Oct 15, 2022
1 parent ca0da97 commit 97cf68e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rustyline = "9.0.0"
rustyline = "10.0.0"
rustyline-derive = "*"
env_logger= "*"
serde = "1.0.130"
serialport = "4.0.1"
chrono = "0.4.19"
[target.'cfg(windows)'.dependencies]
wmi = "0.9.1"
wmi = "0.11.2"
43 changes: 13 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::de::value::UnitDeserializer;
use serde::{Deserialize, Serialize};
use serialport::{available_ports, DataBits, SerialPortType, StopBits};
use std::io::{self, Write};
Expand All @@ -9,7 +10,6 @@ use chrono::{DateTime, Utc};
use std::borrow::Cow::{self, Borrowed, Owned};

use rustyline::completion::{Completer, FilenameCompleter, Pair};
use rustyline::config::OutputStreamType;
use rustyline::error::ReadlineError;
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
use rustyline::hint::{Hinter, HistoryHinter};
Expand Down Expand Up @@ -102,8 +102,8 @@ fn list_com_ports() {
Ok(ports) => {
match ports.len() {
0 => println!("No ports found."),
1 => println!("Found 1 port:"),
n => println!("Found {} ports:", n),
1 => println!("Found 1 serial port:"),
n => println!("Found {} serial ports:", n),
};
for p in ports {
println!(" {}", p.port_name);
Expand Down Expand Up @@ -143,15 +143,14 @@ fn list_com_ports() {
}
}


#[cfg(windows)]
fn get_modem_ports_and_return_vec_struct() -> Result<Vec<Win32_POTSModem>, WMIError> {
fn get_modem_ports() -> Result<Vec<Win32_POTSModem>, WMIError> {
// Creating new COM Port
let com_con = COMLibrary::new()?;
// Create new WMI Connection using COM Port
let wmi_con = WMIConnection::new(com_con.into())?;

// let modem_ports: Vec<Win32_POTSModem> = wmi_con.query()?;

let modem_ports: Vec<Win32_POTSModem> = match wmi_con.query() {
Ok(modem_ports) => modem_ports,
Err(e) => {
Expand All @@ -161,28 +160,13 @@ fn get_modem_ports_and_return_vec_struct() -> Result<Vec<Win32_POTSModem>, WMIEr
Ok(modem_ports)
}

fn main() {
fn main() -> Result<(), ReadlineError> {
list_com_ports();

match get_modem_ports_and_return_vec_struct() {
Ok(modem_ports) => {
for port in &modem_ports {
println!("{:#?}", port);
}
// println!("{}", modem_ports[0].Name);
// println!("{}", modem_ports[0].STATUS);
// println!("{}", modem_ports[0].AttachedTo);
}
Err(e) => {
eprintln!("Error: {}", e);
}
};

// 1. Get port name
//TODO: if there only one port, then just open it, if there more then one port,
// pop up for user to chose one.

let modem_port = get_modem_ports_and_return_vec_struct().unwrap();
let modem_port = get_modem_ports().unwrap();
let port_name = &modem_port[0].AttachedTo;
let baud_rate = 115200;

Expand All @@ -207,7 +191,6 @@ fn main() {
.history_ignore_space(true)
.completion_type(CompletionType::List)
.edit_mode(EditMode::Emacs)
.output_stream(OutputStreamType::Stdout)
.build();
let h = MyHelper {
completer: FilenameCompleter::new(),
Expand All @@ -216,7 +199,7 @@ fn main() {
colored_prompt: "".to_owned(),
validator: MatchingBracketValidator::new(),
};
let mut rl = Editor::with_config(config);
let mut rl = Editor::with_config(config)?;
rl.set_helper(Some(h));
rl.bind_sequence(KeyEvent::alt('n'), Cmd::HistorySearchForward);
rl.bind_sequence(KeyEvent::alt('r'), Cmd::HistorySearchBackward);
Expand All @@ -225,11 +208,11 @@ fn main() {
println!("No previous history.");
}

rl.helper_mut().expect("No helper").colored_prompt = format!("\x1b[1;32m{}❯\x1b[0m", port_name);
rl.helper_mut().expect("No helper").colored_prompt = format!("{} \x1b[1;32m{}❯\x1b[0m", Utc::now(), port_name);
loop {
let now: DateTime<Utc> = Utc::now();
println!("{:?}", now);
let readline = rl.readline(format!("{}❯", port_name).as_str());
// let now: DateTime<Utc> = Utc::now();
// println!("{:?}", now);
let readline = rl.readline(format!("{} {}❯", Utc::now(), port_name).as_str());
match readline {
Ok(line) => {
let line = line + "\r";
Expand Down Expand Up @@ -265,5 +248,5 @@ fn main() {
}
}
}
rl.save_history("history.txt").unwrap();
rl.save_history("history.txt")
}

0 comments on commit 97cf68e

Please sign in to comment.