fix(main): prevent overrunning string index on column edits

This commit is contained in:
xeruf 2024-07-29 09:59:17 +03:00
parent b71916c905
commit 8e7b8d3e66
1 changed files with 36 additions and 29 deletions

View File

@ -3,7 +3,6 @@ use std::fmt::Display;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, stdin, stdout, Write}; use std::io::{BufRead, BufReader, stdin, stdout, Write};
use std::ops::Deref;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::sync::mpsc; use std::sync::mpsc;
@ -88,8 +87,7 @@ async fn main() {
Ok(relay) => { Ok(relay) => {
or_print(client.add_relay(relay).await); or_print(client.add_relay(relay).await);
} }
_ => { _ => match File::open(&relayfile).map(|f| BufReader::new(f).lines().flatten()) {
match File::open(&relayfile).map(|f| BufReader::new(f).lines().flatten()) {
Ok(lines) => { Ok(lines) => {
for line in lines { for line in lines {
or_print(client.add_relay(line).await); or_print(client.add_relay(line).await);
@ -98,20 +96,19 @@ async fn main() {
Err(e) => { Err(e) => {
eprintln!("Could not read relays file: {}", e); eprintln!("Could not read relays file: {}", e);
if let Some(line) = prompt("Relay?") { if let Some(line) = prompt("Relay?") {
let url = if line.contains("://") { line } else { "wss://".to_string() + &line }; let url = if line.contains("://") {
or_print( line
client } else {
.add_relay(url.clone()) "wss://".to_string() + &line
.await, };
).map(|bool| { or_print(client.add_relay(url.clone()).await).map(|bool| {
if bool { if bool {
or_print(fs::write(&relayfile, url)); or_print(fs::write(&relayfile, url));
} }
}); });
}; };
} }
} },
}
} }
//let proxy = Some(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050))); //let proxy = Some(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050)));
@ -188,7 +185,11 @@ async fn main() {
loop { loop {
tasks.print_tasks(); tasks.print_tasks();
print!(" {}{}) ", tasks.get_task_path(tasks.get_position()), tasks.get_prompt_suffix()); print!(
" {}{}) ",
tasks.get_task_path(tasks.get_position()),
tasks.get_prompt_suffix()
);
stdout().flush().unwrap(); stdout().flush().unwrap();
match lines.next() { match lines.next() {
Some(Ok(input)) => { Some(Ok(input)) => {
@ -206,13 +207,19 @@ async fn main() {
let mut iter = input.chars(); let mut iter = input.chars();
let op = iter.next(); let op = iter.next();
let arg = input[1..].trim(); let arg = if input.len() > 1 {
input[1..].trim()
} else {
""
};
match op { match op {
None => {} None => {}
Some(':') => match input[1..2].parse::<usize>() { Some(':') => match iter.next().and_then(|s| s.to_digit(10)) {
Ok(index) => { Some(digit) => {
if input.len() == 2 { let index = digit as usize;
let remaining = iter.collect::<String>().trim().to_string();
if remaining.is_empty() {
tasks.properties.remove(index); tasks.properties.remove(index);
continue; continue;
} }
@ -223,7 +230,7 @@ async fn main() {
tasks.properties.insert(index, value); tasks.properties.insert(index, value);
} }
} }
Err(_) => { None => {
let pos = tasks.properties.iter().position(|s| s == arg); let pos = tasks.properties.iter().position(|s| s == arg);
match pos { match pos {
None => { None => {