Embed mruby and better clipboard support
This commit is contained in:
@@ -115,8 +115,8 @@ LineIterator *begin_l_iter(Knot *root, uint32_t start_line);
|
||||
// Each subsequent call returns the next line as a null terminated string
|
||||
// `it` is the iterator returned from begin_l_iter
|
||||
// After getting the necessary lines free the iterator (no need to go upto
|
||||
// the end) returns null if there are no more lines All return strings
|
||||
// `must` be freed by the caller
|
||||
// the end) returns null if there are no more lines
|
||||
// The string must not be freed
|
||||
char *next_line(LineIterator *it, uint32_t *out_len);
|
||||
|
||||
// Returns the previous line as a null terminated string
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#define PCRE_WORKSPACE_SIZE 512
|
||||
|
||||
#include <mruby.h>
|
||||
#include <mruby/array.h>
|
||||
#include <mruby/compile.h>
|
||||
#include <mruby/hash.h>
|
||||
#include <mruby/irep.h>
|
||||
#include <mruby/string.h>
|
||||
#include "mruby.h"
|
||||
#include "mruby/array.h"
|
||||
#include "mruby/compile.h"
|
||||
#include "mruby/hash.h"
|
||||
#include "mruby/irep.h"
|
||||
#include "mruby/string.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <pcre2.h>
|
||||
extern "C" {
|
||||
|
||||
@@ -29,6 +29,8 @@ struct BarLine {
|
||||
void setup_ruby_bindings(mrb_state *mrb, RClass *C_module);
|
||||
void ruby_start();
|
||||
void ruby_shutdown();
|
||||
void ruby_copy(const char *text, size_t len);
|
||||
std::string ruby_paste();
|
||||
void load_theme();
|
||||
void load_languages_info();
|
||||
uint8_t read_line_endings();
|
||||
|
||||
@@ -1,3 +1,65 @@
|
||||
module Clipboard
|
||||
@clip = ""
|
||||
@os = :os_name_placed_here
|
||||
|
||||
class << self
|
||||
def command_exists?(cmd)
|
||||
system("command -v #{cmd} > /dev/null 2>&1")
|
||||
end
|
||||
def copy(text)
|
||||
if @os == :windows
|
||||
IO.popen("clip", "w") { |f| f.write(text) }
|
||||
elsif @os == :mac
|
||||
IO.popen("pbcopy", "w") { |f| f.write(text) }
|
||||
elsif @os == :linux
|
||||
if ENV["XDG_SESSION_TYPE"]&.downcase == "wayland" || ENV["WAYLAND_DISPLAY"]
|
||||
if command_exists?("wl-copy")
|
||||
IO.popen("wl-copy", "w") { |f| f.write(text) }
|
||||
else
|
||||
osc52_copy(text)
|
||||
end
|
||||
elsif ENV["XDG_SESSION_TYPE"]&.downcase == "x11" || ENV["DISPLAY"]
|
||||
if command_exists?("xsel")
|
||||
IO.popen("xsel --clipboard --input", "w") { |f| f.write(text) }
|
||||
elsif command_exists?("xclip")
|
||||
IO.popen("xclip -selection clipboard", "w") { |f| f.write(text) }
|
||||
else
|
||||
osc52_copy(text)
|
||||
end
|
||||
end
|
||||
end
|
||||
@clip = text
|
||||
end
|
||||
def paste
|
||||
if @os == :windows
|
||||
return `powershell -NoProfile -Command Get-Clipboard`
|
||||
elsif @os == :mac
|
||||
return `pbpaste`
|
||||
elsif @os == :linux
|
||||
if ENV["XDG_SESSION_TYPE"]&.downcase == "wayland" || ENV["WAYLAND_DISPLAY"]
|
||||
if command_exists?("wl-copy")
|
||||
return `wl-paste`
|
||||
end
|
||||
elsif ENV["XDG_SESSION_TYPE"]&.downcase == "x11" || ENV["DISPLAY"]
|
||||
if command_exists?("xsel")
|
||||
return `xsel --clipboard --output`
|
||||
elsif command_exists?("xclip")
|
||||
return `xclip -selection clipboard -o`
|
||||
else
|
||||
return @clip
|
||||
end
|
||||
end
|
||||
end
|
||||
return ""
|
||||
end
|
||||
def osc52_copy(text)
|
||||
encoded = [text].pack("m0")
|
||||
print "\e]52;c;#{encoded}\a"
|
||||
text
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module C
|
||||
@lsp_config = {
|
||||
"clangd" => [
|
||||
@@ -262,7 +324,7 @@ module C
|
||||
@highlighters = {}
|
||||
@b_startup = nil
|
||||
@b_shutdown = nil
|
||||
@b_bar = lambda do |info|
|
||||
@b_bar = proc do |info|
|
||||
# mode, lang_name, warnings, lsp_name, filename, foldername, line, max_line, width
|
||||
# puts info.inspect
|
||||
mode_color = 0x82AAFF
|
||||
@@ -294,20 +356,27 @@ module C
|
||||
highlights << { fg: lang_info[:color], bg: 0x24272d, start: 13, length: 2 }
|
||||
highlights << { fg: 0xced4df, bg: 0x24272d, start: 15, length: filename.length }
|
||||
highlights << { fg: 0x24272d, bg: 0x000000, start: 15 + filename.length, length: 1 }
|
||||
return {
|
||||
next {
|
||||
text: starting,
|
||||
highlights: highlights
|
||||
}
|
||||
end
|
||||
@b_copy = proc do |text|
|
||||
Clipboard.copy(text)
|
||||
end
|
||||
@b_paste = proc do
|
||||
next Clipboard.paste
|
||||
end
|
||||
|
||||
class << self
|
||||
attr_accessor :theme, :lsp_config, :languages,
|
||||
:line_endings, :highlighters
|
||||
attr_reader :b_startup, :b_shutdown, :b_extra_highlights, :b_bar
|
||||
attr_reader :b_startup, :b_shutdown, :b_extra_highlights,
|
||||
:b_bar, :b_copy, :b_paste
|
||||
|
||||
# def bar=(block)
|
||||
# @b_bar = block
|
||||
# end
|
||||
def bar=(&block)
|
||||
@b_bar = block
|
||||
end
|
||||
|
||||
def startup(&block)
|
||||
@b_startup = block
|
||||
@@ -317,6 +386,14 @@ module C
|
||||
@b_shutdown = block
|
||||
end
|
||||
|
||||
def copy(&block)
|
||||
@b_copy = block
|
||||
end
|
||||
|
||||
def paste(&block)
|
||||
@b_paste = block
|
||||
end
|
||||
|
||||
def extra_highlights(&block)
|
||||
@b_extra_highlights = block
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,17 +5,12 @@
|
||||
|
||||
struct LineTree {
|
||||
void clear() {
|
||||
std::unique_lock lock(mtx);
|
||||
clear_node(root);
|
||||
root = nullptr;
|
||||
stack_size = 0;
|
||||
}
|
||||
void build(uint32_t x) {
|
||||
std::unique_lock lock(mtx);
|
||||
root = build_node(x);
|
||||
}
|
||||
void build(uint32_t x) { root = build_node(x); }
|
||||
LineData *at(uint32_t x) {
|
||||
std::shared_lock lock(mtx);
|
||||
LineNode *n = root;
|
||||
while (n) {
|
||||
uint32_t left_size = n->left ? n->left->size : 0;
|
||||
@@ -31,7 +26,6 @@ struct LineTree {
|
||||
return nullptr;
|
||||
}
|
||||
LineData *start_iter(uint32_t x) {
|
||||
std::shared_lock lock(mtx);
|
||||
stack_size = 0;
|
||||
LineNode *n = root;
|
||||
while (n) {
|
||||
@@ -52,7 +46,6 @@ struct LineTree {
|
||||
}
|
||||
void end_iter() { stack_size = 0; }
|
||||
LineData *next() {
|
||||
std::shared_lock lock(mtx);
|
||||
while (stack_size) {
|
||||
auto &f = stack[stack_size - 1];
|
||||
LineNode *n = f.node;
|
||||
@@ -73,21 +66,16 @@ struct LineTree {
|
||||
return nullptr;
|
||||
}
|
||||
void insert(uint32_t x, uint32_t y) {
|
||||
std::unique_lock lock(mtx);
|
||||
if (x > subtree_size(root))
|
||||
x = subtree_size(root);
|
||||
root = insert_node(root, x, y);
|
||||
}
|
||||
void erase(uint32_t x, uint32_t y) {
|
||||
std::unique_lock lock(mtx);
|
||||
if (x + y > subtree_size(root))
|
||||
x = subtree_size(root) - y;
|
||||
root = erase_node(root, x, y);
|
||||
}
|
||||
uint32_t count() {
|
||||
std::shared_lock lock(mtx);
|
||||
return subtree_size(root);
|
||||
}
|
||||
uint32_t count() { return subtree_size(root); }
|
||||
~LineTree() { clear(); }
|
||||
|
||||
private:
|
||||
@@ -117,7 +105,6 @@ private:
|
||||
LineNode *root = nullptr;
|
||||
Frame stack[32];
|
||||
std::atomic<uint8_t> stack_size = 0;
|
||||
std::shared_mutex mtx;
|
||||
static constexpr uint32_t LEAF_TARGET = 256;
|
||||
LineTree::LineNode *erase_node(LineNode *n, uint32_t x, uint32_t y) {
|
||||
if (!n || y == 0)
|
||||
|
||||
@@ -17,10 +17,8 @@ struct Parser {
|
||||
mrb_value parser_block = mrb_nil_value();
|
||||
mrb_value match_block = mrb_nil_value();
|
||||
bool is_custom{false};
|
||||
std::atomic<uint32_t> scroll_max{UINT32_MAX - 2048};
|
||||
std::atomic<uint32_t> scroll_max{0};
|
||||
std::atomic<bool> scroll_dirty{false};
|
||||
std::mutex mutex;
|
||||
std::mutex data_mutex;
|
||||
LineTree line_tree;
|
||||
UniqueQueue<uint32_t> dirty_lines;
|
||||
|
||||
|
||||
@@ -155,9 +155,6 @@ std::string get_exe_dir();
|
||||
char *load_file(const char *path, uint32_t *out_len, bool *out_eol);
|
||||
Language language_for_file(const char *filename);
|
||||
|
||||
void copy_to_clipboard(const char *text, size_t len);
|
||||
char *get_from_clipboard(uint32_t *out_len);
|
||||
|
||||
template <typename T>
|
||||
inline T *safe_get(std::map<uint16_t, T> &m, uint16_t key) {
|
||||
auto it = m.find(key);
|
||||
|
||||
Reference in New Issue
Block a user