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

Implement opening with encoding #51

Merged
merged 6 commits into from
Mar 2, 2025
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
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ chrono = "0.4.40"
clap = { version = "4.5.31", features = ["derive"] }
console = "0.15.10"
dirs = "6.0.0"
encoding_rs = "0.8.35"
humansize = "2.1.3"
image = "0.25.5"
infer = "0.19.0"
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ highlight_theme = "base16-ocean.dark"
# Whether image file preview is enabled in the object preview.
# type: bool
image = false
# Array of labels for the encoding want to use.
# Label names should be specified from https://encoding.spec.whatwg.org/#names-and-labels.
# type: array of strings
encodings = [
"utf-8",
"utf-16be",
"utf-16le",
]
```

### Syntax highlighting
Expand Down Expand Up @@ -223,8 +231,10 @@ https://www.sublimetext.com/docs/syntax.html
- It must be enabled in the [config](#config-file-format)
- image preview (by [ratatui-image](/~https://github.com/benjajaja/ratatui-image))
- It must be enabled in the [config](#config-file-format)
- open with encoding
- Available encodings can be specified in the [config](#config-file-format).

<img src="./img/object-preview.png" width=400> <img src="./img/object-preview-image.png" width=400>
<img src="./img/object-preview.png" width=400> <img src="./img/object-preview-image.png" width=400> <img src="./img/object-preview-encoding.png" width=400>

## Troubleshooting

Expand Down
Binary file added img/object-preview-encoding.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub struct PreviewConfig {
pub highlight_theme: String,
#[default = false]
pub image: bool,
#[default(vec![
"utf-8".into(),
"utf-16be".into(),
"utf-16le".into(),
])]
pub encodings: Vec<String>,
}

fn default_download_dir() -> String {
Expand Down
75 changes: 73 additions & 2 deletions src/pages/object_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::{
object::{FileDetail, ObjectKey, RawObject},
pages::util::{build_helps, build_short_helps},
widget::{
self, ImagePreview, ImagePreviewState, InputDialog, InputDialogState, TextPreview,
TextPreviewState,
self, EncodingDialog, EncodingDialogState, ImagePreview, ImagePreviewState, InputDialog,
InputDialogState, TextPreview, TextPreviewState,
},
};

Expand All @@ -30,6 +30,7 @@ pub struct ObjectPreviewPage {
object_key: ObjectKey,

view_state: ViewState,
encoding_dialog_state: EncodingDialogState,

ctx: Rc<AppContext>,
tx: Sender,
Expand All @@ -46,6 +47,7 @@ enum ViewState {
#[default]
Default,
SaveDialog(InputDialogState),
EncodingDialog,
}

impl ObjectPreviewPage {
Expand All @@ -58,6 +60,8 @@ impl ObjectPreviewPage {
ctx: Rc<AppContext>,
tx: Sender,
) -> Self {
let encoding_dialog_state = EncodingDialogState::new(&ctx.config.preview.encodings);

let preview_type = if infer::is_image(&object.bytes) {
let (state, msg) =
ImagePreviewState::new(&object.bytes, ctx.env.image_picker.clone().into());
Expand All @@ -71,6 +75,7 @@ impl ObjectPreviewPage {
&object,
ctx.config.preview.highlight,
&ctx.config.preview.highlight_theme,
encoding_dialog_state.selected(),
);
if let Some(msg) = msg {
tx.send(AppEventType::NotifyWarn(msg));
Expand All @@ -86,6 +91,7 @@ impl ObjectPreviewPage {
path,
object_key,
view_state: ViewState::Default,
encoding_dialog_state,
ctx,
tx,
}
Expand Down Expand Up @@ -136,6 +142,9 @@ impl ObjectPreviewPage {
key_code_char!('S') => {
self.open_save_dialog();
}
key_code_char!('e') => {
self.open_encoding_dialog();
}
key_code_char!('?') => {
self.tx.send(AppEventType::OpenHelp);
}
Expand Down Expand Up @@ -177,6 +186,24 @@ impl ObjectPreviewPage {
state.handle_key_event(key);
}
},
(ViewState::EncodingDialog, _) => match key {
key_code!(KeyCode::Esc) => {
self.close_encoding_dialog();
}
key_code_char!('j') => {
self.encoding_dialog_state.select_next();
}
key_code_char!('k') => {
self.encoding_dialog_state.select_prev();
}
key_code!(KeyCode::Enter) => {
self.apply_encoding();
}
key_code_char!('?') => {
self.tx.send(AppEventType::OpenHelp);
}
_ => {}
},
}
}

Expand Down Expand Up @@ -209,6 +236,12 @@ impl ObjectPreviewPage {
let (cursor_x, cursor_y) = state.cursor();
f.set_cursor_position((cursor_x, cursor_y));
}

if let ViewState::EncodingDialog = &mut self.view_state {
let encoding_dialog =
EncodingDialog::new(&self.encoding_dialog_state).theme(&self.ctx.theme);
f.render_widget(encoding_dialog, area);
}
}

pub fn helps(&self) -> Vec<String> {
Expand Down Expand Up @@ -236,6 +269,12 @@ impl ObjectPreviewPage {
(&["Esc"], "Close save dialog"),
(&["Enter"], "Download object"),
],
(ViewState::EncodingDialog, _) => &[
(&["Ctrl-c"], "Quit app"),
(&["Esc"], "Close encoding dialog"),
(&["j/k"], "Select item"),
(&["Enter"], "Reopen with encoding"),
],
};

build_helps(helps)
Expand All @@ -262,6 +301,12 @@ impl ObjectPreviewPage {
(&["Enter"], "Download", 1),
(&["?"], "Help", 0),
],
(ViewState::EncodingDialog, _) => &[
(&["Esc"], "Close", 2),
(&["j/k"], "Select", 3),
(&["Enter"], "Encode", 1),
(&["?"], "Help", 0),
],
};

build_short_helps(helps)
Expand All @@ -277,6 +322,32 @@ impl ObjectPreviewPage {
self.view_state = ViewState::Default;
}

fn open_encoding_dialog(&mut self) {
if let PreviewType::Text(_) = &mut self.preview_type {
self.view_state = ViewState::EncodingDialog;
}
}

fn close_encoding_dialog(&mut self) {
self.view_state = ViewState::Default;
self.encoding_dialog_state.reset();
}

fn apply_encoding(&mut self) {
if let ViewState::EncodingDialog = &self.view_state {
if let PreviewType::Text(state) = &mut self.preview_type {
state.set_encoding(self.encoding_dialog_state.selected());
state.update_lines(
&self.file_detail,
&self.object,
self.ctx.config.preview.highlight,
&self.ctx.config.preview.highlight_theme,
);
}
}
self.close_encoding_dialog();
}

pub fn enable_image_render(&mut self) {
if let PreviewType::Image(state) = &mut self.preview_type {
state.set_render(true);
Expand Down
2 changes: 1 addition & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ pub use sort_list_dialog::{
ObjectListSortDialogState, ObjectListSortType,
};
pub use status::{Status, StatusType};
pub use text_preview::{TextPreview, TextPreviewState};
pub use text_preview::{EncodingDialog, EncodingDialogState, TextPreview, TextPreviewState};
6 changes: 5 additions & 1 deletion src/widget/scroll_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum ScrollEvent {
Left,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct ScrollLinesOptions {
pub number: bool,
pub wrap: bool,
Expand Down Expand Up @@ -105,6 +105,10 @@ impl ScrollLinesState {
pub fn toggle_number(&mut self) {
self.options.number = !self.options.number;
}

pub fn current_options(&self) -> ScrollLinesOptions {
self.options
}
}

#[derive(Debug, Default)]
Expand Down
Loading