Skip to content

Commit

Permalink
Add list command and improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
remolueoend committed Mar 7, 2022
1 parent abcd773 commit b30255e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ To Read/Blogs
To Read/Blogs/my fav blog
To Read/Blogs/another blog
```
* Directory nodes which themselves consist of a link will be rendered as their own bookmark
* Directory nodes which themselves consist of a link, will be rendered as their own bookmark
* Leaf nodes not consisting of a link will be ignored.
* If a node is marked as checked on dynalist.io, the node and all its children are ignored.
* If a node is marked as checked on dynalist.io, the node and its children are ignored.

# Installation & Setup
Either download an available binary from the [release](https://github.com/remolueoend/dyna-bookmarks/releases) page or use the [rustup](https://rustup.rs/) toolchain to build your own binary by running `cargo build --release` in this project directory. You'll find the binary under `./target/release/dyna-bookmarks`.
Expand Down Expand Up @@ -62,5 +62,10 @@ Use this command to update your local cache with the current version of the dyna
## `clean` Command
This command deletes the local cache. When running `dyna-bookmarks rofi` afterward, the local cache is initiated again before displaying the `rofi` dialog.

# Props
Special thanks to [Dynalist](https://dynalist.io/) for their amazing tool.
## `list` Command
This command prints the list of all locally cached bookmarks to stdout, each bookmark formatted as `<text>\t<url>`. To further process the bookmarks, you can change the format by piping the output into, e.g. `awk`: `dyna-bookmarks list | awk 'F'\t' '{print($1 "<<>>" $2)}'`, where `$1` refers to the bookmark text and `$2` to the bookmark URL.

To sync the remote document to the local cache before or afterward, use the` dyna-bookmarks sync` command.

# Credits
Special thanks to [Dynalist](https://dynalist.io/) for their fantastic tool.
2 changes: 2 additions & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ struct CacheContent {
bookmarks: ParsedBookmarks,
}

/// Returns the content of the local cache as a list of parsed bookmarks.
/// Fails if the cache cannot be parsed or does not exist.
pub fn read_from_cache(cache_path: &PathBuf) -> Result<ParsedBookmarks> {
let cache_content = fs::read_to_string(cache_path)?;
let cache: CacheContent = toml::from_str(&cache_content)?;
Expand Down
7 changes: 5 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use eyre::Result;
use std::path::{Path, PathBuf};
use xdg::BaseDirectories;

use crate::commands::{clean::CleanCommandArgs, rofi::RofiCommandArgs, sync::SyncCommandArgs};
use crate::commands::{
clean::CleanCommandArgs, list::ListCommandArgs, rofi::RofiCommandArgs, sync::SyncCommandArgs,
};

#[derive(Parser, Debug)]
pub struct ApiAccessArgs {
/// the ID of the dynalist.io document.
#[clap(long, env)]
pub document_id: String,

/// the API token provided by dynalist.io.
#[clap(long, env)]
pub api_token: String,
Expand Down Expand Up @@ -55,4 +56,6 @@ pub enum SubCommandArgs {
Sync(SyncCommandArgs),
/// Delete the local cache.
Clean(CleanCommandArgs),
/// Prints the list of locally cached bookmarks to stdout.
List(ListCommandArgs),
}
36 changes: 36 additions & 0 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use clap::Parser;
use eyre::Result;
use log::error;

use crate::{cache, cli::LocalFilesArgs};

#[derive(Debug, Parser)]
pub struct ListCommandArgs {
#[clap(flatten)]
file_args: LocalFilesArgs,
/// the ID of the dynalist.io document.
#[clap(long, env)]
pub document_id: String,
}

pub fn run_command(args: ListCommandArgs) -> Result<()> {
let cache_path = args.file_args.get_cache_file_path(&args.document_id)?;
let booksmarks = cache::read_from_cache(&cache_path);

match booksmarks {
Ok(bookmarks) => {
for b in bookmarks {
println!("{}\t{}", b.0, b.1);
}
}
Err(err) => {
error!(
"failed to read local cache at {}: {}",
&cache_path.to_str().unwrap_or_default(),
err
);
}
};

Ok(())
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod clean;
pub mod list;
pub mod rofi;
pub mod sync;
2 changes: 1 addition & 1 deletion src/commands/rofi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct RofiCommandArgs {
open_exe: PathBuf,
}

/// First tries to read the content of the cache file. If it fails, it syncs the local document
/// First tries to read the content of the cache file. If it fails, it syncs the remote document
/// and tries to read the cache again. After successfully reading the cache, a rofi dialog is shown
/// allowing the user to select a bookmark and opens it.
/// While the rofi dialog is shown, the remote document is synced in the background (if not done so already).
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::StructOpt;
use dyna_bookmarks::{
cli::{CliArgs, SubCommandArgs},
commands::clean,
commands::list,
commands::rofi,
commands::sync,
};
Expand All @@ -17,5 +18,6 @@ fn main() -> Result<()> {
SubCommandArgs::Rofi(args) => rofi::run_command(args),
SubCommandArgs::Sync(args) => sync::run_command(args),
SubCommandArgs::Clean(args) => clean::run_command(args),
SubCommandArgs::List(args) => list::run_command(args),
}
}

0 comments on commit b30255e

Please sign in to comment.