How to conditionally modify one field (column) in a table?

Given a table passwords.yaml with the content of

--- # User, Password, Shell
-
    - "u1"
    -  "p1"
    -  "/bin/sh"
-   
    - "u2"
    -  "p2"
    -  "/bin/sh"
-
    - "u3"
    -  "p3"
    - "/opt/homebrew/bin/bash"

I use the following typst fragment

#let data = yaml("passwords.yaml")
#figure(
  kind: table,
  align(center)[
    #table(
      columns: 3,
      align: (left, left, left),
      [*#align(center)[User]*], 
        [*#align(center)[Password]*], 
        [*#align(center)[Shell]*],
      ..data.flatten()
    )
  ],
  caption: [passwords.yaml],
) <passwords.yaml>

which produces the expected table.

I would like to obfuscate the passwords (column 1 (starting with 0)) depending on the value of OBF using something like

#let OBF = "TRUE"
#let HASH = "###########"

Is that possible without conditionalizing the whole figure?

Hello, this topic is a question, so please use Questions category for such topics.

First of all, passwords are a very sensitive topic, so I hope you know what you are doing.

There can be various solutions, but here is a very simple one + I simplified your code:

#let passwords = ```yaml
--- # User, Password, Shell
-
    - "u1"
    -  "p1"
    -  "/bin/sh"
-
    - "u2"
    -  "p2"
    -  "/bin/sh"
-
    - "u3"
    -  "p3"
    - "/opt/homebrew/bin/bash"
```.text

#let OBF = false
#let OBF = true // Comment this to toggle.
#let HASH = "###########"

#let data = yaml(bytes(passwords))
#if OBF {
  data = data.map(row => {
    row.at(1) = HASH
    row
  })
}
#show table.cell.where(y: 0): strong
#figure(
  table(
    columns: 3,
    align: (x, y) => if y == 0 { center } else { left },
    table.header[User][Password][Shell],
    ..data.flatten(),
  ),
  caption: [passwords.yaml],
) <passwords.yaml>

image

Thank you.

I am fully aware of the password sensitivity.

Is there a way of moving it over to Questions?

1 Like

Edit your original post and select Questions category under the title name in the edit window.

Done, thanks you very much

1 Like

Hi @ondohotola, thank you for your question! I have changed your post’s title to bring it in line with the question guidelines and thus make it easier to understand from the title:

Good titles are questions you would ask your friend about Typst.

I also added the tables tag, as it makes your question easier to find. Thank you for already moving your topic as Andrew described :slight_smile:

Ah, ok. Did not know that one either.

Thank you as well.