MystPi / bella

🐶 Bella ⌁ A simple functional programming language ⌁ Written in Gleam!
MIT License
20 stars 3 forks source link

Turn usage into a grid, because grids are cool #3

Closed Steve0Greatness closed 1 year ago

Steve0Greatness commented 1 year ago

Reason: grids are cool. Problem with the old way: it's not a grid, and grids are cool.

But really, this automatically adds spaces, meaning if new longer commands are added then all other lines don't need to be re-formatted manually(this would get especially tedious the more commands added).

MystPi commented 1 year ago

Could you give an example of what this would output? Never mind, I think I've got it.

MystPi commented 1 year ago

I have a few problems with this:

Overall, I think this is a good idea, but the implementation would have to change before it is merged.

MystPi commented 1 year ago

Here is an alternative:

const usage = [
  #("create <name>", "Create a new project"),
  #("run", "Run the current project"),
  #("<path>", "Run the given file"),
]

pub fn main() {
  case utils.get_args(), get_project() {
    _, #(True, Error(msg)) -> error(msg)
    ["create", name, ..], _ -> create_project(name)
    ["run", ..], #(True, Ok(project)) -> run_project(project.name)
    [path, ..], _ -> run_file(path)
    _, _ -> print_usage()
  }
}

fn print_usage() -> Nil {
  let assert Ok(max_len) =
    usage
    |> list.map(fn(x) { string.length(x.0) })
    |> list.sort(int.compare)
    |> list.last

  io.println("Usage:")

  usage
  |> list.each(fn(x) {
    let #(command, desc) = x
    let left_over = max_len - string.length(command) + 2
    io.println("  bella " <> command <> string.repeat(" ", left_over) <> desc)
  })
}

Output:

Usage:
  bella create <name>  Create a new project
  bella run            Run the current project
  bella <path>         Run the given file

Do you mind if I use this instead?

Steve0Greatness commented 1 year ago

Sure.