on master help sign in
Show whether file readonly in statusline (#7740)
commitfcbac485f885d90f6341d7acaf75157a01d81892 authorConnortsui20 <87130162+Connortsui20@users.noreply.github.com> dateTue 8 Aug 2023, 08:51 (3 years ago) parenta7a145a
7 files changed+58−2
Cargo.lock+1 book/src/configuration.md+2−1 helix-term/src/commands/typed.rs+1−1 helix-term/src/ui/statusline.rs+14 helix-view/Cargo.toml+1 helix-view/src/document.rs+35 helix-view/src/editor.rs+4
Cargo.lock+1
@@ -1392,6 +1392,7 @@ dependencies = [
13921392 "log",
13931393 "once_cell",
13941394 "parking_lot",
1395+ "rustix 0.38.4",
13951396 "serde",
13961397 "serde_json",
13971398 "slotmap",
book/src/configuration.md+2−1
@@ -89,7 +89,7 @@ The `[editor.statusline]` key takes the following sub-keys:
8989
9090| Key | Description | Default |
9191| --- | --- | --- |
92−| `left` | A list of elements aligned to the left of the statusline | `["mode", "spinner", "file-name", "file-modification-indicator"]` |
92+| `left` | A list of elements aligned to the left of the statusline | `["mode", "spinner", "file-name", "read-only-indicator", "file-modification-indicator"]` |
9393| `center` | A list of elements aligned to the middle of the statusline | `[]` |
9494| `right` | A list of elements aligned to the right of the statusline | `["diagnostics", "selections", "register", "position", "file-encoding"]` |
9595| `separator` | The character used to separate elements in the statusline | `"│"` |
@@ -108,6 +108,7 @@ The following statusline elements can be configured:
108108| `file-modification-indicator` | The indicator to show whether the file is modified (a `[+]` appears when there are unsaved changes) |
109109| `file-encoding` | The encoding of the opened file if it differs from UTF-8 |
110110| `file-line-ending` | The file line endings (CRLF or LF) |
111+| `read-only-indicator` | An indicator that shows `[readonly]` when a file cannot be written |
111112| `total-line-numbers` | The total line numbers of the opened file |
112113| `file-type` | The type of the opened file |
113114| `diagnostics` | The number of warnings and/or errors |
helix-term/src/commands/typed.rs+1−1
@@ -672,7 +672,7 @@ pub fn write_all_impl(
672672 }
673673 if doc.path().is_none() {
674674 if write_scratch {
675− errors.push("cannot write a buffer without a filename\n");
675+ errors.push("cannot write a buffer without a filename");
676676 }
677677 return None;
678678 }
helix-term/src/ui/statusline.rs+14
@@ -145,6 +145,7 @@ where
145145 helix_view::editor::StatusLineElement::FileModificationIndicator => {
146146 render_file_modification_indicator
147147 }
148+ helix_view::editor::StatusLineElement::ReadOnlyIndicator => render_read_only_indicator,
148149 helix_view::editor::StatusLineElement::FileEncoding => render_file_encoding,
149150 helix_view::editor::StatusLineElement::FileLineEnding => render_file_line_ending,
150151 helix_view::editor::StatusLineElement::FileType => render_file_type,
@@ -442,6 +443,19 @@ where
442443 write(context, title, None);
443444}
444445
446+fn render_read_only_indicator<F>(context: &mut RenderContext, write: F)
447+where
448+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
449+{
450+ let title = if context.doc.readonly {
451+ " [readonly] "
452+ } else {
453+ ""
454+ }
455+ .to_string();
456+ write(context, title, None);
457+}
458+
445459fn render_file_base_name<F>(context: &mut RenderContext, write: F)
446460where
447461 F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
helix-view/Cargo.toml+1
@@ -51,6 +51,7 @@ clipboard-win = { version = "4.5", features = ["std"] }
5151
5252[target.'cfg(unix)'.dependencies]
5353libc = "0.2"
54+rustix = { version = "0.38", features = ["fs"] }
5455
5556[dev-dependencies]
5657helix-tui = { path = "../helix-tui" }
helix-view/src/document.rs+35
@@ -185,6 +185,8 @@ pub struct Document {
185185
186186 // when document was used for most-recent-used buffer picker
187187 pub focused_at: std::time::Instant,
188+
189+ pub readonly: bool,
188190}
189191
190192/// Inlay hints for a single `(Document, View)` combo.
@@ -673,6 +675,7 @@ impl Document {
673675 config,
674676 version_control_head: None,
675677 focused_at: std::time::Instant::now(),
678+ readonly: false,
676679 }
677680 }
678681
@@ -955,6 +958,33 @@ impl Document {
955958 }
956959 }
957960
961+ #[cfg(unix)]
962+ // Detect if the file is readonly and change the readonly field if necessary (unix only)
963+ pub fn detect_readonly(&mut self) {
964+ use rustix::fs::{access, Access};
965+ // Allows setting the flag for files the user cannot modify, like root files
966+ self.readonly = match &self.path {
967+ None => false,
968+ Some(p) => access(p, Access::WRITE_OK).is_err(),
969+ };
970+ }
971+
972+ #[cfg(not(unix))]
973+ // Detect if the file is readonly and change the readonly field if necessary (non-unix os)
974+ pub fn detect_readonly(&mut self) {
975+ // TODO Use the Windows' function `CreateFileW` to check if a file is readonly
976+ // Discussion: https://github.com/helix-editor/helix/pull/7740#issuecomment-1656806459
977+ // Vim implementation: https://github.com/vim/vim/blob/4c0089d696b8d1d5dc40568f25ea5738fa5bbffb/src/os_win32.c#L7665
978+ // Windows binding: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Storage/FileSystem/fn.CreateFileW.html
979+ self.readonly = match &self.path {
980+ None => false,
981+ Some(p) => match std::fs::metadata(p) {
982+ Err(_) => false,
983+ Ok(metadata) => metadata.permissions().readonly(),
984+ },
985+ };
986+ }
987+
958988 /// Reload the document from its path.
959989 pub fn reload(
960990 &mut self,
@@ -969,6 +999,9 @@ impl Document {
969999 .ok_or_else(|| anyhow!("can't find file to reload from {:?}", self.display_name()))?
9701000 .to_owned();
9711001
1002+ // Once we have a valid path we check if its readonly status has changed
1003+ self.detect_readonly();
1004+
9721005 let mut file = std::fs::File::open(&path)?;
9731006 let (rope, ..) = from_reader(&mut file, Some(encoding))?;
9741007
@@ -1018,6 +1051,8 @@ impl Document {
10181051 // and error out when document is saved
10191052 self.path = path;
10201053
1054+ self.detect_readonly();
1055+
10211056 Ok(())
10221057 }
10231058
helix-view/src/editor.rs+4
@@ -421,6 +421,7 @@ impl Default for StatusLineConfig {
421421 E::Mode,
422422 E::Spinner,
423423 E::FileName,
424+ E::ReadOnlyIndicator,
424425 E::FileModificationIndicator,
425426 ],
426427 center: vec![],
@@ -473,6 +474,9 @@ pub enum StatusLineElement {
473474 // The file modification indicator
474475 FileModificationIndicator,
475476
477+ /// An indicator that shows `"[readonly]"` when a file cannot be written
478+ ReadOnlyIndicator,
479+
476480 /// The file encoding
477481 FileEncoding,
478482