before creating custom monad & lake script to run main
Some checks are pending
Lean Action CI / build (push) Waiting to run
Some checks are pending
Lean Action CI / build (push) Waiting to run
This commit is contained in:
parent
a7b9963740
commit
3124f2bf78
3 changed files with 91 additions and 5 deletions
|
@ -1,2 +1,78 @@
|
||||||
def hello := "world"
|
def hello := "world"
|
||||||
|
|
||||||
|
structure Config where
|
||||||
|
useASCII : Bool := false
|
||||||
|
currentPrefix : String := ""
|
||||||
|
|
||||||
|
def usage : String :=
|
||||||
|
"Usage: doug [--ascii]
|
||||||
|
Options:
|
||||||
|
\t--ascii\tUse ASCII characters to display the directry structure"
|
||||||
|
|
||||||
|
def configFromArgs : List String → Option Config
|
||||||
|
| [] => some {}
|
||||||
|
| ["--ascii"] => some {useASCII := true}
|
||||||
|
| _ => none
|
||||||
|
|
||||||
|
inductive Entry where
|
||||||
|
| file : String → Entry
|
||||||
|
| dir : String → Entry
|
||||||
|
|
||||||
|
def toEntry (path : System.FilePath) : IO (Option Entry) := do
|
||||||
|
match path.components.getLast? with
|
||||||
|
| none => pure (some (.dir ""))
|
||||||
|
| some "." | some ".." => pure none
|
||||||
|
| some name =>
|
||||||
|
pure (some (if (← path.isDir) then .dir name else .file name))
|
||||||
|
|
||||||
|
def Config.preFile (cfg : Config) :=
|
||||||
|
if cfg.useASCII then "|--" else "├──"
|
||||||
|
|
||||||
|
def Config.preDir (cfg : Config) :=
|
||||||
|
if cfg.useASCII then "| " else "│ "
|
||||||
|
|
||||||
|
def Config.fileName (cfg : Config) (file : String) : String :=
|
||||||
|
s!"{cfg.currentPrefix}{cfg.preFile} {file}"
|
||||||
|
|
||||||
|
def Config.dirName (cfg : Config) (dir : String) : String :=
|
||||||
|
s!"{cfg.currentPrefix}{cfg.preDir} {dir}/"
|
||||||
|
|
||||||
|
/--
|
||||||
|
Modify `cfg` on entering a directory.
|
||||||
|
-/
|
||||||
|
def Config.inDirectory (cfg : Config) : Config :=
|
||||||
|
{cfg with currentPrefix := cfg.preDir ++ " " ++ cfg.currentPrefix}
|
||||||
|
|
||||||
|
def showFileName (cfg : Config) (file : String) : IO Unit := do
|
||||||
|
IO.println (cfg.fileName file)
|
||||||
|
|
||||||
|
def showDirName (cfg : Config) (dir : String) : IO Unit := do
|
||||||
|
IO.println (cfg.dirName dir)
|
||||||
|
|
||||||
|
def doList [Applicative f] : List α → (α → f Unit) → f Unit
|
||||||
|
| [], _ => pure ()
|
||||||
|
| x :: xs, action =>
|
||||||
|
action x *>
|
||||||
|
doList xs action
|
||||||
|
|
||||||
|
partial def dirTree (cfg : Config) (path : System.FilePath) : IO Unit := do
|
||||||
|
match ← toEntry path with
|
||||||
|
| none => pure ()
|
||||||
|
| some (.file name) => showFileName cfg name
|
||||||
|
| some (.dir name) =>
|
||||||
|
showDirName cfg name
|
||||||
|
let contents ← path.readDir
|
||||||
|
let newConfig := cfg.inDirectory
|
||||||
|
doList contents.toList fun d =>
|
||||||
|
dirTree newConfig d.path
|
||||||
|
|
||||||
|
def main (args : List String) : IO UInt32 := do
|
||||||
|
match configFromArgs args with
|
||||||
|
| some config =>
|
||||||
|
dirTree config (← IO.currentDir)
|
||||||
|
pure 0
|
||||||
|
| none =>
|
||||||
|
IO.eprintln s!"Didn't understand argument(s) {" ".intercalate args}\n"
|
||||||
|
IO.eprintln usage
|
||||||
|
pure 1
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import Doug
|
import Doug
|
||||||
|
|
||||||
def main : IO Unit :=
|
-- def main : IO Unit :=
|
||||||
IO.println s!"Hello, {hello}!"
|
-- IO.println s!"Hello, {hello}!"
|
||||||
|
|
|
@ -2,7 +2,7 @@ import Lake
|
||||||
open Lake DSL
|
open Lake DSL
|
||||||
|
|
||||||
meta if get_config? env = some "dev" then -- dev is so not everyone has to build it
|
meta if get_config? env = some "dev" then -- dev is so not everyone has to build it
|
||||||
require «doc-gen4» from git "https://github.com/leanprover/doc-gen4" @ "v4.12.0"
|
require "leanprover" / "doc-gen4"
|
||||||
|
|
||||||
package "doug" where
|
package "doug" where
|
||||||
version := v!"0.1.0"
|
version := v!"0.1.0"
|
||||||
|
@ -18,5 +18,15 @@ lean_exe "doug" where
|
||||||
Doug
|
Doug
|
||||||
-/
|
-/
|
||||||
script test (args) do
|
script test (args) do
|
||||||
IO.println args
|
let exe := (← doug.get).file
|
||||||
pure 0
|
IO.println s!"exe: {exe}"
|
||||||
|
IO.println s!"args: {args}"
|
||||||
|
let proc ← IO.Process.spawn
|
||||||
|
⟨{stdin := .inherit, stdout := .piped, stderr := .piped},
|
||||||
|
exe.toString, args.toArray, none, #[], false ⟩
|
||||||
|
IO.println s!"{← proc.tryWait}"
|
||||||
|
IO.print (← proc.stdout.readToEnd)
|
||||||
|
IO.eprintln (← IO.FS.Handle.readToEnd proc.stderr)
|
||||||
|
let ret ← proc.wait
|
||||||
|
IO.println s!"returned: {ret}"
|
||||||
|
pure ret
|
||||||
|
|
Loading…
Reference in a new issue