summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-01-31 16:02:22 +0100
committerqkzk <qu3nt1n@gmail.com>2023-01-31 16:02:22 +0100
commit8a79377013e60930eb0af68a16e96c19a6abec1c (patch)
treeb41904a83ce91bc049d05cfc074cbe20e13dd0d8
parent16016112acbc7861af1cc2d050f47e46f19749e0 (diff)
remove all Rc to users cache. Use simple reference only
-rw-r--r--src/args.rs2
-rw-r--r--src/fileinfo.rs27
-rw-r--r--src/main.rs3
-rw-r--r--src/preview.rs7
-rw-r--r--src/status.rs18
-rw-r--r--src/tab.rs6
-rw-r--r--src/tree.rs9
7 files changed, 34 insertions, 38 deletions
diff --git a/src/args.rs b/src/args.rs
index b25fffc..4034d44 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -1,6 +1,6 @@
use clap::Parser;
-#[derive(Parser, Debug)]
+#[derive(Parser, Debug, Clone)]
#[clap(author, version, about)]
/// FM : dired like file manager{n}
pub struct Args {
diff --git a/src/fileinfo.rs b/src/fileinfo.rs
index ddeaeea..d4b9bed 100644
--- a/src/fileinfo.rs
+++ b/src/fileinfo.rs
@@ -2,7 +2,6 @@ use std::fs::{metadata, read_dir, DirEntry, Metadata};
use std::iter::Enumerate;
use std::os::unix::fs::{FileTypeExt, MetadataExt};
use std::path;
-use std::rc::Rc;
use chrono::offset::Local;
use chrono::DateTime;
@@ -126,7 +125,7 @@ pub struct FileInfo {
impl FileInfo {
/// Reads every information about a file from its metadata and returs
/// a new `FileInfo` object if we can create one.
- pub fn new(direntry: &DirEntry, users_cache: &Rc<UsersCache>) -> FmResult<FileInfo> {
+ pub fn new(direntry: &DirEntry, users_cache: &UsersCache) -> FmResult<FileInfo> {
let metadata = direntry.metadata()?;
let path = direntry.path();
let filename = extract_filename(direntry)?;
@@ -139,7 +138,7 @@ impl FileInfo {
pub fn from_path_with_name(
path: &path::Path,
filename: &str,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
) -> FmResult<Self> {
let metadata = metadata(path)?;
@@ -150,7 +149,7 @@ impl FileInfo {
path: &path::Path,
metadata: &Metadata,
filename: String,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
) -> FmResult<Self> {
let path = path.to_owned();
let size = extract_file_size(metadata);
@@ -234,7 +233,6 @@ impl FileInfo {
/// Holds the information about file in the current directory.
/// We know about the current path, the files themselves, the selected index,
/// the "display all files including hidden" flag and the key to sort files.
-#[derive(Clone)]
pub struct PathContent {
/// The current path
pub path: path::PathBuf,
@@ -245,7 +243,7 @@ pub struct PathContent {
/// The kind of sort used to display the files.
sort_kind: SortKind,
used_space: u64,
- pub users_cache: Rc<UsersCache>,
+ pub users_cache: UsersCache,
}
impl PathContent {
@@ -254,7 +252,7 @@ impl PathContent {
/// Selects the first file if any.
pub fn new(
path: &path::Path,
- users_cache: Rc<UsersCache>,
+ users_cache: UsersCache,
filter: &FilterKind,
show_hidden: bool,
) -> FmResult<Self> {
@@ -299,7 +297,7 @@ impl PathContent {
path: &path::Path,
show_hidden: bool,
filter_kind: &FilterKind,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
) -> FmResult<Vec<FileInfo>> {
let mut files: Vec<FileInfo> = Self::create_dot_dotdot(path, users_cache)?;
@@ -311,10 +309,7 @@ impl PathContent {
Ok(files)
}
- fn create_dot_dotdot(
- path: &path::Path,
- users_cache: &Rc<UsersCache>,
- ) -> FmResult<Vec<FileInfo>> {
+ fn create_dot_dotdot(path: &path::Path, users_cache: &UsersCache) -> FmResult<Vec<FileInfo>> {
let current = FileInfo::from_path_with_name(path, ".", users_cache)?;
match path.parent() {
Some(parent) => {
@@ -474,7 +469,7 @@ impl PathContent {
/// Refresh the existing users.
pub fn refresh_users(
&mut self,
- users_cache: Rc<UsersCache>,
+ users_cache: UsersCache,
filter: &FilterKind,
show_hidden: bool,
) -> FmResult<()> {
@@ -554,7 +549,7 @@ fn convert_octal_mode(mode: usize) -> &'static str {
/// Reads the owner name and returns it as a string.
/// If it's not possible to get the owner name (happens if the owner exists on a remote machine but not on host),
/// it returns the uid as a `Result<String>`.
-fn extract_owner(metadata: &Metadata, users_cache: &Rc<UsersCache>) -> FmResult<String> {
+fn extract_owner(metadata: &Metadata, users_cache: &UsersCache) -> FmResult<String> {
match users_cache.get_user_by_uid(metadata.uid()) {
Some(uid) => Ok(uid
.name()
@@ -568,7 +563,7 @@ fn extract_owner(metadata: &Metadata, users_cache: &Rc<UsersCache>) -> FmResult<
/// Reads the group name and returns it as a string.
/// If it's not possible to get the group name (happens if the group exists on a remote machine but not on host),
/// it returns the gid as a `Result<String>`.
-fn extract_group(metadata: &Metadata, users_cache: &Rc<UsersCache>) -> FmResult<String> {
+fn extract_group(metadata: &Metadata, users_cache: &UsersCache) -> FmResult<String> {
match users_cache.get_group_by_gid(metadata.gid()) {
Some(gid) => Ok(gid
.name()
@@ -619,7 +614,7 @@ fn filekind_and_filename(filename: &str, file_kind: &FileKind) -> String {
/// Returns None if there's no file.
pub fn files_collection(
fileinfo: &FileInfo,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
show_hidden: bool,
filter_kind: &FilterKind,
) -> Option<Vec<FileInfo>> {
diff --git a/src/main.rs b/src/main.rs
index 25ffdc4..5807e28 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -36,7 +36,8 @@ fn main() -> FmResult<()> {
help,
&config.terminal,
)?;
- let colors = config.colors;
+ let colors = config.colors.clone();
+ drop(config);
while let Ok(event) = event_reader.poll_event() {
event_dispatcher.dispatch(&mut status, event, &colors)?;
diff --git a/src/preview.rs b/src/preview.rs
index b2314f4..f469361 100644
--- a/src/preview.rs
+++ b/src/preview.rs
@@ -4,7 +4,6 @@ use std::io::{BufRead, BufReader, Read};
use std::iter::{Enumerate, Skip, Take};
use std::panic;
use std::path::{Path, PathBuf};
-use std::rc::Rc;
use std::slice::Iter;
use content_inspector::{inspect, ContentType};
@@ -58,7 +57,7 @@ impl Preview {
/// it to the display method.
pub fn new(
file_info: &FileInfo,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
status: &Status,
colors: &Colors,
) -> FmResult<Self> {
@@ -534,7 +533,7 @@ impl Directory {
/// We only hold the result here, since the tree itself has now usage atm.
pub fn new(
path: &Path,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
colors: &Colors,
filter_kind: &FilterKind,
show_hidden: bool,
@@ -558,7 +557,7 @@ impl Directory {
}
/// Creates an empty directory preview.
- pub fn empty(path: &Path, users_cache: &Rc<UsersCache>) -> FmResult<Self> {
+ pub fn empty(path: &Path, users_cache: &UsersCache) -> FmResult<Self> {
Ok(Self {
tree: Tree::empty(path, users_cache)?,
len: 0,
diff --git a/src/status.rs b/src/status.rs
index 35a8dc5..d3ced23 100644
--- a/src/status.rs
+++ b/src/status.rs
@@ -1,7 +1,6 @@
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
-use std::rc::Rc;
use std::sync::Arc;
use regex::Regex;
@@ -78,15 +77,20 @@ impl Status {
// unsafe because of UsersCache::with_all_users
let sys = System::new_all();
let opener = load_opener(OPENER_PATH, terminal).unwrap_or_else(|_| Opener::new(terminal));
- let users_cache = unsafe { Rc::new(UsersCache::with_all_users()) };
- let mut tab = Tab::new(args, height, users_cache)?;
+ let users_cache = unsafe { UsersCache::with_all_users() };
+ let mut tab = Tab::new(args.clone(), height, users_cache)?;
tab.shortcut
.extend_with_mount_points(&Self::disks_mounts(sys.disks()));
- let trash = Trash::new()?;
let encrypted_devices = DeviceOpener::default();
+ let users_cache2 = unsafe { UsersCache::with_all_users() };
+ let mut tab2 = Tab::new(args, height, users_cache2)?;
+ tab2.shortcut
+ .extend_with_mount_points(&Self::disks_mounts(sys.disks()));
+ let trash = Trash::new()?;
+
Ok(Self {
- tabs: [tab.clone(), tab],
+ tabs: [tab2, tab],
index: 0,
flagged: Flagged::default(),
marks: Marks::read_from_config_file(),
@@ -296,9 +300,9 @@ impl Status {
/// Refresh the existing users.
pub fn refresh_users(&mut self) -> FmResult<()> {
- let users_cache = Rc::new(unsafe { UsersCache::with_all_users() });
for tab in self.tabs.iter_mut() {
- tab.refresh_users(users_cache.clone())?;
+ let users_cache = unsafe { UsersCache::with_all_users() };
+ tab.refresh_users(users_cache)?;
}
Ok(())
}
diff --git a/src/tab.rs b/src/tab.rs
index dd8ffe5..7148bbc 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -1,5 +1,4 @@
use std::path;
-use std::rc::Rc;
use users::UsersCache;
@@ -19,7 +18,6 @@ use crate::visited::History;
/// Holds every thing about the current tab of the application.
/// Most of the mutation is done externally.
-#[derive(Clone)]
pub struct Tab {
/// The mode the application is currenty in
pub mode: Mode,
@@ -59,7 +57,7 @@ pub struct Tab {
impl Tab {
/// Creates a new tab from args and height.
- pub fn new(args: Args, height: usize, users_cache: Rc<UsersCache>) -> FmResult<Self> {
+ pub fn new(args: Args, height: usize, users_cache: UsersCache) -> FmResult<Self> {
let path = std::fs::canonicalize(path::Path::new(&args.path))?;
let directory = Directory::empty(&path, &users_cache)?;
let filter = FilterKind::All;
@@ -204,7 +202,7 @@ impl Tab {
}
/// Refresh the existing users.
- pub fn refresh_users(&mut self, users_cache: Rc<UsersCache>) -> FmResult<()> {
+ pub fn refresh_users(&mut self, users_cache: UsersCache) -> FmResult<()> {
self.path_content
.refresh_users(users_cache, &self.filter, self.show_hidden)
}
diff --git a/src/tree.rs b/src/tree.rs
index fc79ad9..2461fb5 100644
--- a/src/tree.rs
+++ b/src/tree.rs
@@ -1,5 +1,4 @@
use std::path::Path;
-use std::rc::Rc;
use tuikit::attr::Attr;
use users::UsersCache;
@@ -130,7 +129,7 @@ impl Tree {
pub fn from_path(
path: &Path,
max_depth: usize,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
filter_kind: &FilterKind,
show_hidden: bool,
parent_position: Vec<usize>,
@@ -160,7 +159,7 @@ impl Tree {
fn create_tree_from_fileinfo(
fileinfo: FileInfo,
max_depth: usize,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
filter_kind: &FilterKind,
display_hidden: bool,
parent_position: Vec<usize>,
@@ -190,7 +189,7 @@ impl Tree {
fn make_leaves(
fileinfo: &FileInfo,
max_depth: usize,
- users_cache: &Rc<UsersCache>,
+ users_cache: &UsersCache,
display_hidden: bool,
filter_kind: &FilterKind,
sort_kind: &SortKind,
@@ -236,7 +235,7 @@ impl Tree {
/// Creates an empty tree. Used when the user changes the CWD and hasn't displayed
/// a tree yet.
- pub fn empty(path: &Path, users_cache: &Rc<UsersCache>) -> FmResult<Self> {
+ pub fn empty(path: &Path, users_cache: &UsersCache) -> FmResult<Self> {
let filename = filename_from_path(path)?;
let fileinfo = FileInfo::from_path_with_name(path, filename, users_cache)?;
let node = Node {