Skip to content

Commit

Permalink
fix: unable to build on android using termux (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Nov 7, 2023
1 parent d40f104 commit 534733b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ base64 = "0.21.0"
rustc-hash = "1.1.0"
bstr = "1.3.0"
nu-ansi-term = "0.47.0"
arboard = { version = "3.2.0", default-features = false }
async-trait = "0.1.74"
textwrap = "0.16.0"
ansi_colours = "1.2.2"
Expand All @@ -53,6 +52,9 @@ version = "5.0.0"
default-features = false
features = ["parsing", "regex-onig", "plist-load"]

[target.'cfg(not(any(target_os = "android", target_os = "emscripten")))'.dependencies]
arboard = { version = "3.2.0", default-features = false }

[profile.release]
lto = true
strip = true
Expand Down
17 changes: 3 additions & 14 deletions src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use self::validator::ReplValidator;
use crate::client::init_client;
use crate::config::GlobalConfig;
use crate::render::{render_error, render_stream};
use crate::utils::{create_abort_signal, AbortSignal};
use crate::utils::{create_abort_signal, set_text, AbortSignal};

use anyhow::{bail, Context, Result};
use arboard::Clipboard;
use crossbeam::sync::WaitGroup;
use fancy_regex::Regex;
use lazy_static::lazy_static;
Expand All @@ -24,7 +23,6 @@ use reedline::{
ColumnarMenu, EditMode, Emacs, KeyCode, KeyModifiers, Keybindings, Reedline, ReedlineEvent,
ReedlineMenu, Vi,
};
use std::cell::RefCell;
use std::io::Read;

const MENU_NAME: &str = "completion_menu";
Expand Down Expand Up @@ -56,7 +54,6 @@ pub struct Repl {
editor: Reedline,
prompt: ReplPrompt,
abort: AbortSignal,
clipboard: std::result::Result<RefCell<Clipboard>, arboard::Error>,
}

impl Repl {
Expand All @@ -67,13 +64,10 @@ impl Repl {

let abort = create_abort_signal();

let clipboard = Clipboard::new().map(RefCell::new);

Ok(Self {
config: config.clone(),
editor,
prompt,
clipboard,
abort,
})
}
Expand Down Expand Up @@ -315,13 +309,8 @@ Type ".help" for more information.
if text.is_empty() {
bail!("No text")
}
match self.clipboard.as_ref() {
Err(err) => bail!("{}", err),
Ok(clip) => {
clip.borrow_mut().set_text(text)?;
Ok(())
}
}
set_text(text)?;
Ok(())
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/utils/clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[cfg(not(any(target_os = "android", target_os = "emscripten")))]
lazy_static::lazy_static! {
static ref CLIPBOARD: std::sync::Arc<std::sync::Mutex<Option<arboard::Clipboard>>> =
std::sync::Arc::new(std::sync::Mutex::new(arboard::Clipboard::new().ok()));
}

#[cfg(not(any(target_os = "android", target_os = "emscripten")))]
pub fn set_text(text: &str) -> anyhow::Result<()> {
let mut clipboard = CLIPBOARD.lock().unwrap();
match clipboard.as_mut() {
Some(clipboard) => clipboard.set_text(text)?,
None => anyhow::bail!("No available clipboard"),
}
Ok(())
}

#[cfg(any(target_os = "android", target_os = "emscripten"))]
pub fn set_text(_text: &str) -> anyhow::Result<()> {
anyhow::bail!("No available clipboard")
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod abort_signal;
mod clipboard;
mod prompt_input;
mod split_line;
mod tiktoken;

pub use self::abort_signal::{create_abort_signal, AbortSignal};
pub use self::clipboard::set_text;
pub use self::prompt_input::*;
pub use self::split_line::*;
pub use self::tiktoken::cl100k_base_singleton;
Expand Down

0 comments on commit 534733b

Please sign in to comment.