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

read only indicator #3709

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You may also specify a file to use for configuration with the `-c` or
| `auto-info` | Whether to display infoboxes | `true` |
| `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. | `false` |
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file. | `[]` |
| `read-only-indicator` | String to display next to filename in statusbar when configured and if file is read-only. Set to `""` to disable. | `"[RO]"` |
| `bufferline` | Renders a line at the top of the editor displaying open buffers. Can be `always`, `never` or `multiple` (only shown if more than one buffer is in use) | `never` |
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |

Expand All @@ -64,8 +65,8 @@ Statusline elements can be defined as follows:

```toml
[editor.statusline]
left = ["mode", "spinner"]
center = ["file-name"]
left = ["mode", "spinner", "file-name", "read-only-indicator"]
center = []
right = ["diagnostics", "selections", "position", "file-encoding", "file-line-ending", "file-type"]
separator = "│"
```
Expand All @@ -77,6 +78,7 @@ The following elements can be configured:
| `mode` | The current editor mode (`NOR`/`INS`/`SEL`) |
| `spinner` | A progress spinner indicating LSP activity |
| `file-name` | The path/name of the opened file |
| `read-only-indicator` | When configured, indicates whether the file is read-only. |
| `file-encoding` | The encoding of the opened file if it differs from UTF-8 |
| `file-line-ending` | The file line endings (CRLF or LF) |
| `file-type` | The type of the opened file |
Expand Down
18 changes: 16 additions & 2 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use helix_core::{coords_at_pos, encoding, Position};
use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME},
document::{Mode, WritePermission, SCRATCH_BUFFER_NAME},
graphics::Rect,
theme::Style,
Document, Editor, View,
Expand Down Expand Up @@ -144,6 +144,7 @@ where
helix_view::editor::StatusLineElement::Selections => render_selections,
helix_view::editor::StatusLineElement::Position => render_position,
helix_view::editor::StatusLineElement::PositionPercentage => render_position_percentage,
helix_view::editor::StatusLineElement::ReadOnlyIndicator => render_read_only_indicator,
helix_view::editor::StatusLineElement::Separator => render_separator,
helix_view::editor::StatusLineElement::Spacer => render_spacer,
}
Expand Down Expand Up @@ -345,7 +346,7 @@ where
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!(
" {}{} ",
"{}{}",
path,
if context.doc.is_modified() { "[+]" } else { "" }
)
Expand All @@ -354,6 +355,19 @@ where
write(context, title, None);
}

fn render_read_only_indicator<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let read_only_indicator = if context.doc.write_permission == WritePermission::ReadOnly {
context.editor.config().read_only_indicator.to_string()
} else {
String::new()
};

write(context, read_only_indicator, None);
}

fn render_separator<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
Expand Down
22 changes: 21 additions & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ pub enum Mode {
Insert = 2,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize)]
pub enum WritePermission {
Writable,
ReadOnly,
Unknown,
}

impl Display for Mode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -120,9 +127,11 @@ pub struct Document {

diagnostics: Vec<Diagnostic>,
language_server: Option<Arc<helix_lsp::Client>>,
/// Write permissions of the file - read-only, writable, or unknown.
pub write_permission: WritePermission,
}

use std::{fmt, mem};
use std::{fmt, fs, mem};
impl fmt::Debug for Document {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Document")
Expand All @@ -141,6 +150,7 @@ impl fmt::Debug for Document {
.field("version", &self.version)
.field("modified_since_accessed", &self.modified_since_accessed)
.field("diagnostics", &self.diagnostics)
.field("write_permission", &self.write_permission)
// .field("language_server", &self.language_server)
.finish()
}
Expand Down Expand Up @@ -358,6 +368,7 @@ impl Document {
last_saved_revision: 0,
modified_since_accessed: false,
language_server: None,
write_permission: WritePermission::Unknown,
}
}

Expand Down Expand Up @@ -388,6 +399,15 @@ impl Document {
}

doc.detect_indent_and_line_ending();
// this seems overly complex but should make it easier
// to extend doc.file_stat with more information in the future.
doc.write_permission = fs::metadata(path).map_or(WritePermission::Unknown, |f| {
if f.permissions().readonly() {
WritePermission::ReadOnly
} else {
WritePermission::Writable
}
});
Comment on lines +402 to +410
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be part of doc.set_path since a path can also be changed after opening?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the implications of changing a path after opening?


Ok(doc)
}
Expand Down
10 changes: 9 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}

use arc_swap::access::{DynAccess, DynGuard};

const DEFAULT_READ_ONLY_INDICATOR: &str = "[RO]";

fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down Expand Up @@ -166,6 +168,8 @@ pub struct Config {
pub bufferline: BufferLine,
/// Vertical indent width guides.
pub indent_guides: IndentGuidesConfig,
/// String indicating whether a file is read-only. Defaults to `"[RO]"`
pub read_only_indicator: String,
/// Whether to color modes with different colors. Defaults to `false`.
pub color_modes: bool,
}
Expand Down Expand Up @@ -267,7 +271,7 @@ impl Default for StatusLineConfig {
use StatusLineElement as E;

Self {
left: vec![E::Mode, E::Spinner, E::FileName],
left: vec![E::Mode, E::Spinner, E::FileName, E::ReadOnlyIndicator],
center: vec![],
right: vec![E::Diagnostics, E::Selections, E::Position, E::FileEncoding],
separator: String::from("│"),
Expand Down Expand Up @@ -305,6 +309,9 @@ pub enum StatusLineElement {
/// The cursor position
Position,

/// The read-only indicator
ReadOnlyIndicator,

/// The separator string
Separator,

Expand Down Expand Up @@ -576,6 +583,7 @@ impl Default for Config {
whitespace: WhitespaceConfig::default(),
bufferline: BufferLine::default(),
indent_guides: IndentGuidesConfig::default(),
read_only_indicator: DEFAULT_READ_ONLY_INDICATOR.to_string(),
color_modes: false,
}
}
Expand Down