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

Initial Windows root navigation #98

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ rexpect = "0.5"
[profile.release]
lto = true
strip = "debuginfo"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["fileapi"] }
16 changes: 13 additions & 3 deletions src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub type MatchesLocType = Vec<(usize, usize)>;

/// A vector that keeps track of items that are 'filtered'. It offers indexing/viewing
/// both the vector of filtered items and the whole unfiltered vector.
struct MatchesVec {
pub struct MatchesVec {
all_items: Vec<CustomDirEntry>,
// Each key-value pair in this map corresponds to an item in `all_items` that matches the
// current search. The key is the item's index in `all_items`, while the value contains the
Expand Down Expand Up @@ -112,6 +112,16 @@ pub struct CustomDirEntry {
}

impl CustomDirEntry {
pub fn custom(path_buf: PathBuf) -> CustomDirEntry {
Self{
_path: path_buf.clone(),
// TODO: Don't let this hack survive! But we need it to properly respond for `is_dir`
metadata: Some(std::fs::metadata("/").unwrap()),
symlink_target: None,
_file_name: std::ffi::OsString::from(path_buf.clone()),
}
}

/// Return the file name of this directory entry. The file name is an OsString,
/// which may not be possible to convert to a String. In this case, this
/// function returns an empty string.
Expand Down Expand Up @@ -169,7 +179,7 @@ impl From<&Path> for CustomDirEntry {
}

/// The type of the `ls_output_buf` buffer of the app state
type LsBufType = MatchesVec;
pub type LsBufType = MatchesVec;

/// Possible non-error results of 'change directory' operation
#[derive(Debug)]
Expand All @@ -196,7 +206,7 @@ pub struct TereAppState {

// This vector will hold the list of files/folders in the current directory,
// including ".." (the parent folder).
ls_output_buf: LsBufType,
pub ls_output_buf: LsBufType,

// Have to manually keep track of the logical absolute path of our app, see https://stackoverflow.com/a/70309860/5208725
pub current_path: PathBuf,
Expand Down
55 changes: 54 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ mod action;
pub mod help_window;
pub mod markup_render;

#[cfg(windows)]
extern crate winapi;

use std::convert::TryFrom;
use std::error::Error;
use std::ffi::CString;
use std::fmt::Write as _;
use std::io::{Stderr, Write};
use std::path::PathBuf;

use crate::app_state::{CdResult, TereAppState};
use crate::app_state::{CdResult, CustomDirEntry, TereAppState};
use crate::error::TereError;
use crate::settings::{CaseSensitiveMode, GapSearchMode, SortMode};
pub use action::{Action, ActionContext};
Expand Down Expand Up @@ -606,6 +611,54 @@ impl<'a> TereTui<'a> {
Ok(())
}

#[cfg(windows)]
fn get_roots() -> CTResult<Vec<String>> {
use winapi::um::fileapi::{
GetDriveTypeA,
GetLogicalDrives,
};

let mut results: Vec<String> = Vec::new();

unsafe {
let mut drives = GetLogicalDrives();
let mut drive_letter = 'A';

while drives != 0 {
if drives & 1 == 1 {
let str = drive_letter.to_string() + ":\\";
let cstr = CString::new(str.clone())?;
let x = GetDriveTypeA(cstr.as_ptr());
if x == 3 || x == 2 {
results.push(str);
}
}
drive_letter = std::char::from_u32((drive_letter as u32) + 1).unwrap();
drives >>= 1;
}
}

Ok(results)
}

#[cfg(windows)]
fn on_go_to_root(&mut self) -> CTResult<()> {
let drive_letters: Vec<CustomDirEntry> = Self::get_roots()?
.iter()
.map(|r| PathBuf::from(r))
.map(|p| CustomDirEntry::custom(p))
.collect();

self.info_message("Select drive to browse")?;
self.app_state.ls_output_buf = drive_letters.into();

self.redraw_header()?;
self.redraw_main_window()?;
self.redraw_footer()?;
Ok(())
}

#[cfg(not(windows))]
fn on_go_to_root(&mut self) -> CTResult<()> {
// note: this is the same as std::path::Component::RootDir
// using a temporary buffer to avoid allocating on the heap, because MAIN_SEPARATOR_STR is
Expand Down