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

[Refactor] refactor use codesnap library #137

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Next Next commit
[Refactor] refactor use codesnap library
mistricky committed Nov 29, 2024
commit 191c1adc9bd59d913881a683edabacf8279a3274
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -9,3 +9,9 @@ rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=-crt-static"]

[target.aarch64-unknown-linux-musl]
rustflags = ["-C", "target-feature=-crt-static"]
620 changes: 385 additions & 235 deletions generator/Cargo.lock

Large diffs are not rendered by default.

18 changes: 6 additions & 12 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -3,17 +3,11 @@ name = "generator"
version = "0.1.0"
edition = "2021"

[dependencies]
nvim-oxi = { features = ["neovim-0-9", "libuv"], version = "0.5.1" }
tiny-skia = "0.11.4"
syntect = "5.2.0"
cosmic-text = "0.12.0"
serde = "1.0.204"
arboard = { features = ["wayland-data-control"], version = "3.4.0" }
thiserror = "1.0.63"
regex = "1.10.5"
two-face = "0.4.0"
cached = "0.53.1"

[lib]
crate-type = ["cdylib"]

[dependencies]
codesnap = "0.7.1"
mlua = { version = "0.10.0", features = ["module", "luajit", "serialize"] }
serde = { version = "1.0.204", features = ["derive"] }

84 changes: 0 additions & 84 deletions generator/src/code.rs

This file was deleted.

44 changes: 0 additions & 44 deletions generator/src/color.rs

This file was deleted.

10 changes: 0 additions & 10 deletions generator/src/components.rs

This file was deleted.

147 changes: 0 additions & 147 deletions generator/src/components/background.rs

This file was deleted.

90 changes: 0 additions & 90 deletions generator/src/components/breadcrumbs.rs

This file was deleted.

17 changes: 0 additions & 17 deletions generator/src/components/code_block.rs

This file was deleted.

42 changes: 0 additions & 42 deletions generator/src/components/container.rs

This file was deleted.

2 changes: 0 additions & 2 deletions generator/src/components/editor.rs

This file was deleted.

75 changes: 0 additions & 75 deletions generator/src/components/editor/code.rs

This file was deleted.

109 changes: 0 additions & 109 deletions generator/src/components/editor/mac_title_bar.rs

This file was deleted.

76 changes: 0 additions & 76 deletions generator/src/components/highlight_code_block.rs

This file was deleted.

3 changes: 0 additions & 3 deletions generator/src/components/interface.rs

This file was deleted.

204 changes: 0 additions & 204 deletions generator/src/components/interface/component.rs

This file was deleted.

27 changes: 0 additions & 27 deletions generator/src/components/interface/render_error.rs

This file was deleted.

80 changes: 0 additions & 80 deletions generator/src/components/interface/style.rs

This file was deleted.

100 changes: 0 additions & 100 deletions generator/src/components/line_number.rs

This file was deleted.

106 changes: 0 additions & 106 deletions generator/src/components/rect.rs

This file was deleted.

82 changes: 0 additions & 82 deletions generator/src/components/watermark.rs

This file was deleted.

63 changes: 0 additions & 63 deletions generator/src/config.rs

This file was deleted.

46 changes: 0 additions & 46 deletions generator/src/copy.rs

This file was deleted.

80 changes: 0 additions & 80 deletions generator/src/copy_ascii.rs

This file was deleted.

3 changes: 0 additions & 3 deletions generator/src/edges.rs

This file was deleted.

5 changes: 0 additions & 5 deletions generator/src/edges/edge.rs

This file was deleted.

31 changes: 0 additions & 31 deletions generator/src/edges/margin.rs

This file was deleted.

30 changes: 0 additions & 30 deletions generator/src/edges/padding.rs

This file was deleted.

107 changes: 0 additions & 107 deletions generator/src/highlight.rs

This file was deleted.

90 changes: 61 additions & 29 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,62 @@
mod code;
mod color;
mod components;
mod config;
mod copy;
mod copy_ascii;
mod edges;
mod highlight;
mod path;
mod save;
mod snapshot;
mod text;

use config::TakeSnapshotParams;
use copy::copy_into_clipboard;
use copy_ascii::copy_ascii;
use nvim_oxi::{api, Dictionary, Function};
use save::save_snapshot;

#[nvim_oxi::plugin]
fn generator() -> nvim_oxi::Result<Dictionary> {
let copy_into_clipboard: Function<TakeSnapshotParams, Result<(), api::Error>> =
Function::from_fn(copy_into_clipboard);

Ok(Dictionary::from_iter([
("copy_into_clipboard", copy_into_clipboard),
("save_snapshot", Function::from_fn(save_snapshot)),
("copy_ascii", Function::from_fn(copy_ascii)),
]))
mod snapshot_config;

use codesnap::snapshot::{image_snapshot::ImageSnapshot, snapshot_data::SnapshotData};
use mlua::prelude::*;
use snapshot_config::SnapshotConfigLua;

enum SnapshotType {
Png,
Svg,
Html,
}

impl From<String> for SnapshotType {
fn from(value: String) -> Self {
match value.as_str() {
"png" => SnapshotType::Png,
"svg" => SnapshotType::Svg,
"html" => SnapshotType::Html,
_ => SnapshotType::Png,
}
}
}

impl SnapshotType {
fn snapshot_data(&self, image_snapshot: ImageSnapshot) -> LuaResult<SnapshotData> {
let data = match self {
SnapshotType::Png => image_snapshot.png_data(),
SnapshotType::Svg => todo!(),
SnapshotType::Html => todo!(),
}
.map_err(|_| mlua::Error::RuntimeError("Failed to generate snapshot data".to_string()))?;

Ok(data)
}
}

fn save(
_: &Lua,
(snapshot_type, path, config): (String, String, SnapshotConfigLua),
) -> LuaResult<()> {
let image_snapshot = config
.0
.create_snapshot()
.map_err(|_| mlua::Error::RuntimeError("Failed to create snapshot".to_string()))?;
let snapshot_type: SnapshotType = snapshot_type.into();

snapshot_type
.snapshot_data(image_snapshot)?
.save(&path)
.map_err(|_| mlua::Error::RuntimeError(format!("Failed to save snapshot data to {}", path)))
}

fn copy_to_clipboard(_: &Lua, snapshot_type: SnapshotType) {}

#[mlua::lua_module(skip_memory_check)]
fn codesnap_generator(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;

exports.set("save", lua.create_function(save)?)?;

Ok(exports)
}
10 changes: 0 additions & 10 deletions generator/src/path.rs

This file was deleted.

27 changes: 0 additions & 27 deletions generator/src/save.rs

This file was deleted.

78 changes: 0 additions & 78 deletions generator/src/snapshot.rs

This file was deleted.

12 changes: 12 additions & 0 deletions generator/src/snapshot_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use codesnap::config::SnapshotConfig;
use mlua::{FromLua, LuaSerdeExt};

pub struct SnapshotConfigLua(pub SnapshotConfig);

impl FromLua for SnapshotConfigLua {
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
let config: SnapshotConfig = lua.from_value::<SnapshotConfig>(value)?;

Ok(SnapshotConfigLua(config))
}
}
121 changes: 0 additions & 121 deletions generator/src/text.rs

This file was deleted.