From 09773dd15b993aa3cdbbb65e4119112b88e8de57 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Thu, 5 Nov 2020 16:55:30 +1100 Subject: [PATCH] Re-introduce history command --- .gitignore | 4 +++- swap/src/cli.rs | 1 + swap/src/main.rs | 21 ++++++++++++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index d89bb47f..40a04136 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ - # Created by https://www.toptal.com/developers/gitignore/api/rust,clion+all,emacs # Edit at https://www.toptal.com/developers/gitignore?templates=rust,clion+all,emacs @@ -154,4 +153,7 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk +# sled DB directory generated during local development +.swap-db/ + # End of https://www.toptal.com/developers/gitignore/api/rust,clion+all,emacs diff --git a/swap/src/cli.rs b/swap/src/cli.rs index aff4d67c..4627afab 100644 --- a/swap/src/cli.rs +++ b/swap/src/cli.rs @@ -33,4 +33,5 @@ pub enum Options { #[structopt(long = "tor")] tor: bool, }, + History, } diff --git a/swap/src/main.rs b/swap/src/main.rs index f5c34f89..77b6a11f 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -16,6 +16,7 @@ use anyhow::Result; use futures::{channel::mpsc, StreamExt}; use libp2p::Multiaddr; use log::LevelFilter; +use prettytable::{row, Table}; use std::{io, io::Write, process, sync::Arc}; use structopt::StructOpt; use swap::{ @@ -27,9 +28,11 @@ use swap::{ network::transport::{build, build_tor, SwapTransport}, Cmd, Rsp, SwapAmounts, }; -use tempfile::tempdir; use tracing::info; +#[macro_use] +extern crate prettytable; + mod cli; mod trace; @@ -44,8 +47,8 @@ async fn main() -> Result<()> { trace::init_tracing(LevelFilter::Debug)?; - let db_dir = tempdir()?; - let db = Database::open(db_dir.path()).unwrap(); + // This currently creates the directory if it's not there in the first place + let db = Database::open(std::path::Path::new("./.swap-db/")).unwrap(); match opt { Options::Alice { @@ -130,6 +133,18 @@ async fn main() -> Result<()> { ) .await?; } + Options::History => { + let mut table = Table::new(); + + table.add_row(row!["SWAP ID", "STATE"]); + + for (swap_id, state) in db.all()? { + table.add_row(row![swap_id, state]); + } + + // Print the table to stdout + table.printstd(); + } } Ok(())