Merge pull request #9 from house-of-vanity/code

Code
This commit is contained in:
House of Vanity
2021-01-10 21:39:38 +03:00
committed by GitHub
4 changed files with 36 additions and 17 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "desubot"
version = "0.5.0"
version = "0.5.1"
authors = ["AB <ab@hexor.ru>"]
edition = "2018"
@ -30,7 +30,8 @@ subprocess = "0.2.6"
serde_json = "1.0"
markov = "1.1.0"
rand = "0.7.3"
mystem = "0.2.1"
mystem = "^0.2"
#mystem = { path = "../mystem-rs" }
async-trait = "0.1.42"
sqlparser = "0.7.0"

View File

@ -1,10 +1,10 @@
static CODE: &str = "<b>Code highlighter</b>
#[allow(dead_code)]
static CODE_HELP: &str = "<b>Code highlighter</b>
<i>Usage</i>
<pre>/CODE
#&lt;theme - Dracula by default&gt;
&lt;CODE&gt;
#&lt;lang - JS by default&gt;</pre>
#&lt;lang - JS by default&gt; #&lt;theme - Dracula by default&gt;</pre>
Language may be defined by both name and extension - Rust, rs...
Max lines - 80
@ -33,4 +33,11 @@ gruvbox
gruvbox-light
gruvbox-white
zenburn
";
";
#[allow(dead_code)]
pub static SQL_HELP: &str = "<b>Perform an SQL command</b>
<i>* Only one sentence per message.
* Only SELECT command.
* Max result length is 100 lines. Use LIMIT 100.
* SQLITE syntax is available only.</i>";

View File

@ -17,11 +17,13 @@ use sqlparser::ast::Statement;
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;
use syntect::easy::HighlightLines;
use syntect::highlighting::Theme;
use syntect::parsing::SyntaxReference;
use syntect::util::LinesWithEndings;
use telegram_bot::prelude::*;
use telegram_bot::{Api, Message, ParseMode};
use syntect::highlighting::Theme;
include!("../assets/help_text.rs");
pub struct Here {
pub data: String,
@ -65,6 +67,10 @@ impl Execute for Sql {
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
let mut sql = self.data.clone();
debug!("PIZDA - {}", sql);
if sql == "/sql" || sql == "/sql-" {
return Ok(SQL_HELP.to_string())
}
let is_head = if sql.starts_with('-') {
sql = sql.replacen("-", "", 1);
false
@ -551,11 +557,16 @@ impl Execute for Code {
unimplemented!()
}
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
let mut lines: Vec<String> = self.data.trim().split("\n").map(|s| s.to_string()).collect();
let mut lines: Vec<String> = self
.data
.trim()
.split("\n")
.map(|s| s.to_string())
.collect();
if lines.len() >= 81 {
return Err(CodeHighlightningError);
}
let last_line = &lines[lines.len()-1];
let last_line = &lines[lines.len() - 1];
let tags = last_line
.trim()
@ -593,7 +604,6 @@ impl Execute for Code {
.map(|s| s.unwrap())
.collect();
let theme = if theme.len() != 1 {
ts.themes.get("Dracula").unwrap()
} else {
@ -606,6 +616,7 @@ impl Execute for Code {
.collect::<Vec<_>>();
let formatter = silicon::formatter::ImageFormatterBuilder::<String>::new()
.window_controls(false)
.line_offset(1)
.round_corner(false);
let mut formatter = formatter.build().unwrap();
let image = formatter.format(&highlight, &theme);

View File

@ -6,7 +6,6 @@ use crate::utils;
use mystem::MyStem;
use telegram_bot::*;
include!("../assets/help_text.rs");
pub async fn handler(
@ -25,19 +24,18 @@ pub async fn handler(
title,
&message.from.id,
&message.from.first_name,
{if data.len() <= 200 {data} else {&data[..200]}}.replace("\n", " ")
data.replace("\n", " ")
);
let cleaned_message = data.replace(&format!("@{}", me.clone().username.unwrap()), "");
match cleaned_message.as_str() {
s if s.to_string().starts_with("/code") => {
match {
Code {
data: s.replace("/code", ""),
}
.exec_with_result(&api, &message)
.await
.exec_with_result(&api, &message)
.await
} {
Ok(path) => {
let file = InputFileUpload::with_path(path.clone());
@ -50,7 +48,7 @@ pub async fn handler(
}
Err(_) => {
let _ = api
.send(message.text_reply(CODE).parse_mode(ParseMode::Html))
.send(message.text_reply(CODE_HELP).parse_mode(ParseMode::Html))
.await?;
}
}
@ -120,7 +118,9 @@ pub async fn handler(
.exec_mystem(&api, &message, mystem)
.await?
}
_ => db::add_sentence(&message, mystem).await?,
_ => {
db::add_sentence(&message, mystem).await?
}
}
}
MessageKind::Photo { ref caption, .. } => {