---
title: "Printing directory trees with printtree"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Printing directory trees with printtree}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(printtree)
```

## Basic Usage

Print the tree for current working directory

```{r}
tmp <- tempdir()
demo <- file.path(tmp, "printtree-demo")

# Start fresh

if (dir.exists(demo)) unlink(demo, recursive = TRUE, force = TRUE)

dir.create(demo, recursive = TRUE)
dir.create(file.path(demo, "R"))
dir.create(file.path(demo, "data", "raw"), recursive = TRUE)

file.create(file.path(demo, "R", "hello.R"))
file.create(file.path(demo, "README.md"))
file.create(file.path(demo, ".Rhistory"))

```

Print the tree

```{r}
print_rtree()
```

## Project root detection

When project = "root", printtree can walk upward from the provided path to detect a project root using simple markers. By default this includes:

-   .Rproj files (RStudio / Posit projects)

-   DESCRIPTION files (R package roots)

In this example, we mark demo as a package-like root by creating a DESCRIPTION file, then print from a subdirectory:

```{r}
file.create(file.path(demo, "DESCRIPTION"))
subdir <- file.path(demo, "data", "raw")
print_rtree(subdir, project = "root")
```

You can customize detection using rootmarkers:

```{r}
print_rtree(subdir,
project = "root",
root_markers = c(".Rproj", "DESCRIPTION", "_quarto.yml"))

```

## Common Options

Limit depth:

```{r}
print_rtree(max_depth = 2)

```

Show hidden files:

```{r}
print_rtree(demo, show_hidden = TRUE, max_depth = 2)

```

Use unicode tree glyphs (if your terminal supports them):

```{r}
print_rtree(demo, format = "unicode", max_depth = 2)

```

Generate a PNG snapshot:

```{r}
# Save PNG snapshots 
png_light <- tempfile("tree-light-", fileext = ".png")
png_dark  <- tempfile("tree-dark-",  fileext = ".png")

# Light (white bg, black text)
print_rtree(demo, snapshot = TRUE, snapshot_bg = "white", snapshot_file = png_light)

# Dark (black bg, white text)
print_rtree(demo, snapshot = TRUE, snapshot_bg = "black", snapshot_file = png_dark)


```

