Compare commits
5 commits
00bd7a997a
...
b87970d4e2
Author | SHA1 | Date | |
---|---|---|---|
|
b87970d4e2 | ||
|
2ce5801925 | ||
|
ca50bdf3bb | ||
|
9eb6138852 | ||
|
88ecd68eb8 |
8 changed files with 70 additions and 48 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1488,7 +1488,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "mostr"
|
||||
version = "0.6.0"
|
||||
version = "0.6.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"chrono-english",
|
||||
|
|
|
@ -5,7 +5,7 @@ repository = "https://forge.ftt.gmbh/janek/mostr"
|
|||
readme = "README.md"
|
||||
license = "GPL 3.0"
|
||||
authors = ["melonion"]
|
||||
version = "0.6.0"
|
||||
version = "0.6.1"
|
||||
rust-version = "1.82"
|
||||
edition = "2021"
|
||||
default-run = "mostr"
|
||||
|
@ -35,4 +35,4 @@ nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "e82bc787bdd849
|
|||
[dev-dependencies]
|
||||
tokio = { version = "1.41", features = ["rt", "rt-multi-thread", "macros", "io-std"] }
|
||||
chrono-english = "0.1"
|
||||
linefeed = "0.6"
|
||||
linefeed = "0.6"
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
[toolchain]
|
||||
channel = "nightly-2024-11-09"
|
||||
channel = "1.82.0"
|
||||
|
|
|
@ -146,6 +146,11 @@ pub fn format_timestamp_local(stamp: &Timestamp) -> String {
|
|||
format_timestamp(stamp, "%y-%m-%d %a %H:%M")
|
||||
}
|
||||
|
||||
/// Format nostr timestamp with seconds precision.
|
||||
pub fn format_timestamp_full(stamp: &Timestamp) -> String {
|
||||
format_timestamp(stamp, "%y-%m-%d %a %H:%M:%S")
|
||||
}
|
||||
|
||||
pub fn format_timestamp_relative_to(stamp: &Timestamp, reference: &Timestamp) -> String {
|
||||
// Rough difference in days
|
||||
match (stamp.as_u64() as i64 - reference.as_u64() as i64) / 80_000 {
|
||||
|
|
15
src/kinds.rs
15
src/kinds.rs
|
@ -41,7 +41,7 @@ Task:
|
|||
- `hashtags` - list of hashtags set for the task
|
||||
- `tags` - values of all nostr tags associated with the event, except event tags
|
||||
- `desc` - last note on the task
|
||||
- `description` - accumulated notes on the task
|
||||
- `description` - all notes on the task
|
||||
- `time` - time tracked on this task by you
|
||||
Utilities:
|
||||
- `state` - indicator of current progress
|
||||
|
@ -78,7 +78,8 @@ where
|
|||
.tags(id.into_iter().map(Tag::event))
|
||||
}
|
||||
|
||||
pub fn join<'a, T>(tags: T) -> String
|
||||
/// Formats and joins the tags with commata
|
||||
pub fn join_tags<'a, T>(tags: T) -> String
|
||||
where
|
||||
T: IntoIterator<Item=&'a Tag>,
|
||||
{
|
||||
|
@ -131,12 +132,16 @@ pub fn to_hashtag(tag: &str) -> Tag {
|
|||
TagStandard::Hashtag(tag.to_string()).into()
|
||||
}
|
||||
|
||||
fn format_tag(tag: &Tag) -> String {
|
||||
pub fn format_tag(tag: &Tag) -> String {
|
||||
if let Some(et) = match_event_tag(tag) {
|
||||
return format!("{}: {:.8}",
|
||||
et.marker.as_ref().map(|m| m.to_string()).unwrap_or(MARKER_PARENT.to_string()),
|
||||
et.id);
|
||||
}
|
||||
format_tag_basic(tag)
|
||||
}
|
||||
|
||||
pub fn format_tag_basic(tag: &Tag) -> String {
|
||||
match tag.as_standardized() {
|
||||
Some(TagStandard::PublicKey {
|
||||
public_key,
|
||||
|
@ -149,12 +154,12 @@ fn format_tag(tag: &Tag) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_hashtag(tag: &Tag) -> bool {
|
||||
pub fn is_hashtag(tag: &Tag) -> bool {
|
||||
tag.single_letter_tag()
|
||||
.is_some_and(|letter| letter.character == Alphabet::T)
|
||||
}
|
||||
|
||||
pub(crate) fn to_prio_tag(value: Prio) -> Tag {
|
||||
pub fn to_prio_tag(value: Prio) -> Tag {
|
||||
Tag::custom(TagKind::Custom(Cow::from(PRIO)), [value.to_string()])
|
||||
}
|
||||
|
||||
|
|
37
src/main.rs
37
src/main.rs
|
@ -10,7 +10,7 @@ use std::time::Duration;
|
|||
|
||||
use crate::event_sender::MostrMessage;
|
||||
use crate::helpers::*;
|
||||
use crate::kinds::{join, match_event_tag, Prio, BASIC_KINDS, PROPERTY_COLUMNS, PROP_KINDS};
|
||||
use crate::kinds::{format_tag_basic, match_event_tag, Prio, BASIC_KINDS, PROPERTY_COLUMNS, PROP_KINDS};
|
||||
use crate::task::{State, Task, TaskState, MARKER_PROPERTY};
|
||||
use crate::tasks::{PropertyCollection, StateFilter, TasksRelay};
|
||||
use chrono::Local;
|
||||
|
@ -84,7 +84,7 @@ fn read_keys(readline: &mut DefaultEditor) -> Result<Keys> {
|
|||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
println!("Running Mostr Version {}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
|
||||
let mut args = args().skip(1).peekable();
|
||||
let mut builder = if args.peek().is_some_and(|arg| arg == "--debug") {
|
||||
args.next();
|
||||
|
@ -385,20 +385,29 @@ async fn main() -> Result<()> {
|
|||
match arg {
|
||||
None => {
|
||||
if let Some(task) = tasks.get_current_task() {
|
||||
println!("Change History:");
|
||||
for e in once(&task.event).chain(task.props.iter().rev()) {
|
||||
let content = match State::try_from(e.kind) {
|
||||
Ok(state) => {
|
||||
format!("State: {state}{}",
|
||||
if e.content.is_empty() { String::new() } else { format!(" - {}", e.content) })
|
||||
}
|
||||
Err(_) => {
|
||||
e.content.to_string()
|
||||
}
|
||||
};
|
||||
println!("{} {} [{}]",
|
||||
format_timestamp_local(&e.created_at),
|
||||
content,
|
||||
join(e.tags.iter().filter(|t| match_event_tag(t).unwrap().marker.is_none_or(|m| m != MARKER_PROPERTY))));
|
||||
format_timestamp_full(&e.created_at),
|
||||
match State::try_from(e.kind) {
|
||||
Ok(state) => {
|
||||
format!("State: {state}{}",
|
||||
if e.content.is_empty() { String::new() } else { format!(" - {}", e.content) })
|
||||
}
|
||||
Err(_) => {
|
||||
e.content.to_string()
|
||||
}
|
||||
},
|
||||
e.tags.iter().filter_map(|t| {
|
||||
match match_event_tag(t) {
|
||||
Some(et) =>
|
||||
Some(et).take_if(|et| et.marker.as_ref().is_some_and(|m| m != MARKER_PROPERTY))
|
||||
.map(|et| format!("{}: {}", et.marker.as_ref().unwrap(), tasks.get_relative_path(et.id))),
|
||||
None =>
|
||||
Some(format_tag_basic(t)),
|
||||
}
|
||||
}).join(", ")
|
||||
)
|
||||
}
|
||||
continue 'repl;
|
||||
} else {
|
||||
|
|
|
@ -92,11 +92,12 @@ impl Task {
|
|||
self.event.content.trim().trim_start_matches('#').to_string()
|
||||
}
|
||||
|
||||
pub(crate) fn description_events(&self) -> impl Iterator<Item=&Event> + '_ {
|
||||
fn description_events(&self) -> impl DoubleEndedIterator<Item=&Event> + '_ {
|
||||
self.props.iter().filter(|event| event.kind == Kind::TextNote)
|
||||
}
|
||||
|
||||
pub(crate) fn descriptions(&self) -> impl Iterator<Item=&String> + '_ {
|
||||
/// Description items, ordered newest to oldest
|
||||
pub(crate) fn descriptions(&self) -> impl DoubleEndedIterator<Item=&String> + '_ {
|
||||
self.description_events().map(|e| &e.content)
|
||||
}
|
||||
|
||||
|
@ -208,8 +209,8 @@ impl Task {
|
|||
// Dynamic
|
||||
"priority" => self.priority_raw().map(|c| c.to_string()),
|
||||
"status" => self.state_label().map(|c| c.to_string()),
|
||||
"desc" => self.descriptions().last().cloned(),
|
||||
"description" => Some(self.descriptions().join(" ")),
|
||||
"desc" => self.descriptions().next().cloned(),
|
||||
"description" => Some(self.descriptions().rev().join(" ")),
|
||||
"hashtags" => Some(self.join_tags(|tag| { is_hashtag(tag) })),
|
||||
"tags" => Some(self.join_tags(|_| true)), // TODO test these!
|
||||
"alltags" => Some(format!("{:?}", self.tags)),
|
||||
|
|
44
src/tasks.rs
44
src/tasks.rs
|
@ -399,6 +399,14 @@ impl TasksRelay {
|
|||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub(crate) fn get_relative_path(&self, id: EventId) -> String {
|
||||
join_tasks(
|
||||
self.traverse_up_from(Some(id))
|
||||
.take_while(|t| Some(t.event.id) != self.get_position()),
|
||||
false,
|
||||
).unwrap_or(id.to_string())
|
||||
}
|
||||
|
||||
/// Iterate over the task referenced by the given id and all its available parents.
|
||||
fn traverse_up_from(&self, id: Option<EventId>) -> ParentIterator {
|
||||
ParentIterator {
|
||||
|
@ -407,13 +415,6 @@ impl TasksRelay {
|
|||
}
|
||||
}
|
||||
|
||||
fn relative_path(&self, id: EventId) -> String {
|
||||
join_tasks(
|
||||
self.traverse_up_from(Some(id))
|
||||
.take_while(|t| Some(t.event.id) != self.get_position()),
|
||||
false,
|
||||
).unwrap_or(id.to_string())
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
|
@ -597,7 +598,7 @@ impl TasksRelay {
|
|||
}
|
||||
}),
|
||||
"path" => self.get_task_path(Some(task.event.id)),
|
||||
"rpath" => self.relative_path(task.event.id),
|
||||
"rpath" => self.get_relative_path(task.event.id),
|
||||
// TODO format strings configurable
|
||||
"time" => display_time("MMMm", self.time_tracked(*task.get_id())),
|
||||
"rtime" => display_time("HH:MM", self.total_time_tracked(*task.get_id())),
|
||||
|
@ -973,7 +974,7 @@ impl TasksRelay {
|
|||
let (input, input_tags) = extract_tags(input.trim());
|
||||
let prio =
|
||||
if input_tags.iter().any(|t| t.kind().to_string() == PRIO) { None } else { self.priority.map(|p| to_prio_tag(p)) };
|
||||
info!("Created task \"{input}\" with tags [{}]", join(&input_tags));
|
||||
info!("Created task \"{input}\" with tags [{}]", join_tags(&input_tags));
|
||||
let id = self.submit(
|
||||
EventBuilder::new(TASK_KIND, &input)
|
||||
.tags(input_tags)
|
||||
|
@ -1215,7 +1216,7 @@ impl TasksRelay {
|
|||
/// Sanitizes Input.
|
||||
pub(crate) fn make_note(&mut self, note: &str) -> EventId {
|
||||
let (name, tags) = extract_tags(note.trim());
|
||||
let format = format!("\"{name}\" with tags [{}]", join(&tags));
|
||||
let format = format!("\"{name}\" with tags [{}]", join_tags(&tags));
|
||||
let mut prop =
|
||||
EventBuilder::new(Kind::TextNote, name).tags(tags);
|
||||
//.filter(|id| self.get_by_id(id).is_some_and(|t| t.is_task()))
|
||||
|
@ -1284,13 +1285,14 @@ impl Display for TasksRelay {
|
|||
}
|
||||
writeln!(
|
||||
lock,
|
||||
"Active from {} (total tracked time {}m) - {} since {}",
|
||||
"Active from {} (total tracked time {}m) - State {} since {}",
|
||||
tracking_stamp.map_or("?".to_string(), |t| format_timestamp_relative(&t)),
|
||||
self.time_tracked(*t.get_id()) / 60,
|
||||
state.get_label(),
|
||||
format_timestamp_relative(&state.time)
|
||||
)?;
|
||||
writeln!(lock, "{}", t.descriptions().join("\n"))?;
|
||||
for d in t.descriptions().rev() { writeln!(lock, "{}", d)?; }
|
||||
writeln!(lock)?;
|
||||
}
|
||||
|
||||
let position = self.get_position();
|
||||
|
@ -1447,6 +1449,8 @@ fn display_time(format: &str, secs: u64) -> String {
|
|||
)
|
||||
}
|
||||
|
||||
/// Joins the tasks of this upwards iterator.
|
||||
/// * `include_last_id` whether to add the id of an unknown parent at the top
|
||||
pub(crate) fn join_tasks<'a>(
|
||||
iter: impl Iterator<Item=&'a Task>,
|
||||
include_last_id: bool,
|
||||
|
@ -1455,14 +1459,12 @@ pub(crate) fn join_tasks<'a>(
|
|||
tasks
|
||||
.iter()
|
||||
.map(|t| t.get_title())
|
||||
.chain(if include_last_id {
|
||||
.chain(
|
||||
tasks.last()
|
||||
.take_if(|_| include_last_id)
|
||||
.and_then(|t| t.parent_id())
|
||||
.map(|id| id.to_string())
|
||||
.into_iter()
|
||||
} else {
|
||||
None.into_iter()
|
||||
})
|
||||
.into_iter())
|
||||
.fold(None, |acc, val| {
|
||||
Some(acc.map_or_else(
|
||||
|| val.clone(),
|
||||
|
@ -1963,7 +1965,7 @@ mod tasks_test {
|
|||
let t11 = tasks.make_task("t11 # tag");
|
||||
assert_eq!(tasks.visible_tasks().len(), 1);
|
||||
assert_eq!(tasks.get_task_path(Some(t11)), "t1>t11");
|
||||
assert_eq!(tasks.relative_path(t11), "t11");
|
||||
assert_eq!(tasks.get_relative_path(t11), "t11");
|
||||
let t12 = tasks.make_task("t12");
|
||||
assert_eq!(tasks.visible_tasks().len(), 2);
|
||||
|
||||
|
@ -1973,7 +1975,7 @@ mod tasks_test {
|
|||
let t111 = tasks.make_task("t111");
|
||||
assert_tasks!(tasks, [t111]);
|
||||
assert_eq!(tasks.get_task_path(Some(t111)), "t1>t11>t111");
|
||||
assert_eq!(tasks.relative_path(t111), "t111");
|
||||
assert_eq!(tasks.get_relative_path(t111), "t111");
|
||||
tasks.view_depth = 2;
|
||||
assert_tasks!(tasks, [t111]);
|
||||
|
||||
|
@ -1988,7 +1990,7 @@ mod tasks_test {
|
|||
tasks.move_to(Some(t1));
|
||||
assert_position!(tasks, t1);
|
||||
assert_eq!(tasks.get_own_events_history().count(), 3);
|
||||
assert_eq!(tasks.relative_path(t111), "t11>t111");
|
||||
assert_eq!(tasks.get_relative_path(t111), "t11>t111");
|
||||
assert_eq!(tasks.view_depth, 2);
|
||||
assert_tasks!(tasks, [t111, t12]);
|
||||
tasks.set_view(vec![t11]);
|
||||
|
@ -2041,7 +2043,7 @@ mod tasks_test {
|
|||
tasks.get_task_path(Some(dangling)),
|
||||
"0000000000000000000000000000000000000000000000000000000000000000>test"
|
||||
);
|
||||
assert_eq!(tasks.relative_path(dangling), "test");
|
||||
assert_eq!(tasks.get_relative_path(dangling), "test");
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // #[test]
|
||||
|
|
Loading…
Add table
Reference in a new issue