Enable line wrapping based on terminal size

This commit is contained in:
projectmoon 2024-01-12 22:31:02 +01:00
parent 8cdd20c2aa
commit 4583f6dac4
5 changed files with 77 additions and 10 deletions

25
Cargo.lock generated
View File

@ -47,6 +47,7 @@ dependencies = [
"arangors",
"async-recursion",
"async-trait",
"crossterm",
"eventsource-client",
"futures",
"itertools 0.12.0",
@ -60,6 +61,7 @@ dependencies = [
"serde_json",
"strum",
"syn 1.0.109",
"textwrap",
"thiserror",
"tokio",
"uuid",
@ -1732,6 +1734,12 @@ version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9"
[[package]]
name = "smawk"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
[[package]]
name = "socket2"
version = "0.4.10"
@ -1857,6 +1865,17 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "textwrap"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
dependencies = [
"smawk",
"unicode-linebreak",
"unicode-width",
]
[[package]]
name = "thiserror"
version = "1.0.53"
@ -2132,6 +2151,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-linebreak"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f"
[[package]]
name = "unicode-normalization"
version = "0.1.22"

View File

@ -24,6 +24,8 @@ uuid = {version = "1.6.1", features = [ "std", "v7", "fast-rng" ] }
polodb_core = "4.4.0"
arangors = "0.5.4"
itertools = "0.12.0"
crossterm = "0.27.0"
textwrap = "0.16.0"
[build-dependencies]
prettyplease = "0.1.25"

View File

@ -1,4 +1,5 @@
use crate::db::Database;
use crate::io::display;
use crate::models::commands::CommandExecution;
use crate::state::GameState;
use anyhow::Result;
@ -24,15 +25,15 @@ impl GameLoop {
async fn handle_execution(&mut self, execution: CommandExecution) -> Result<()> {
if !execution.valid {
println!(
display!(
"You can't do that: {}",
execution.reason.unwrap_or("for some reason...".to_string())
execution.reason.unwrap_or("for some reason...".to_string())
);
return Ok(());
}
println!("\n\n{}\n\n", execution.narration);
display!("\n\n{}\n\n", execution.narration);
for event in execution.events {
self.state.update(event).await?;
@ -75,20 +76,20 @@ impl GameLoop {
pub async fn run_loop(&mut self) -> Result<()> {
loop {
println!("{}", self.state.current_scene);
display!("{}", self.state.current_scene);
let sig = self.editor.read_line(&self.prompt);
match sig {
Ok(Signal::Success(buffer)) => {
println!("We processed: {}", buffer);
display!("We processed: {}", buffer);
self.handle_input(&buffer).await?;
}
Ok(Signal::CtrlD) | Ok(Signal::CtrlC) => {
println!("\nAborted!");
display!("\nAborted!");
break;
}
x => {
println!("Event: {:?}", x);
display!("Event: {:?}", x);
}
}
}

23
src/io.rs Normal file
View File

@ -0,0 +1,23 @@
#[inline]
pub(crate) fn display_text<S : AsRef<str>>(text: S) {
let text = text.as_ref();
let (columns, _) = crossterm::terminal::size().ok().unwrap_or((80, 25));
let columns: usize = columns.into();
let text = textwrap::wrap(text, columns);
text.into_iter().for_each(|line| {
println!("{}", line);
});
}
macro_rules! display {
($text:expr) => {
crate::io::display_text($text);
};
($fmt:expr, $text:expr) => {
crate::io::display_text(format!($fmt, $text));
};
}
pub(crate) use display;

View File

@ -7,17 +7,28 @@ use models::{
};
use reedline::{DefaultPrompt, Reedline, Signal};
use state::GameState;
use std::{rc::Rc, time::Duration};
use std::{io::stdout, rc::Rc, time::Duration};
use arangors::Connection;
use crossterm::{
event, execute,
style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
terminal::EnableLineWrap,
ExecutableCommand,
};
mod io;
mod ai;
mod commands;
mod db;
mod game_loop;
#[allow(dead_code)]
mod kobold_api;
mod models;
mod state;
mod game_loop;
use crate::{db::Database, models::world::scenes::StageOrStub};
use kobold_api::Client;
@ -57,6 +68,8 @@ async fn load_root_scene(db: &Database, state: &mut GameState) -> Result<()> {
#[tokio::main]
async fn main() -> Result<()> {
stdout().execute(EnableLineWrap)?;
let base_client = reqwest::ClientBuilder::new()
.connect_timeout(Duration::from_secs(180))
.pool_idle_timeout(Duration::from_secs(180))
@ -65,7 +78,10 @@ async fn main() -> Result<()> {
//let client = Client::new_with_client("http://127.0.0.1:5001/api", base_client);
let conn = Connection::establish_without_auth("http://localhost:8529").await?;
let client = Rc::new(Client::new_with_client("http://192.168.1.65:5001/api", base_client));
let client = Rc::new(Client::new_with_client(
"http://192.168.1.65:5001/api",
base_client,
));
let db = Rc::new(Database::new(conn, "test_world").await?);
let logic = ai::AiLogic::new(client, &db);