Initial commit

This commit is contained in:
Iaphetes 2024-11-11 20:27:06 +01:00
commit d377de0999
6 changed files with 5139 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

4496
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "Apographe"
version = "0.1.0"
edition = "2021"
[dependencies]
iced = {version = "0.13", features = ["highlighter", "tokio", "debug"]}
tokio = {version = "1.41", features = ["fs"]}
rfd = "0.13"

112
flake.lock generated Normal file
View file

@ -0,0 +1,112 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixgl": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1713543440,
"narHash": "sha256-lnzZQYG0+EXl/6NkGpyIz+FEOc/DSEG57AP1VsdeNrM=",
"owner": "guibou",
"repo": "nixGL",
"rev": "310f8e49a149e4c9ea52f1adf70cdc768ec53f8a",
"type": "github"
},
"original": {
"owner": "guibou",
"repo": "nixGL",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1660551188,
"narHash": "sha256-a1LARMMYQ8DPx1BgoI/UN4bXe12hhZkCNqdxNi6uS0g=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "441dc5d512153039f19ef198e662e4f3dbb9fd65",
"type": "github"
},
"original": {
"owner": "nixos",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1731239293,
"narHash": "sha256-q2yjIWFFcTzp5REWQUOU9L6kHdCDmFDpqeix86SOvDc=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "9256f7c71a195ebe7a218043d9f93390d49e6884",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-24.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1728538411,
"narHash": "sha256-f0SBJz1eZ2yOuKUr5CA9BHULGXVSn6miBuUWdTyhUhU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b69de56fac8c2b6f8fd27f2eca01dcda8e0a4221",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixgl": "nixgl",
"nixpkgs": "nixpkgs_2",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": "nixpkgs_3"
},
"locked": {
"lastModified": 1731292155,
"narHash": "sha256-fYVoUUtSadbOrH0z0epVQDsStBDS/S/fAK//0ECQAAI=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "7c4cd99ed7604b79e8cb721099ac99c66f656b3a",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

210
flake.nix Normal file
View file

@ -0,0 +1,210 @@
{
description = "Rust dev shell for Hertog's bevy project (YAY!)";
# Flake inputs
inputs = {
# nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2305.491812.tar.gz";
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
rust-overlay.url = "github:oxalica/rust-overlay"; # A helper for Rust + Nix
nixgl.url = "github:guibou/nixGL"; # Allows you to run OpenGL and or Vulkan applications in a nix shell
};
# Flake outputs
outputs = { self, nixpkgs, rust-overlay, nixgl, ... }:
let
# Overlays enable you to customize the Nixpkgs attribute set
overlays = [
# Makes a `rust-bin` attribute available in Nixpkgs
(import rust-overlay)
nixgl.overlay
# Provides a `rustToolchain` attribute for Nixpkgs that we can use to
# create a Rust environment
(self: super: {
rustToolchain = super.rust-bin.stable.latest.default;
})
];
# Systems supported
allSystems = [
"x86_64-linux" # 64-bit Intel/AMD Linux
"aarch64-linux" # 64-bit ARM Linux
"x86_64-darwin" # 64-bit Intel macOS
"aarch64-darwin" # 64-bit ARM macOS
];
# Helper to provide system-specific attributes
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
pkgs = import nixpkgs { inherit overlays system; };
});
in
{
# Development environment output
devShells = forAllSystems ({ pkgs }: {
default = pkgs.mkShell {
# The Nix packages provided in the environment
packages = (with pkgs; [
# Bevy
pkg-config
alsa-lib
vulkan-tools
vulkan-headers
vulkan-loader
vulkan-validation-layers
udev
clang
lld
mold
# If using an intel GPU
pkgs.nixgl.nixVulkanIntel
# If on x11
xorg.libX11
xorg.libX11
xorg.libXcursor
xorg.libXi
xorg.libXrandr
# If on wayland
libxkbcommon
wayland
# Rust
rustup
rustToolchain
# Dev packages
helix
zellij
]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]);
shellHook = ''
# Required
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.lib.makeLibraryPath [
pkgs.alsaLib
pkgs.udev
pkgs.vulkan-loader
pkgs.libxkbcommon
]}"
# Aliases and other fluff/ease of use
alias runIntel="nixVulkanIntel cargo run"
echo "Welcome to nix-hell uh nix-shell!"
'';
};
});
};
}
# {
# description = "Flake for Orthros-BE";
# # Flake inputs
# inputs = {
# nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
# rust-overlay.url = "github:oxalica/rust-overlay"; # A helper for Rust + Nix
# nixgl.url = "github:guibou/nixGL"; # Allows you to run OpenGL and or Vulkan applications in a nix shell
# };
# # Flake outputs
# outputs = { self, nixpkgs, rust-overlay, nixgl, ... }:
# let
# # Overlays enable you to customize the Nixpkgs attribute set
# overlays = [
# # Makes a `rust-bin` attribute available in Nixpkgs
# (import rust-overlay)
# nixgl.overlay
# # Provides a `rustToolchain` attribute for Nixpkgs that we can use to
# # create a Rust environment
# (self: super: {
# rustToolchain = super.rust-bin.nightly.latest.default;
# })
# ];
# # Systems supported
# allSystems = [
# "x86_64-linux" # 64-bit Intel/AMD Linux
# "aarch64-linux" # 64-bit ARM Linux
# "x86_64-darwin" # 64-bit Intel macOS
# "aarch64-darwin" # 64-bit ARM macOS
# ];
# # Helper to provide system-specific attributes
# forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
# pkgs = import nixpkgs { inherit overlays system; };
# });
# in
# {
# # Development environment output
# devShells = forAllSystems ({ pkgs }: {
# default = pkgs.mkShell {
# # The Nix packages provided in the environment
# packages = (with pkgs; [
# # Bevy
# pkg-config
# alsa-lib
# vulkan-tools
# vulkan-headers
# vulkan-loader
# vulkan-validation-layers
# udev
# clang
# lld
# # If using an intel GPU
# pkgs.nixgl.nixVulkanIntel
# # If on x11
# xorg.libX11
# xorg.libXcursor
# xorg.libXi
# xorg.libXrandr
# # If on wayland
# libxkbcommon
# wayland
# # Rust
# rustup
# rustToolchain
# ]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]);
# buildInputs = [
# pkgs.udev
# pkgs.alsa-lib
# pkgs.vulkan-loader
# pkgs.xorg.libX11
# pkgs.xorglibXcursor
# pkgs.xorg.LibXi
# pkgs.xorg.libXrandr
# pkgs.libxkbcommon
# pkgs.wayland
# ];
# LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs;
# # shellHook = ''
# # Required
# # export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.lib.makeLibraryPath [
# # pkgs.alsaLib
# # pkgs.udev
# # pkgs.vulkan-loader
# # pkgs.libxkbcommon
# # pkgs.libllvm
# # pkgs.libdrm
# # pkgs.libelf
# # pkgs.elfutils
# # pkgs.xorg.libxcb
# # pkgs.zstd
# # # If using an intel GPU
# # pkgs.nixgl.nixVulkanIntel
# # pkgs.amdvlk
# # # If on x11
# # pkgs.xorg.libX11
# # pkgs.xorg.libXcursor
# # pkgs.xorg.libXi
# # pkgs.xorg.libXrandr
# # pkgs.xorg.libxshmfence
# # pkgs.amdvlk
# # pkgs.xorg.xcbutilkeysyms
# # pkgs.vulkan-tools
# # pkgs.vulkan-headers
# # pkgs.vulkan-loader
# # pkgs.vulkan-validation-layers
# # pkgs.lld
# # ]
# # }"
# # Aliases and other fluff/ease of use
# # alias runIntel="nixVulkanIntel cargo run"
# # '';
# };
# });
# };
# }

