Skip to content

Added Rust category and a snippet #197

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 16 additions & 0 deletions public/consolidated/rust.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
}
]
},
{
"name": "Linux",
"snippets": [
{
"title": "Get Desktop Enviroment",
"description": "Get the Desktop Enviroment that the user is currently using.",
"author": "sponkurtus2 ",
"tags": [
"linux",
"file"
],
"contributors": [],
"code": "fn get_desktop_env() -> String {\n // Return empty string if no X display is available\n if env::var(\"DISPLAY\").is_err() {\n return String::new();\n }\n\n // Check common desktop environment variables.\n for env_var in &[\n \"XDG_SESSION_DESKTOP\",\n \"XDG_CURRENT_DESKTOP\",\n \"DESKTOP_SESSION\",\n ] {\n if let Ok(de) = env::var(env_var) {\n return de;\n }\n }\n\n // As fallback, try to get desktop name from last word of last line in .xinitrc\n let path = format!(\"{}/.xinitrc\", env::var(\"HOME\").unwrap_or_default());\n if let Ok(mut file) = File::open(&path) {\n let mut buf = String::new();\n if file.read_to_string(&mut buf).is_ok() {\n if let Some(last_line) = buf.lines().last() {\n let last_word = last_line.split(' ').last().unwrap_or(\"\");\n return last_word.to_string();\n }\n }\n }\n\n // Return \"N/A\" if no desktop environment could be detected\n String::from(\"N/A\")\n}\n\n// Usage:\nget_desktop_env(); // Returns: the desktop enviroment that the user actually has e.g. i3.\n"
}
]
},
{
"name": "String Manipulation",
"snippets": [
Expand Down
44 changes: 44 additions & 0 deletions snippets/rust/linux/get-desktop-enviroment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: Get Desktop Enviroment
description: Get the Desktop Enviroment that the user is currently using.
author: sponkurtus2
tags: linux,file
---

```rust
fn get_desktop_env() -> String {
// Return empty string if no X display is available
if env::var("DISPLAY").is_err() {
return String::new();
}

// Check common desktop environment variables.
for env_var in &[
"XDG_SESSION_DESKTOP",
"XDG_CURRENT_DESKTOP",
"DESKTOP_SESSION",
] {
if let Ok(de) = env::var(env_var) {
return de;
}
}

// As fallback, try to get desktop name from last word of last line in .xinitrc
let path = format!("{}/.xinitrc", env::var("HOME").unwrap_or_default());
if let Ok(mut file) = File::open(&path) {
let mut buf = String::new();
if file.read_to_string(&mut buf).is_ok() {
if let Some(last_line) = buf.lines().last() {
let last_word = last_line.split(' ').last().unwrap_or("");
return last_word.to_string();
}
}
}

// Return "N/A" if no desktop environment could be detected
String::from("N/A")
}

// Usage:
get_desktop_env(); // Returns: the desktop enviroment that the user actually has e.g. i3.
```
Loading