Skip to content

Commit

Permalink
use better-files
Browse files Browse the repository at this point in the history
  • Loading branch information
molarmanful committed Oct 25, 2023
1 parent d79b700 commit 852c3a6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
3 changes: 1 addition & 2 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ object sclin extends ScoverageModule with PublishModule with ScalafmtModule {
ivy"com.lihaoyi::fansi:0.4.0",
ivy"com.lihaoyi::upickle:3.1.3",
ivy"io.monix::monix:3.4.1",
ivy"com.lihaoyi::geny:1.0.0",
ivy"com.lihaoyi::os-lib:0.9.1"
ivy"com.github.pathikrit::better-files:3.9.2"
)

def cmdoc() = T.command {
Expand Down
20 changes: 4 additions & 16 deletions sclin/src/AnyType.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sclin

import better.files.*
import cats.kernel.Eq
import geny.Generator
import monix.eval.Task
Expand Down Expand Up @@ -549,11 +550,9 @@ enum ANY:
case Its(_) => matchType(a)
case _ => toARR

def toPath: os.Path = os.Path(toFilePath, os.pwd)

def toFilePath: os.FilePath = this match
case Itr(_) | _: FN => foldLeft(os.pwd)((a, b) => os.Path(b.toFilePath, a))
case _ => os.FilePath(toString)
def toPath: File = this match
case Itr(_) => foldLeft(File(""))((a, b) => a / b.toString)
case _ => File(toString)

def map(f: ANY => ANY): ANY = this match
case OBS(x) => x.map(f).toOBS
Expand Down Expand Up @@ -1237,17 +1236,6 @@ object ANY:

extension (t: Observable[ANY]) def toOBS: OBS = OBS(t)

extension [T](g: Generator[T])

def obs: Observable[T] =
Observable.create(OverflowStrategy.Unbounded): s =>
g.generate:
s.onNext(_) match
case Ack.Continue => Generator.Continue
case Ack.Stop => Generator.End
s.onComplete()
Cancelable.empty

given ReadWriter[ANY] = readwriter[ujson.Value].bimap[ANY](_.toJSON, _.toANY)

given Eq[ANY] with
Expand Down
34 changes: 23 additions & 11 deletions sclin/src/Lib.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sclin

import better.files.*
import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
import monix.reactive.Observable
Expand Down Expand Up @@ -283,9 +284,19 @@ extension (env: ENV)
case "$FILE" => getLFile
/*
@s -> STR
Current working directory of program execution.
Current working directory at start of program execution.
*/
case "$CWD" => env.push(os.pwd.toString.sSTR)
case "$PWD" => env.push(Dsl.pwd.toString.sSTR)
/*
@s -> STR
Current working directory at current state in program execution.
*/
case "$CWD" => env.push(Dsl.cwd.toString.sSTR)
/*
@s -> STR
Home directory.
*/
case "$~" => env.push(File.home.toString.sSTR)
/*
@s -> SEQ[NUM*]
Infinite `SEQ` of 0 to ∞.
Expand Down Expand Up @@ -2417,15 +2428,16 @@ extension (env: ENV)
def form: ENV = env.mod1(_.toForm.sSTR)
def outf: ENV = env.form.outn

def fread: ENV = env.mod2: (x, y) =>
y.vec1: n =>
os.read
.chunks(x.toPath, n.toInt)
.obs
.map { case (xs, n) => xs.take(n).toVector.map(_.toChar).mkString.sSTR }
.toOBS
def freadl: ENV =
env.mod1(x => os.read.lines.stream(x.toPath).obs.map(_.sSTR).toOBS)
def fread: ENV = env.mod1: x =>
Observable
.fromIterator(Task(x.toPath.bytes))
.map(_.toChar.toString.sSTR)
.toOBS
def freadl: ENV = env.mod1: x =>
Observable
.fromIterator(Task(x.toPath.lineIterator))
.map(_.sSTR)
.toOBS

def dup: ENV = env.mods1(x => Vector(x, x))
def dups: ENV = env.push(env.stack.toARR)
Expand Down
9 changes: 5 additions & 4 deletions sclin/src/Main.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sclin

import better.files.*
import mainargs.*
import scala.util.chaining.*

Expand All @@ -9,7 +10,7 @@ object Main:
ParserForMethods(this).runOrExit(args.toIndexedSeq, allowPositional = true)

@main def sclin(
@arg(short = 'f', doc = "Execute file.") file: Option[os.Path],
@arg(short = 'f', doc = "Execute file.") file: Option[File],
@arg(short = 'e', doc = "Execute string.") eval: Option[String],
@arg(hidden = true) doceval: Option[String],
@arg(short = 's', doc = "Step mode.") step: Flag,
Expand All @@ -31,7 +32,7 @@ object Main:
nc = nocolor.value
)
file match
case Some(f) => ENV.run(os.read(f), file, flags, cflag)
case Some(f) => ENV.run(f.contentAsString, file, flags, cflag)
case _ =>
eval match
case Some(s) => ENV.run(s, file, flags, cflag)
Expand All @@ -44,7 +45,7 @@ object Main:
err:
s"ERR: $e\n ---\n${e.getStackTrace.map(" " + _).mkString("\n")}"

given TokensReader.Simple[os.Path] with
given TokensReader.Simple[File] with

def shortName = "path"
def read(strs: Seq[String]) = Right(os.Path(strs.head, os.pwd))
def read(strs: Seq[String]) = Right(File(strs.head))
3 changes: 2 additions & 1 deletion sclin/src/Misc.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package sclin

import better.files.*
import monix.execution.CancelableFuture
import scala.collection.concurrent.TrieMap
import scala.collection.immutable.HashMap
import scala.collection.immutable.VectorMap
import spire.math.*
import util.chaining.*

type FILE = Option[os.Path]
type FILE = Option[File]

type ARRW[T] = Vector[T]
type SEQW[T] = LazyList[T]
Expand Down

0 comments on commit 852c3a6

Please sign in to comment.