Skip to content

Commit a901678

Browse files
committed
Add operation for picking abs/relative path
1 parent c0e4582 commit a901678

File tree

7 files changed

+172
-124
lines changed

7 files changed

+172
-124
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fpick"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
license = "MIT"
55
edition = "2021"
66
description = "Interactive file picker"

README.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
![](./docs/img/screenshot1.png)
1414

15+
**fpick** returns the selected path to standard output, so you combine it with other commands:
16+
```sh
17+
cd "$(fpick)"
18+
cat $(fpick)
19+
```
20+
1521
## Installation
1622
### Cargo
1723
```sh
@@ -27,7 +33,7 @@ curl -L https://github.com/igrek51/fpick/releases/download/0.3.0/fpick -o ~/bin/
2733
chmod +x ~/bin/fpick
2834
```
2935

30-
## Usage
36+
## Usage & Controls
3137
Launch the interactive file picker by running `fpick`.
3238

3339
Navigate with keyboard:
@@ -36,11 +42,25 @@ Navigate with keyboard:
3642
- `` to enter a directory.
3743
- `` to go up,
3844
- Type a phrase to filter the list of files
39-
- `Enter` to select a file, exit and print its path to stdout.
40-
41-
See `fpick --help` for more options.
45+
- `Enter` on selected file to exit and print its path to stdout.
46+
- `Enter` on selected directory to enter inside it.
47+
- `Enter` on `.` to pick current directory.
48+
- `Esc` or `Ctrl + C` to exit.
49+
- `/` to go to root directory.
50+
- `Alt + Enter` on selected file / directory to open context menu and execute an operation:
51+
- **Open** - open directory in file manager or a file in a default application
52+
- **Show in less**
53+
- **Edit in vim**
54+
- **Edit in sudo vim**
55+
- **Delete file**
56+
- **Delete directory**
57+
- **Copy filename to clipboard**
58+
- **Pick absolute path** - return absolute path to stdout.
59+
- **Pick relative path** - return relative path to stdout.
4260

4361
## CLI arguments
62+
See `fpick --help` for options.
63+
4464
Usage:
4565
- `fpick [OPTIONS]` to select a file in a current directory and return its path
4666
- `fpick [OPTIONS] <PATH>` to select a file starting from a specified directory
@@ -53,12 +73,17 @@ Options:
5373
## Examples
5474
You can use it in combination with other commands, for example to print the selected file:
5575
```sh
56-
cat `fpick`
76+
cat $(fpick)
5777
```
5878

5979
Tired of typing `ls` and `cd`, over and over again,
6080
just to find a file in a deeply nested directory tree?
6181
Use `fpick` to navigate through directories interactively:
6282
```sh
63-
cd `fpick`
83+
cd "$(fpick)"
84+
```
85+
86+
Set alias for quick access:
87+
```sh
88+
alias cfp='cd "$(fpick)"'
6489
```

src/action_menu.rs

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,73 @@ use crate::logs::log;
66
#[derive(Debug, Clone)]
77
pub struct MenuAction {
88
pub name: &'static str,
9-
pub command: &'static str,
9+
pub operation: Operation,
10+
}
11+
12+
#[derive(Debug, Clone)]
13+
pub enum Operation {
14+
ShellCommand { template: &'static str },
15+
PickAbsolutePath,
16+
PickRelativePath,
1017
}
1118

1219
pub fn generate_known_actions() -> Vec<MenuAction> {
1320
vec![
21+
MenuAction {
22+
name: "Open",
23+
operation: Operation::ShellCommand {
24+
template: "xdg-open \"{}\"",
25+
},
26+
},
27+
MenuAction {
28+
name: "Show in less",
29+
operation: Operation::ShellCommand {
30+
template: "gnome-terminal -- less \"{}\"",
31+
},
32+
},
1433
MenuAction {
1534
name: "Edit in vim",
16-
command: "gnome-terminal -- vim \"{}\"",
35+
operation: Operation::ShellCommand {
36+
template: "gnome-terminal -- vim \"{}\"",
37+
},
1738
},
1839
MenuAction {
19-
name: "Open in less",
20-
command: "gnome-terminal -- less \"{}\"",
40+
name: "Edit in sudo vim",
41+
operation: Operation::ShellCommand {
42+
template: "gnome-terminal -- sudo vim \"{}\"",
43+
},
2144
},
2245
MenuAction {
2346
name: "Delete file",
24-
command: "rm \"{}\"",
47+
operation: Operation::ShellCommand {
48+
template: "rm \"{}\"",
49+
},
2550
},
2651
MenuAction {
2752
name: "Delete directory",
28-
command: "rm -rf \"{}\"",
53+
operation: Operation::ShellCommand {
54+
template: "rm -rf \"{}\"",
55+
},
2956
},
3057
MenuAction {
3158
name: "Copy filename to clipboard",
32-
command: "echo -n \"{}\" | xclip -selection clipboard",
59+
operation: Operation::ShellCommand {
60+
template: "echo -n \"{}\" | xclip -selection clipboard",
61+
},
62+
},
63+
MenuAction {
64+
name: "Pick absolute path",
65+
operation: Operation::PickAbsolutePath,
66+
},
67+
MenuAction {
68+
name: "Pick relative path",
69+
operation: Operation::PickRelativePath,
3370
},
3471
]
3572
}
3673

37-
pub fn run_menu_action(path: &String, menu_action: &MenuAction) -> Result<()> {
38-
let command = menu_action.command;
39-
let cmd = String::from(command).replace("{}", path);
74+
pub fn execute_shell_operation(path: &String, command_template: &str) -> Result<()> {
75+
let cmd = String::from(command_template).replace("{}", path);
4076
log(format!("Executing command: {:?}", cmd).as_str());
4177
let mut c = Command::new("sh")
4278
.arg("-c")

src/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ pub struct App {
1818
pub relative_path: bool,
1919
pub absolute_path: bool,
2020
pub print_stderr: bool,
21-
pub parent_nodes: Vec<FileNode>, // nodes leading to the current directory
21+
pub parent_file_nodes: Vec<FileNode>, // nodes leading to the current directory
2222
pub starting_dir_nodes: Vec<FileNode>, // nodes leading to the starting directory
23-
pub child_nodes: Vec<FileNode>, // nodes in the current directory
24-
pub child_tree_nodes: Vec<TreeNode>, // nodes of filesystem tree to display
23+
pub child_file_nodes: Vec<FileNode>, // nodes in the current directory
24+
pub child_tree_nodes: Vec<TreeNode>, // nodes of filesystem tree to display
2525
pub dir_cursor: usize,
2626
pub filter_text: String,
2727
pub file_tree_state: ListState,

0 commit comments

Comments
 (0)