Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve panic #1

Merged
merged 3 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "icepick"
version = "0.0.1"
version = "0.0.2"
authors = ["Felipe Sere <felipesere@gmail.com>",
"Uku Taht <uku.taht@gmail.com>",
"Carol Nichols <cnichols@thinkthroughmath.com>"]
Expand All @@ -22,11 +22,9 @@ test = true
doc = false

[dependencies]
libc = "0.2.4"
getopts = "0.2.14"

[dependencies.ansi_term]
git = "https://github.com/ogham/rust-ansi-term.git"
libc = "0.2.139"
getopts = "0.2.21"
ansi_term = "0.12"

[profile.release]
opt-level = 3
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Then you can pipe input to it and fuzzy select on it:
find . -name "*.css" | icepick | xargs rm
```

The above commend would allow you to match on all CSS files in your current
The above commend would allow you to match on all CSS files in your current
directory and remove the selected one.

For more uses see [the original Ruby implementation](https://github.com/garybernhardt/selecta) by Gary Bernhardt.
Expand All @@ -45,4 +45,5 @@ If you have an idea to improve performance, run `cargo bench` and see how the re
@felipesere
@heruku
@carols10cents
@erwinvaneijk

2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ install: build
uninstall:
rm $(prefix)/bin/icepick

bench:
rustup run nightly cargo bench
6 changes: 3 additions & 3 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use tty::IO;
use crate::tty::IO;

pub struct Ansi<'a> {
pub io: Box<(IO + 'a)>,
pub io: Box<(dyn IO + 'a)>,
}

impl <'a> Ansi<'a> {
impl<'a> Ansi<'a> {
pub fn escape(&mut self, message: &str) {
let out = Ansi::esc(message);
self.io.write(out.as_ref());
Expand Down
2 changes: 1 addition & 1 deletion src/fake_tty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tty::IO;
use crate::tty::IO;

pub struct FakeIO {
last: String,
Expand Down
15 changes: 9 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ extern crate getopts;
extern crate icepick;

use getopts::Options;
use std::io::BufRead;
use std::io;
use std::io::BufRead;

use icepick::screen::Screen;

Expand All @@ -25,8 +25,10 @@ fn extract_initial_query() -> Option<String> {
opts.optopt("s", "search", "initial search query", "");

let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
Err(f) => { panic!(f.to_string()) }
Ok(m) => m,
Err(f) => {
panic!("{}", f)
}
};

matches.opt_str("s")
Expand All @@ -39,8 +41,9 @@ fn get_args() -> Vec<String> {
fn read_lines() -> Vec<String> {
let stdin = io::stdin();
let reader = stdin.lock();
let l = reader.lines().map( |line| {
line.unwrap().trim().to_string()
}).collect();
let l = reader
.lines()
.map(|line| line.unwrap().trim().to_string())
.collect();
l
}
4 changes: 2 additions & 2 deletions src/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use search::Search;
use text::Text;
use crate::search::Search;
use crate::text::Text;

pub struct Renderer;

Expand Down
74 changes: 42 additions & 32 deletions src/score.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::cmp::min;
use std::ascii::AsciiExt;
use std::ops::Range;

#[derive(Clone, Debug,PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Quality(pub f32);

impl Quality {
Expand All @@ -12,50 +11,59 @@ impl Quality {
}
}

#[derive(Clone, Debug,PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Match<'a> {
pub quality: Quality,
pub range: Range<usize>,
pub original: &'a String,

}

impl <'a> Match<'a> {
impl<'a> Match<'a> {
pub fn parts(&self) -> (String, String, String) {
let start = self.range.start;
let end = self.range.end;
let input = self.original;
(input[..start].to_string(),
input[start..end].to_string(),
input[end..].to_string())
let start = self.range.start;
let end = self.range.end;
let input = self.original;
(
input[..start].to_string(),
input[start..end].to_string(),
input[end..].to_string(),
)
}
}

impl <'a>Match<'a>{
impl<'a> Match<'a> {
pub fn new(quality: Quality, range: Range<usize>, original: &'a String) -> Match<'a> {
Match { quality: quality, range: range, original: original }
Match {
quality: quality,
range: range,
original: original,
}
}

pub fn with_empty_range(original: &'a String) -> Match<'a> {
Match::new(Quality(1.0), Range{start: 0,end: 0}, original)
Match::new(Quality(1.0), Range { start: 0, end: 0 }, original)
}
}

pub fn score<'a>(choice: &'a String, query: &String) -> Option<Match<'a>> {
let choice_length = choice.len() as f32;
let query_length = query.len() as f32;

if query_length == 0.0 { return Some(Match::with_empty_range(choice)) }
if query_length == 0.0 {
return Some(Match::with_empty_range(choice));
}
let lower_choice = choice.to_ascii_lowercase();


// TODO convert this over to compute_match_length(...).map(...)
match compute_match_length(&lower_choice, query) {
Some((start, match_length)) => {
let quality = Quality( (query_length / match_length as f32) / choice_length);
let substring = Range {start: start, end: start+match_length};
let quality = Quality((query_length / match_length as f32) / choice_length);
let substring = Range {
start: start,
end: start + match_length,
};
Some(Match::new(quality, substring, choice))
},
}
None => None,
}
}
Expand All @@ -67,7 +75,7 @@ fn slice_shift_char(line: &str) -> Option<(char, &str)> {
let mut chars = line.chars();
let ch = chars.next().unwrap();
let len = line.len();
let next_s = &line[ch.len_utf8().. len];
let next_s = &line[ch.len_utf8()..len];
Some((ch, next_s))
}
}
Expand All @@ -85,14 +93,18 @@ fn compute_match_length(choice: &String, query: &String) -> Option<(usize, usize
for_each_beginning(choice, first, |beginning| {
match match_length_from(choice, rest, beginning) {
Some(length) => {
shortest_match = min(length, shortest_match);
shortest_start = beginning;
},
None => {},
shortest_match = min(length, shortest_match);
shortest_start = beginning;
}
None => {}
};
});

if shortest_match == impossible_match {None} else {Some((shortest_start, shortest_match))}
if shortest_match == impossible_match {
None
} else {
Some((shortest_start, shortest_match))
}
}

fn for_each_beginning<F: FnMut(usize)>(choice: &String, beginning: char, mut f: F) {
Expand All @@ -107,16 +119,14 @@ fn match_length_from(choice: &String, query: &str, beginning: usize) -> Option<u
let mut match_index = beginning;

for query_char in query.chars() {
match find_first_after(choice, query_char, match_index + 1) {
Some(n) => match_index = n,
None => return None,
};
match find_first_after(choice, query_char, match_index + 1) {
Some(n) => match_index = n,
None => return None,
};
}
Some(match_index - beginning + 1)
}

fn find_first_after(choice: &String, query: char, offset: usize) -> Option<usize> {
choice[offset..]
.find(query)
.map(|index| index + offset)
choice[offset..].find(query).map(|index| index + offset)
}
14 changes: 7 additions & 7 deletions src/screen.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use search::Search;
use ansi::Ansi;
use tty::TTY;
use fake_tty::FakeIO;
use renderer::Renderer;
use text::Text;
use crate::search::Search;
use crate::ansi::Ansi;
use crate::tty::TTY;
use crate::fake_tty::FakeIO;
use crate::renderer::Renderer;
use crate::text::Text;
use std::cmp::min;
use text::Printable;
use crate::text::Printable;

pub struct Screen <'a> {
pub ansi: Ansi<'a>,
Expand Down
Loading