Skip to content
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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SFTP · SCP · FTP/FTPS · WebDAV · S3 · Azure · Google Cloud Storage — on
### 🗂️ Classic commander, modern feel
Two panels, session tabs, bookmarks, drag & drop and full keyboard control with the
**original Norton Commander layout** and function key bar — or Total Commander's.
Light and dark theme, English and German.
Light and dark theme, ten skins, English and German.

</td>
<td width="50%" valign="top">
Expand Down Expand Up @@ -107,6 +107,17 @@ through any SSH session.
Host-key verification, TLS checks, an encrypted credential vault (XChaCha20-Poly1305,
optional Argon2id master password), throttled and CSRF-protected web logins.

</td>
</tr>
<tr>
<td colspan="2" valign="top">

### 🎨 Skins
Ten built-in skins — from the standard look to *Retro Blue*, *Nord*, *Solarized*, *Catppuccin*,
*Gruvbox*, *Dracula* and *High Contrast* — that color everything, terminal and editor included.
Adjust accent, row height, fonts and corners, or **create your own** in the skin editor or as a
small JSON file: [Skins](docs/src/guide/skins.md) · [Creating skins](docs/src/guide/creating-skins.md).

</td>
</tr>
</table>
Expand All @@ -120,6 +131,8 @@ optional Argon2id master password), throttled and CSRF-protected web logins.
| ![Remote Explorer](docs/src/images/remote-explorer.png) | ![Synchronization checklist](docs/src/images/sync-checklist.png) |
| **Nearby instances on the LAN** | **Share this instance** |
| ![Nearby](docs/src/images/nearby.png) | ![Share this instance](docs/src/images/share.png) |
| **Skins** | **Skin editor** |
| ![Skins in Preferences](docs/src/images/skins-gallery.png) | ![Skin editor](docs/src/images/skin-editor.png) |

## 🚀 Quick start

Expand Down Expand Up @@ -176,6 +189,7 @@ The full documentation lives in [`docs/`](docs/) and can be built as a website w
| **Deployment** | [Editor](docs/src/guide/editor.md) · [Remote Explorer](docs/src/guide/remote-explorer.md) | **Development** |
| [Headless server](docs/src/deployment/headless-server.md) | [Terminal and SSH tunnels](docs/src/guide/terminal-and-tunnels.md) | [Architecture](docs/src/development/architecture.md) |
| [Security](docs/src/deployment/security.md) | [LAN discovery](docs/src/guide/lan-discovery.md) · [Web mode](docs/src/guide/web-mode.md) | [Testing](docs/src/development/testing.md) · [Releasing](docs/src/development/releasing.md) |
| | [Skins](docs/src/guide/skins.md) · [Creating skins](docs/src/guide/creating-skins.md) | [Skin tokens](docs/src/reference/skin-tokens.md) |

## 🛠️ Building

Expand Down
20 changes: 20 additions & 0 deletions crates/rc-api/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rc_core::{Error, Protocol};
use crate::handlers::{compare, discovery, edit, explorer, format, fs, jobs, misc, sessions, terminal};
use crate::server::ServerStatus;
use crate::settings::Settings;
use crate::skins;
use crate::state::{AppState, Bookmark, CustomCommand, Origin};

/// Error returned to the UI.
Expand Down Expand Up @@ -540,6 +541,25 @@ pub async fn dispatch(state: &Arc<AppState>, origin: &Origin, method: &str, para
}
"commands.run" => ok(misc::command_run(st, origin, parse(params)?).await?),

// ---------------------------------------------------------- skins
"skins.list" => ok(skins::list(&skins::skins_dir(&st.config_dir))),
"skins.save" => {
#[derive(Deserialize)]
struct P {
skin: Value,
}
let p: P = parse(params)?;
let saved = skins::save(&skins::skins_dir(&st.config_dir), p.skin)?;
st.events.emit("skins.changed", json!({ "id": saved.skin["id"] }));
ok(saved)
}
"skins.delete" => {
let p: Id = parse(params)?;
skins::delete(&skins::skins_dir(&st.config_dir), &p.id)?;
st.events.emit("skins.changed", json!({ "id": p.id }));
ok(Value::Null)
}

