use std::fmt;
use std::cell::Cell;
use crate::order_book::OrderBook;
use crate::tick::{TickUnit, Tick};
thread_local! {
static DISPLAY_LIMIT: Cell<usize> = Cell::new(5);
static DISPLAY_PRICE_TICK: Cell<Option<Tick>> = Cell::new(None);
static DISPLAY_SIZE_TICK: Cell<Option<Tick>> = Cell::new(None);
}
pub fn set_limit(limit: usize) {
DISPLAY_LIMIT.with(|dl| dl.set(limit));
}
pub fn set_price_tick(maybe_tick: Option<Tick>) {
DISPLAY_PRICE_TICK.with(|dt| dt.set(maybe_tick));
}
pub fn set_size_tick(maybe_tick: Option<Tick>) {
DISPLAY_SIZE_TICK.with(|dt| dt.set(maybe_tick));
}
pub fn displayable_price(ticked: TickUnit) -> String {
match DISPLAY_PRICE_TICK.with(|dt| dt.get()) {
Some(tick) => tick.unticked(ticked).unwrap(),
None => format!("{}", ticked),
}
}
pub fn displayable_size(ticked: TickUnit) -> String {
match DISPLAY_SIZE_TICK.with(|dt| dt.get()) {
Some(tick) => tick.unticked(ticked).unwrap(),
None => format!("{}", ticked),
}
}
impl fmt::Display for OrderBook {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let display_limit = DISPLAY_LIMIT.with(|dl| dl.get());
writeln!(f, "## ASK")?;
let ask: Vec<_> = self.ask()
.take(display_limit)
.collect();
for (&price, &size) in ask.iter().rev() {
writeln!(f, "{}:\t{}", displayable_price(price), displayable_size(size))?;
}
write!(f, "\n\n")?;
for (&price, &size) in self.bid()
.take(display_limit)
{
writeln!(f, "{}:\t{}", displayable_price(price), displayable_size(size))?;
}
writeln!(f, "## BID")?;
Ok(())
}
}