Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
micouy committed May 10, 2021
1 parent a09d58a commit 39254f7
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 232 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 1 addition & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kn"
version = "0.1.0"
version = "0.2.0"
edition = "2018"
autobins = false
include = [
Expand All @@ -26,14 +26,6 @@ keywords = ["cli", "utility", "filesystem"]
name = "_kn"
path = "src/main.rs"

[[bin]]
name = "kn-ui"
path = "src/interactive/ui.rs"

[[bin]]
name = "kn-interactive"
path = "src/interactive/mod.rs"


[dependencies]
regex = "1.4"
Expand Down
9 changes: 7 additions & 2 deletions init/kn.bash.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

function kn() {
if [[ "$#" -eq 0 ]]; then
# no args provided, go to home dir
# no args provided, search in interactive mode

\builtin cd ~
\builtin local __result_file
__result_file="`mktemp`"

\builtin local __path

_kn interactive "$__result_file" && \builtin cd "`cat $__result_file`"
elif [[ "$#" -eq 1 ]] && [[ "$1" = '-' ]]; then
# only dash provided, go to previous location if it exists

Expand Down
9 changes: 7 additions & 2 deletions init/kn.zsh.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

function kn() {
if [[ "$#" -eq 0 ]]; then
# no args provided, go to home dir
# no args provided, search in interactive mode

\builtin cd ~
\builtin local __result_file
__result_file="`mktemp`"

\builtin local __path

_kn interactive "$__result_file" && \builtin cd "`cat $__result_file`"
elif [[ "$#" -eq 1 ]] && [[ "$1" = '-' ]]; then
# only dash provided, go to previous location

Expand Down
5 changes: 2 additions & 3 deletions src/init/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[allow(dead_code)]
use crate::{Error, Result};

use clap::ArgMatches;

use crate::{Error, Result};

pub fn init(matches: &ArgMatches<'_>) -> Result<String> {
// Fail silently?
let shell = matches
Expand Down
45 changes: 17 additions & 28 deletions src/interactive/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(warnings)]
#![feature(destructuring_assignment)]

use clap::ArgMatches;
use std::{
env,
ffi::OsString,
Expand All @@ -12,14 +11,16 @@ use std::{
path::{Path, PathBuf},
process::exit,
};

use alphanumeric_sort;
use clap::ArgMatches;
use termion::{
clear,
cursor::{self, DetectCursorPos, Goto},
event::{Event, Key},
input::{TermRead, TermReadEventsAndRaw},
raw::IntoRawMode,
};
use alphanumeric_sort;

use crate::{
error::{Error, Result},
Expand All @@ -32,27 +33,6 @@ use crate::{
},
utils::{self, as_path},
};
/*
*/

/*
#[path = "../search/mod.rs"]
pub mod search;
use search::{
abbr::{Abbr, Congruence},
fs::{DefaultFileSystem, FileSystem},
search_level,
Finding,
};
#[path = "../utils.rs"]
#[macro_use] pub mod utils;
use utils::as_path;
#[path = "../error.rs"]
pub mod error;
use error::{Error, Result};
*/

mod ui;

Expand Down Expand Up @@ -92,7 +72,10 @@ impl Location {
}

fn prepare_children<P, F>(path: P, file_system: &F) -> Result<Vec<PathBuf>>
where P: AsRef<Path>, F: FileSystem {
where
P: AsRef<Path>,
F: FileSystem,
{
let mut children = file_system.read_dir(path)?;
alphanumeric_sort::sort_path_slice(&mut children);

Expand Down Expand Up @@ -145,7 +128,8 @@ impl Location {
.chain(self.suffix.iter().map(as_path))
.chain(iter::once(as_path(&component)))
.collect::<PathBuf>();
self.children = Self::prepare_children(new_location, file_system)?;
self.children =
Self::prepare_children(new_location, file_system)?;
self.suffix.push(component.as_os_str().to_os_string());

Ok(())
Expand Down Expand Up @@ -252,7 +236,7 @@ impl State {
where
F: FileSystem,
{
if c == '/' {
if c == '/' || c == '\\' {
self.confirm_selection(file_system)?;
} else {
self.consume_char(c);
Expand Down Expand Up @@ -394,6 +378,11 @@ pub fn interactive() -> Result<PathBuf> {
let mut state = State::new(root, &file_system)?;
let ui_state = state.get_ui_state();
let (mut ui, selection) = UI::new(&mut stdout, ui_state)?;

if let Some(suggestion_ix) = selection {
state.select_suggestion(suggestion_ix);
}

ui.clear()?;
ui.display()?;

Expand Down Expand Up @@ -431,8 +420,8 @@ pub fn interactive() -> Result<PathBuf> {
return Err(Error::CtrlC);
}

// Any other char.
Key::Char(c) => {
// Any other char, excluding whitespace.
Key::Char(c) if !c.is_whitespace() => {
let ui_state = state.handle_input(c, &file_system)?;
let stdout = ui.take();
let ui_and_selection = UI::new(stdout, ui_state)?;
Expand Down
77 changes: 0 additions & 77 deletions src/interactive/search.rs

This file was deleted.

17 changes: 2 additions & 15 deletions src/interactive/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,13 @@ use termion::{
cursor::{self, DetectCursorPos},
raw::RawTerminal,
};

use unicode_width::*;

use crate::{
error::{Error, Result},
utils::as_path,
};

/*
#[path = "../error.rs"]
mod error;
use error::{Error, Result};
#[path = "../utils.rs"]
#[macro_use] mod _utils;
use _utils::as_path;
*/

const SUGGESTIONS_SEPARATOR: &'static str = " ";

// Palette:
Expand All @@ -47,8 +36,8 @@ in the proper position to start printing.

#[derive(Debug)]
pub struct UIState {
pub suggestions: Vec<String>, /* TODO: Possible problems with converting
* OsString to String? */
// TODO: Possible problems with converting OsString to String?
pub suggestions: Vec<String>,
pub input: Option<String>,
pub location: PathBuf,
}
Expand Down Expand Up @@ -502,8 +491,6 @@ mod utils {
}
}

fn main() {}

#[cfg(test)]
mod test {
use super::*;
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(unused_parens)]
#![allow(warnings)] // Temporary.
#![feature(destructuring_assignment)]

use std::{fs, process::exit};
Expand Down
70 changes: 0 additions & 70 deletions src/query/entry.rs

This file was deleted.

Loading

0 comments on commit 39254f7

Please sign in to comment.