// ---------------------------------------------------------- external editing
"edit.external" => {
desktop_only(origin)?;
Expand Down
1 change: 1 addition & 0 deletions crates/rc-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod persist;
pub mod prompts;
pub mod server;
pub mod settings;
pub mod skins;
pub mod state;

pub use dispatch::{dispatch, RpcError};
Expand Down
58 changes: 58 additions & 0 deletions crates/rc-api/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,52 @@ impl Default for TerminalSettings {
}
}

/// Look of the user interface: a skin plus the user's own adjustments on top of it.
/// `None` = what the skin says.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct AppearanceSettings {
/// Id of a built-in skin or of a file in `<config dir>/skins/`. Unknown ids fall back
/// to "default".
pub skin: String,
/// Accent color (CSS color), for both the light and the dark variant.
pub accent: Option<String>,
/// File list density: "compact", "normal" or "comfortable".
pub density: Option<String>,
/// Font size of the file lists in pixels.
pub list_font_size: Option<u32>,
/// Corner radius of controls in pixels.
pub radius: Option<u32>,
/// Alternating row backgrounds in the file lists.
pub stripes: Option<bool>,
/// Font of the user interface (CSS font-family).
pub font_ui: Option<String>,
/// Monospaced font (paths, editor, permissions).
pub font_mono: Option<String>,
}

