Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

issue: TUI #172

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,508 changes: 1,019 additions & 489 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"anchor",
"account",
"terminal",
"terminal-tui",
"common",
"checkout",
"cli",
Expand Down
10 changes: 9 additions & 1 deletion issue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ description = "Manage radicle issues"
lexopt = { version = "0.2" }
anyhow = { version = "1.0" }
librad = { version = "0" }
radicle-terminal = { path = "../terminal" }
radicle-common = { path = "../common" }
radicle-terminal = { path = "../terminal" }
radicle-terminal-tui = { path = "../terminal-tui", optional = true }
timeago = { version = "0.3.1" }
tuirealm = { version = "1.7.0", optional = true }
tui-realm-stdlib = { version = "1.1.6", optional = true }
serde_json = { version = "1.0" }
serde_yaml = { version = "0.8" }
serde = { version = "1.0" }

[features]
tui = ["radicle-terminal-tui", "tuirealm", "tui-realm-stdlib"]
123 changes: 86 additions & 37 deletions issue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ use std::str::FromStr;

use anyhow::{anyhow, Context};

use librad::git::storage::ReadOnly;
use librad::git::Urn;

use radicle_common::args::{Args, Error, Help};
use radicle_common::cobs::issue::*;
// use radicle_common::cobs::shared::*;
use radicle_common::{cobs, keys, project};
use radicle_terminal as term;

#[cfg(feature = "tui")]
mod tui;

pub const HELP: Help = Help {
name: "issue",
description: env!("CARGO_PKG_DESCRIPTION"),
Expand All @@ -21,6 +28,7 @@ Usage
rad issue delete <id>
rad issue react <id> [--emoji <char>]
rad issue list
rad issue interactive

Options

Expand All @@ -41,6 +49,7 @@ pub enum OperationName {
React,
Delete,
List,
Interactive,
}

impl Default for OperationName {
Expand All @@ -67,6 +76,7 @@ pub enum Operation {
reaction: cobs::Reaction,
},
List,
Interactive,
}

/// Tool options.
Expand Down Expand Up @@ -125,6 +135,7 @@ impl Args for Options {
"d" | "delete" => op = Some(OperationName::Delete),
"l" | "list" => op = Some(OperationName::List),
"r" | "react" => op = Some(OperationName::React),
"i" | "interactive" => op = Some(OperationName::Interactive),

unknown => anyhow::bail!("unknown operation '{}'", unknown),
},
Expand Down Expand Up @@ -158,6 +169,7 @@ impl Args for Options {
id: id.ok_or_else(|| anyhow!("an issue id to remove must be provided"))?,
},
OperationName::List => Operation::List,
OperationName::Interactive => Operation::Interactive,
};

Ok((Options { op }, vec![]))
Expand Down Expand Up @@ -189,43 +201,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
}
}
Operation::Create { title, description } => {
let meta = Metadata {
title: title.unwrap_or("Enter a title".to_owned()),
labels: vec![],
};
let yaml = serde_yaml::to_string(&meta)?;
let doc = format!(
"{}---\n\n{}",
yaml,
description.unwrap_or("Enter a description...".to_owned())
);

if let Some(text) = term::Editor::new().edit(&doc)? {
let mut meta = String::new();
let mut frontmatter = false;
let mut lines = text.lines();

while let Some(line) = lines.by_ref().next() {
if line.trim() == "---" {
if frontmatter {
break;
} else {
frontmatter = true;
continue;
}
}
if frontmatter {
meta.push_str(line);
meta.push('\n');
}
}

let description: String = lines.collect::<Vec<&str>>().join("\n");
let meta: Metadata =
serde_yaml::from_str(&meta).context("failed to parse yaml front-matter")?;

issues.create(&project, &meta.title, description.trim(), &meta.labels)?;
}
create(&project, &issues, title, description)?;
}
Operation::List => {
for (id, issue) in issues.all(&project)? {
Expand All @@ -235,7 +211,80 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
Operation::Delete { id } => {
issues.remove(&project, &id)?;
}
Operation::Interactive => {
tui(&storage, &project, &issues)?;
}
}

Ok(())
}

fn create(
project: &Urn,
store: &IssueStore,
title: Option<String>,
description: Option<String>,
) -> anyhow::Result<()> {
let meta = Metadata {
title: title.unwrap_or("Enter a title".to_owned()),
labels: vec![],
};
let yaml = serde_yaml::to_string(&meta)?;
let doc = format!(
"{}---\n\n{}",
yaml,
description.unwrap_or("Enter a description...".to_owned())
);

if let Some(text) = term::Editor::new().edit(&doc)? {
let mut meta = String::new();
let mut frontmatter = false;
let mut lines = text.lines();

while let Some(line) = lines.by_ref().next() {
if line.trim() == "---" {
if frontmatter {
break;
} else {
frontmatter = true;
continue;
}
}
if frontmatter {
meta.push_str(line);
meta.push('\n');
}
}
let description: String = lines.collect::<Vec<&str>>().join("\n");
let meta: Metadata =
serde_yaml::from_str(&meta).context("failed to parse yaml front-matter")?;

store.create(project, &meta.title, description.trim(), &meta.labels)?;
}
Ok(())
}

#[cfg(feature = "tui")]
fn tui<S: AsRef<ReadOnly>>(storage: &S, project: &Urn, store: &IssueStore) -> anyhow::Result<()> {
use radicle_terminal_tui::Window;
use tui::app;

if let Some(metadata) = project::get(&storage, &project)? {
let mut window = Window::default();
window.run(&mut app::IssueTui::new(&storage, &metadata, &store))?;
} else {
anyhow::bail!("Could not load project metadata.");
}

Ok(())
}

#[cfg(not(feature = "tui"))]
fn tui<S: AsRef<ReadOnly>>(
_storage: &S,
_project: &Urn,
_store: &IssueStore,
) -> anyhow::Result<()> {
term::warning("Could not run tui. Please activate feature 'tui'.");
Ok(())
}
3 changes: 3 additions & 0 deletions issue/src/tui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod app;
pub mod components;
pub mod issue;
Loading