Skip to content

Commit

Permalink
Merge branch 'release/1.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaaskarian committed Jul 28, 2024
2 parents 871c361 + 38290fa commit c80f8bb
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 368 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "c3"
version = "1.3.1"
version = "1.3.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions bump-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ bump_version() {
build_package() {
cargo build --release || echo_exit Linux build failed.
cargo build --release --target x86_64-pc-windows-gnu || echo_exit Windows build failed.
export ANDROID_NDK_HOME=/opt/android-sdk/ndk/27.0.12077973
cargo ndk -t aarch64-linux-android build --release || echo_exit Termux build failed.
}

cargo test || echo_exit Unittests failed.
Expand Down
3 changes: 2 additions & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ zip_source() {
release_package() {
cp target/release/c3 c3.x86.linux || echo_exit copy linux binary failed
cp target/x86_64-pc-windows-gnu/release/c3.exe c3.x86_64.windows.exe || echo_exit copy windows binary failed
FILES="c3.x86.linux c3.x86_64.windows.exe $SOURCE"
cp target/aarch64-linux-android/release/c3 c3.termux || echo_exit copy termux binary failed
FILES="c3.x86.linux c3.x86_64.windows.exe c3.termux $SOURCE"
gh release create "$TAG" $FILES --title "$TAG" --notes "**Full Changelog**: https://github.com/$USERNAME/$PACKAGE_NAME/compare/$LAST_TAG...$TAG" --repo $USERNAME/$PACKAGE_NAME
rm $FILES
}
Expand Down
156 changes: 90 additions & 66 deletions src/cli_app.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,106 @@
use clap::{Command, CommandFactory};
use super::todo_app::{App, RestrictionFunction, Todo, TodoList};
use crate::DisplayArgs;
use crate::{CliArgs, DisplayArgs, DoOnSelected};
use crate::Args;
use clap_complete::{generate, Generator};
use std::io;
use std::process;

pub struct NotCli;
#[inline]
pub fn run(app: &mut App) -> io::Result<()> {
let app = CliApp::new(app);
app.print()?;
Ok(())
}

pub struct CliApp<'a> {
todo_app: &'a App,
}