310
src/main.rs Normal file
View file

@ -0,0 +1,310 @@
use iced::highlighter;
use iced::keyboard;
use iced::widget::{
self, button, column, container, horizontal_space, pick_list, row, text, text_editor, toggler,
tooltip,
};
use iced::{Center, Element, Fill, Font, Task, Theme};
use std::ffi;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
pub fn main() -> iced::Result {
iced::application("Editor - Iced", Editor::update, Editor::view)
.theme(Editor::theme)
.font(include_bytes!("../fonts/icons.ttf").as_slice())
.default_font(Font::MONOSPACE)
.run_with(Editor::new)
}
struct Editor {
file: Option<PathBuf>,
content: text_editor::Content,
theme: highlighter::Theme,
word_wrap: bool,
is_loading: bool,
is_dirty: bool,
}
#[derive(Debug, Clone)]
enum Message {
ActionPerformed(text_editor::Action),
ThemeSelected(highlighter::Theme),
WordWrapToggled(bool),
NewFile,
OpenFile,
FileOpened(Result<(PathBuf, Arc<String>), Error>),
SaveFile,
FileSaved(Result<PathBuf, Error>),
}
impl Editor {
fn new() -> (Self, Task<Message>) {
(
Self {
file: None,
content: text_editor::Content::new(),
theme: highlighter::Theme::SolarizedDark,
word_wrap: true,
is_loading: true,
is_dirty: false,
},
Task::batch([
Task::perform(
load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))),
Message::FileOpened,
),
widget::focus_next(),
]),
)
}
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::ActionPerformed(action) => {
self.is_dirty = self.is_dirty || action.is_edit();
self.content.perform(action);
Task::none()
}
Message::ThemeSelected(theme) => {
self.theme = theme;
Task::none()
}
Message::WordWrapToggled(word_wrap) => {
self.word_wrap = word_wrap;
Task::none()
}
Message::NewFile => {
if !self.is_loading {
self.file = None;
self.content = text_editor::Content::new();
}
Task::none()
}
Message::OpenFile => {
if self.is_loading {
Task::none()
} else {
self.is_loading = true;
Task::perform(open_file(), Message::FileOpened)
}
}
Message::FileOpened(result) => {
self.is_loading = false;
self.is_dirty = false;
if let Ok((path, contents)) = result {
self.file = Some(path);
self.content = text_editor::Content::with_text(&contents);
}
Task::none()
}
Message::SaveFile => {
if self.is_loading {
Task::none()
} else {
self.is_loading = true;
Task::perform(
save_file(self.file.clone(), self.content.text()),
Message::FileSaved,
)
}
}
Message::FileSaved(result) => {
self.is_loading = false;
if let Ok(path) = result {
self.file = Some(path);
self.is_dirty = false;
}
Task::none()
}
}
}
fn view(&self) -> Element<Message> {
let controls = row![
action(new_icon(), "New file", Some(Message::NewFile)),
action(
open_icon(),
"Open file",
(!self.is_loading).then_some(Message::OpenFile)
),
action(
save_icon(),
"Save file",
self.is_dirty.then_some(Message::SaveFile)
),
horizontal_space(),
toggler(self.word_wrap)
.label("Word Wrap")
.on_toggle(Message::WordWrapToggled),
pick_list(
highlighter::Theme::ALL,
Some(self.theme),
Message::ThemeSelected
)
.text_size(14)
.padding([5, 10])
]
.spacing(10)
.align_y(Center);
let status = row![
text(if let Some(path) = &self.file {
let path = path.display().to_string();
if path.len() > 60 {
format!("...{}", &path[path.len() - 40..])
} else {
path
}
} else {
String::from("New file")
}),
horizontal_space(),
text({
let (line, column) = self.content.cursor_position();
format!("{}:{}", line + 1, column + 1)
})
]
.spacing(10);
column![
controls,
text_editor(&self.content)
.height(Fill)
.on_action(Message::ActionPerformed)
.wrapping(if self.word_wrap {
text::Wrapping::Word
} else {
text::Wrapping::None
})
.highlight(
self.file
.as_deref()
.and_then(Path::extension)
.and_then(ffi::OsStr::to_str)
.unwrap_or("rs"),
self.theme,
)
.key_binding(|key_press| {
match key_press.key.as_ref() {
keyboard::Key::Character("s") if key_press.modifiers.command() => {
Some(text_editor::Binding::Custom(Message::SaveFile))
}
_ => text_editor::Binding::from_key_press(key_press),
}
}),
status,
]
.spacing(10)
.padding(10)
.into()
}
fn theme(&self) -> Theme {
if self.theme.is_dark() {
Theme::Dark
} else {
Theme::Light
}
}
}
#[derive(Debug, Clone)]
pub enum Error {
DialogClosed,
IoError(io::ErrorKind),
}
async fn open_file() -> Result<(PathBuf, Arc<String>), Error> {
let picked_file = rfd::AsyncFileDialog::new()
.set_title("Open a text file...")
.pick_file()
.await
.ok_or(Error::DialogClosed)?;
load_file(picked_file).await
}
async fn load_file(path: impl Into<PathBuf>) -> Result<(PathBuf, Arc<String>), Error> {
let path = path.into();
let contents = tokio::fs::read_to_string(&path)
.await
.map(Arc::new)
.map_err(|error| Error::IoError(error.kind()))?;
Ok((path, contents))
}
async fn save_file(path: Option<PathBuf>, contents: String) -> Result<PathBuf, Error> {
let path = if let Some(path) = path {
path
} else {
rfd::AsyncFileDialog::new()
.save_file()
.await
.as_ref()
.map(rfd::FileHandle::path)
.map(Path::to_owned)
.ok_or(Error::DialogClosed)?
};
tokio::fs::write(&path, contents)
.await
.map_err(|error| Error::IoError(error.kind()))?;
Ok(path)
}
fn action<'a, Message: Clone + 'a>(
content: impl Into<Element<'a, Message>>,
label: &'a str,
on_press: Option<Message>,
) -> Element<'a, Message> {
let action = button(container(content).center_x(30));
if let Some(on_press) = on_press {
tooltip(
action.on_press(on_press),
label,
tooltip::Position::FollowCursor,
)
.style(container::rounded_box)
.into()
} else {
action.style(button::secondary).into()
}
}
fn new_icon<'a, Message>() -> Element<'a, Message> {
icon('\u{0e800}')
}
fn save_icon<'a, Message>() -> Element<'a, Message> {
icon('\u{0e801}')
}
fn open_icon<'a, Message>() -> Element<'a, Message> {
icon('\u{0f115}')
}
fn icon<'a, Message>(codepoint: char) -> Element<'a, Message> {
const ICON_FONT: Font = Font::with_name("editor-icons");
text(codepoint).font(ICON_FONT).into()
}