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

add customizable read-only indicator to statusbar #2862

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
681d34b
initial implementation of bufferline
aaron404 Jun 13, 2022
fc8df18
fixed lint
aaron404 Jun 13, 2022
860ec7c
changed to 'bufferline', added enum for config modes, some cleanup
aaron404 Jun 13, 2022
4cb66c3
fixed lint
aaron404 Jun 13, 2022
479823d
added file modification indicator
aaron404 Jun 13, 2022
a2c6199
removed redundant code, added proper themeing with fallback, changed …
aaron404 Jun 14, 2022
1eca21e
add read-only flag to statusbar
sbromberger Jun 22, 2022
b36ce8f
doc cleanup
sbromberger Jun 23, 2022
824cbc6
map_or instead of map_or_else
sbromberger Jun 23, 2022
2bbd316
remove commented code
aaron404 Jun 23, 2022
b7d26ce
Merge branch 'master' into 497-add-bufferline
aaron404 Jun 23, 2022
039ac47
Update helix-term/src/ui/editor.rs
sbromberger Jun 23, 2022
ecb9c23
Update helix-term/src/ui/editor.rs
aaron404 Jun 23, 2022
1fe3e70
renamed FileStat / file_stat to WritePermission/write_permission
sbromberger Jun 25, 2022
6bb75fa
Merge branch 'sbromberger/read-only-indicator' of github.com:sbromber…
sbromberger Jun 25, 2022
f39b2fa
add ui.bufferline.background key for themes
aaron404 Jun 28, 2022
d80a9fd
address PR comments
aaron404 Jun 29, 2022
fe102bc
merge master
aaron404 Jun 29, 2022
8dfb9fe
Update helix-term/src/ui/editor.rs
aaron404 Jun 29, 2022
db5f30a
simplify computation of editor area:
aaron404 Jun 29, 2022
7ff2ff5
change to set_stringn to avoid overflow
aaron404 Jul 3, 2022
7fdb653
Merge branch 'master' into 497-add-bufferline
aaron404 Jul 5, 2022
c3a8892
Merge branch 'master' into sbromberger/read-only-indicator
sbromberger Jul 21, 2022
9c1eab4
refactor ro indicator given customizable statusline
sbromberger Jul 21, 2022
f926824
corrected docs for filename and ro indicator
sbromberger Jul 21, 2022
8d59fe9
Update configuration.md
sbromberger Jul 22, 2022
fda90d0
remove commented code
sbromberger Jul 23, 2022
0cb3dff
addressed pr comments
aaron404 Jul 24, 2022
0e3caa6
Merge branch 'helix-editor:master' into 497-add-bufferline
aaron404 Jul 25, 2022
5e7ccc9
Merge pull request #1 from sbromberger/patch-1
aaron404 Jul 26, 2022
303a721
Merge branch 'master' into sbromberger/read-only-indicator
sbromberger Jul 26, 2022
ec6d12f
fix build error
aaron404 Jul 27, 2022
5136a1b
Merge branch 'master' into sbromberger/read-only-indicator
sbromberger Jul 27, 2022
2874fa6
Merge branch 'master' into sbromberger/read-only-indicator
sbromberger Jul 31, 2022
3ad44f9
Update helix-view/src/document.rs
sbromberger Jul 31, 2022
e8a230a
Update helix-view/src/document.rs
sbromberger Jul 31, 2022
2922e68
Merge branch 'sbromberger/read-only-indicator' of github.com:sbromber…
sbromberger Jul 31, 2022
3ecea29
merge master, plus readonly->readonly_indicator
sbromberger Jul 31, 2022
cc3ab9f
needs more bikeshed
sbromberger Jul 31, 2022
042805d
address pr comments
aaron404 Aug 31, 2022
12846be
revert accidental change
aaron404 Aug 31, 2022
20ecab4
Merge commit 'refs/pull/2759/head' of https://github.com/helix-editor…
sbromberger Sep 1, 2022
346734c
Merge branch 'master' of https://github.com/helix-editor/helix
sbromberger Sep 2, 2022
30edb39
Merge branch 'master' into sbromberger/read-only-indicator
sbromberger Sep 2, 2022
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
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
16 changes: 15 additions & 1 deletion helix-term/src/ui/statusline.rs
@@ -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 @@ -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
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
}
});

Ok(doc)
}
Expand Down
10 changes: 9 additions & 1 deletion helix-view/src/editor.rs
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