Skip to content

Commit

Permalink
RealPath for cd, ls accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherDavenport committed Apr 26, 2021
1 parent 55deb94 commit 79bfcd8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions core/src/main/scala/io/chrisdavenport/shellfish/Shell.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ trait Shell[F[_]]{
/** Show all matching executables in PATH, not just the first */
def whichAll(path: String): Stream[F, String]

def ls: Stream[F, String]
def ls(path: String): Stream[F, String]
}

Expand Down Expand Up @@ -208,8 +209,9 @@ object Shell {

def cd(path: String): F[Unit] = for {
newPath <- getResolved(path)
out <- files.isDirectory(newPath).ifM(
setWd(newPath.toAbsolutePath.toString),
real = newPath.toRealPath()
out <- files.isDirectory(real).ifM(
setWd(real.toString),
new RuntimeException(s"cd: no such file or directory $path").raiseError
)
} yield out
Expand Down Expand Up @@ -306,9 +308,10 @@ object Shell {
.map(_.toString)
)

def ls: Stream[F, String] = ls("")
def ls(path: String): Stream[F, String] =
Stream.eval(getResolved(path)).flatMap(p =>
files.walk(p, 1).map(_.toString)
files.walk(p, 1).map(_.toString).drop(1) // Exclude Myself
)

}
Expand Down
3 changes: 2 additions & 1 deletion examples/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ object Main extends IOApp {
// got <- p.shellStrict("find", List("-wholename", "*.scala"))
// got <- which("java")
// got <- hostname
got <- ls("").compile.toList
_ <- cd("..")
got <- ls.compile.toList
_ <- echo(got.toString)
// _ <- p.shellStrict("which", "java":: Nil).flatMap(a => echo(a.toString))
} yield ExitCode.Success
Expand Down

0 comments on commit 79bfcd8

Please sign in to comment.