impl Default for AppearanceSettings {
fn default() -> Self {
AppearanceSettings {
skin: "default".into(),
accent: None,
density: None,
list_font_size: None,
radius: None,
stripes: None,
font_ui: None,
font_mono: None,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct Settings {
pub language: String,
/// "system", "light" or "dark": which variant of the skin to show.
pub theme: String,
pub appearance: AppearanceSettings,
pub show_hidden: bool,
pub confirm_delete: bool,
pub confirm_overwrite: bool,
Expand Down Expand Up @@ -123,6 +164,7 @@ impl Default for Settings {
Settings {
language: "auto".into(),
theme: "system".into(),
appearance: AppearanceSettings::default(),
show_hidden: false,
confirm_delete: true,
confirm_overwrite: true,
Expand Down Expand Up @@ -162,4 +204,20 @@ mod tests {
assert_eq!(v["keymap"], "total");
assert_eq!(v["showKeyBar"], false);
}

#[test]
fn appearance_defaults_to_the_default_skin() {
let s: Settings = serde_json::from_str(r#"{ "theme": "dark" }"#).unwrap();
assert_eq!(s.appearance.skin, "default");
assert!(s.appearance.accent.is_none());

let s: Settings =
serde_json::from_str(r##"{ "appearance": { "skin": "nord", "accent": "#ff8800", "stripes": true } }"##)
.unwrap();
assert_eq!(s.appearance.skin, "nord");
assert_eq!(s.appearance.accent.as_deref(), Some("#ff8800"));
let v = serde_json::to_value(&s).unwrap();
assert_eq!(v["appearance"]["stripes"], true);
assert_eq!(v["appearance"]["listFontSize"], serde_json::Value::Null);
}
}
224 changes: 224 additions & 0 deletions crates/rc-api/src/skins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
//! User skins: one JSON file per skin in `<config dir>/skins/`.
//!
//! The skin format (colors, fonts, shapes) is interpreted by the user interface, which
//! validates every value before it applies it (see `ui/src/lib/skins/`). The backend only
//! stores the files: it checks that a skin is a JSON object with a usable `id` and `name`,
//! keeps them small, and makes sure an id can never name a file outside the skins folder.
//!
//! Files may also be dropped into the folder by hand. Their `id` decides which skin they
//! are; a file without one uses its file name (`ocean.json` → `ocean`).

use std::path::{Path, PathBuf};

use serde::Serialize;
use serde_json::Value;

use rc_core::store::save_json;
use rc_core::{Error, Result};

/// Larger files are not skins (and would slow down every start of the UI).
pub const MAX_SKIN_BYTES: u64 = 256 * 1024;
/// At most this many skins are loaded.
pub const MAX_SKINS: usize = 200;

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SkinFile {
/// File name inside the skins folder.
pub file: String,
/// The skin as stored (`id` is always set).
pub skin: Value,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SkinFileError {
pub file: String,
pub message: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SkinList {
/// The skins folder (native path), for "open folder" and the documentation.
pub dir: String,
pub skins: Vec<SkinFile>,
/// Files that could not be loaded.
pub errors: Vec<SkinFileError>,
}

pub fn skins_dir(config_dir: &Path) -> PathBuf {
config_dir.join("skins")
}

/// `a-z`, `0-9`, `-` and `_`, starting with a letter or digit, at most 64 characters.
pub fn valid_id(id: &str) -> bool {
let b = id.as_bytes();
!b.is_empty()
&& b.len() <= 64
&& (b[0].is_ascii_lowercase() || b[0].is_ascii_digit())
&& b.iter().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == b'-' || *c == b'_')
}

fn read_skin(path: &Path) -> std::result::Result<Value, String> {
let meta = std::fs::metadata(path).map_err(|e| e.to_string())?;
if meta.len() > MAX_SKIN_BYTES {
return Err(format!("larger than {} KiB", MAX_SKIN_BYTES / 1024));
}
let data = std::fs::read(path).map_err(|e| e.to_string())?;
let mut v: Value = serde_json::from_slice(&data).map_err(|e| format!("invalid JSON: {e}"))?;
let obj = v.as_object_mut().ok_or("not a JSON object")?;
let id = match obj.get("id") {
Some(Value::String(id)) => id.clone(),
None => path.file_stem().and_then(|s| s.to_str()).unwrap_or_default().to_ascii_lowercase(),
Some(_) => return Err("\"id\" is not a string".into()),
};
if !valid_id(&id) {
return Err(format!("invalid id {id:?} (use a-z, 0-9, - and _)"));
}
obj.insert("id".into(), Value::String(id));
Ok(v)
}

/// All skins in `dir`, sorted by file name. A missing folder is an empty list.
pub fn list(dir: &Path) -> SkinList {
let mut out = SkinList { dir: dir.display().to_string(), skins: Vec::new(), errors: Vec::new() };
let Ok(rd) = std::fs::read_dir(dir) else {
return out;
};
let mut files: Vec<PathBuf> = rd
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.is_file() && p.extension().is_some_and(|x| x.eq_ignore_ascii_case("json")))
.collect();
files.sort();
for path in files {
let file = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
if out.skins.len() >= MAX_SKINS {
out.errors.push(SkinFileError { file, message: format!("more than {MAX_SKINS} skins") });
continue;
}
match read_skin(&path) {
Ok(skin) => {
let id = skin["id"].as_str().unwrap_or_default();
if let Some(first) = out.skins.iter().find(|s| s.skin["id"] == id) {
let message = format!("id {id:?} is already used by {}", first.file);
out.errors.push(SkinFileError { file, message });
} else {
out.skins.push(SkinFile { file, skin });
}
}
Err(message) => out.errors.push(SkinFileError { file, message }),
}
}
out
}

/// Store a skin (create or replace). A skin that came from a hand-named file is written
/// back to that file.
pub fn save(dir: &Path, mut skin: Value) -> Result<SkinFile> {
let obj = skin.as_object_mut().ok_or_else(|| Error::InvalidInput("a skin must be a JSON object".into()))?;
let id = obj.get("id").and_then(Value::as_str).unwrap_or_default().to_string();
if !valid_id(&id) {
return Err(Error::InvalidInput(format!("invalid skin id {id:?} (use a-z, 0-9, - and _)")));
}
let name_ok = obj.get("name").and_then(Value::as_str).is_some_and(|n| !n.trim().is_empty());
if !name_ok {
return Err(Error::InvalidInput("a skin needs a name".into()));
}
let size = serde_json::to_vec_pretty(&skin)?.len() as u64;
if size > MAX_SKIN_BYTES {
return Err(Error::InvalidInput(format!("skin is larger than {} KiB", MAX_SKIN_BYTES / 1024)));
}
let existing = list(dir).skins.into_iter().find(|s| s.skin["id"] == id.as_str());
let file = existing.map(|s| s.file).unwrap_or_else(|| format!("{id}.json"));
save_json(&dir.join(&file), &skin)?;
Ok(SkinFile { file, skin })
}

/// Remove the file(s) of a skin. Unknown ids are not an error.
pub fn delete(dir: &Path, id: &str) -> Result<()> {
if !valid_id(id) {
return Err(Error::InvalidInput(format!("invalid skin id {id:?}")));
}
for s in list(dir).skins.into_iter().filter(|s| s.skin["id"] == id) {
let path = dir.join(&s.file);
std::fs::remove_file(&path).map_err(|e| Error::io_at(e, &path.display().to_string()))?;
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn ids_cannot_leave_the_folder() {
for bad in ["", "../x", "a/b", "a\\b", "A", "-x", ".hidden", "x.json", &"x".repeat(65)] {
assert!(!valid_id(bad), "{bad:?} accepted");
}
for good in ["default", "nord-2", "my_skin", "9lives"] {
assert!(valid_id(good), "{good:?} rejected");
}
}

#[test]
fn save_list_delete() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("skins");
assert!(list(&dir).skins.is_empty());

let saved = save(&dir, json!({ "id": "ocean", "name": "Ocean", "dark": { "accent": "#0af" } })).unwrap();
assert_eq!(saved.file, "ocean.json");
let l = list(&dir);
assert_eq!(l.skins.len(), 1);
assert_eq!(l.skins[0].skin["dark"]["accent"], "#0af");

// replaced, not duplicated
save(&dir, json!({ "id": "ocean", "name": "Ocean 2" })).unwrap();
let l = list(&dir);
assert_eq!(l.skins.len(), 1);
assert_eq!(l.skins[0].skin["name"], "Ocean 2");

delete(&dir, "ocean").unwrap();
assert!(list(&dir).skins.is_empty());
delete(&dir, "ocean").unwrap();
}

#[test]
fn rejects_invalid_skins() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
assert!(save(dir, json!([1, 2])).is_err());
assert!(save(dir, json!({ "id": "../evil", "name": "x" })).is_err());
assert!(save(dir, json!({ "id": "ok" })).is_err());
assert!(save(dir, json!({ "id": "ok", "name": " " })).is_err());
let huge = "x".repeat(MAX_SKIN_BYTES as usize);
assert!(save(dir, json!({ "id": "big", "name": "Big", "description": huge })).is_err());
assert!(std::fs::read_dir(dir).unwrap().next().is_none(), "nothing may be written");
}

#[test]
fn hand_written_files() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
std::fs::write(dir.join("My Theme.json"), r#"{ "id": "mine", "name": "Mine" }"#).unwrap();
std::fs::write(dir.join("sunset.json"), r#"{ "name": "Sunset" }"#).unwrap();
std::fs::write(dir.join("broken.json"), "{ nope").unwrap();
std::fs::write(dir.join("twice.json"), r#"{ "id": "mine", "name": "Again" }"#).unwrap();
std::fs::write(dir.join("notes.txt"), "ignored").unwrap();

let l = list(dir);
let ids: Vec<_> = l.skins.iter().map(|s| s.skin["id"].as_str().unwrap().to_string()).collect();
assert_eq!(ids, ["mine", "sunset"]);
let bad: Vec<_> = l.errors.iter().map(|e| e.file.as_str()).collect();
assert_eq!(bad, ["broken.json", "twice.json"]);

// saving keeps the user's file name
let s = save(dir, json!({ "id": "mine", "name": "Mine 2" })).unwrap();
assert_eq!(s.file, "My Theme.json");
delete(dir, "mine").unwrap();
assert!(!dir.join("My Theme.json").exists());
}
}
Loading
Loading