impl<'a> CliApp<'a> {
#[inline]
pub fn new(app: &'a mut App) -> Self {
for message in app.args.append_todo.clone() {
app.append(message);
}

if let Some(path) = app.args.output_file.clone() {
app.output_list_to_path(&path);
pub fn run(app: &mut App, args: CliArgs) -> Result<(),NotCli> {
if !args.search_and_select.is_empty() {
for query in args.search_and_select {
app.set_query_restriction(query, None)
}

for message in app.args.prepend_todo.clone() {
app.prepend(message);
if app.is_todos_empty() {
process::exit(1);
}
if let Some(path) = app.args.append_file.clone() {
app.append_list_from_path(&path)
let restriction = app.restriction().clone();
if let Some(do_on_selected) = args.do_on_selected {
match do_on_selected {
DoOnSelected::Delete => {
app.current_list_mut().todos.retain(|todo| !restriction(todo))
}
DoOnSelected::Done => {
for todo in app.current_list_mut().todos_mut(&restriction) {
todo.set_done(true);
}
}
}
} else {
print_todos(app);
return Ok(())
}
app.do_commands_on_selected();
let _ = app.write();
CliApp { todo_app: app }
}

#[inline]
fn print_list(&self) {
for display in self.todo_app.display_current() {
println!("{}", display);
if args.batch_edit {
app.batch_editor_messages();
}
if app.is_changed() {
app.write();
}
if args.print_path {
println!("{}", app.args.todo_path.to_str().unwrap());
let notes = app.args.todo_path.parent().unwrap().join("notes");
if notes.is_dir() {
println!("{}", notes.to_str().unwrap_or(""));
}
return Ok(())
}
if let Some(generator) = args.completion {
print_completions(generator, &mut Args::command());
return Ok(())
}

#[inline]
pub fn print(&self) -> io::Result<()> {
if !self.todo_app.args.search_and_select.is_empty() {
self.todo_app.print_selected();
return Ok(());
}
if self.todo_app.args.stdout {
self.todo_app.print()?;
return Ok(());
}
if self.todo_app.is_tree() {
let mut print_todo = PrintTodoTree::new(self.todo_app.args.minimal_tree);
print_todo.print_list(
&self.todo_app.todo_list,
&self.todo_app.args.display_args,
self.todo_app.restriction(),
);
if args.stdout {
app.write_to_stdout();
return Ok(())
}
if args.minimal_tree || args.list {
if app.args.no_tree {
print_todos(app);
} else {
self.print_list()
let mut print_todo = PrintTodoTree::new(args.minimal_tree);
print_todo.print_list(
&app.todo_list,
&app.args.display_args,
app.restriction(),
)
}
Ok(())
return Ok(())
}
if let Some(path) = args.output_file.as_ref() {
app.output_list_to_path(path);
return Ok(())
}
Err(NotCli)
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn print_todos(app: &App) {
for display in app.display_current() {
println!("{}", display);
}
}

// TODO: Use traverse_tree instead of this struct for printing todo tree.
#[derive(Clone)]
struct PrintTodoTree {
last_stack: Vec<bool>,
should_print_indention: bool,
should_skip_indention: bool,
is_last: bool,
}

impl PrintTodoTree {
#[inline]
pub fn new(should_print_indention: bool) -> Self {
pub fn new(should_skip_indention: bool) -> Self {
PrintTodoTree {
last_stack: vec![],
is_last: false,
should_print_indention,
should_skip_indention,
}
}

Expand Down Expand Up @@ -130,27 +150,31 @@ impl PrintTodoTree {

#[inline]
fn print_note(&mut self, note: &str) {
let mut last_stack = self.last_stack.clone();
last_stack.push(self.what_to_push());

for line in note.lines() {
self.print_prenote(last_stack.clone());
let last = if self.last_stack.is_empty() {
None
} else {
Some(self.what_to_push())
};
self.print_prenote(&self.last_stack, last);
println!("{}", line);
}
}

#[inline]
fn print_prenote(&self, last_stack: Vec<bool>) {
self.print_preindention(&last_stack);
print!(" ")
fn print_prenote(&self, last_stack: &[bool], last_item: Option<bool>) {
if !self.should_skip_indention {
self.print_preindention(last_stack, last_item);
}
print!(" ")
}

#[inline]
fn print_indention(&self) {
if self.should_print_indention {
if self.should_skip_indention {
return;
}
self.print_preindention(&self.last_stack);
self.print_preindention(&self.last_stack, None);
if self.is_last {
print!("└── ");
} else {
Expand All @@ -159,10 +183,10 @@ impl PrintTodoTree {
}

#[inline(always)]
fn print_preindention(&self, last_stack: &[bool]) {
fn print_preindention(&self, last_stack: &[bool], last_item: Option<bool>) {
let mut stack_iter = last_stack.iter();
stack_iter.next();
for &x in stack_iter {
for &x in stack_iter.chain(last_item.as_ref()) {
if x {
print!("│ ")
} else {
Expand Down
10 changes: 6 additions & 4 deletions src/fileio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use std::time::{SystemTime, UNIX_EPOCH};
use std::env;
use std::process::Command;

#[inline(always)]
pub fn append_notes_to_path_parent(filename: &Path) -> PathBuf {
filename.parent().unwrap().join("notes")
}

#[inline(always)]
pub fn open_temp_editor(content: Option<&str>, path: PathBuf) -> io::Result<String> {
let mut file = File::create(&path)?;
Expand All @@ -16,10 +21,7 @@ pub fn open_temp_editor(content: Option<&str>, path: PathBuf) -> io::Result<Stri
let editor = if cfg!(windows) {
String::from("notepad")
} else {
match env::var("EDITOR") {
Ok(editor) => editor,
Err(_) => String::from("vim"),
}
env::var("EDITOR").unwrap_or(String::from("vim"))
};
Command::new(editor)
.arg(&path)
Expand Down
Loading

0 comments on commit c80f8bb

Please sign in to comment.