Skip to content

Instantly share code, notes, and snippets.

def printTable[A](rows: Seq[A], header: String*)(f: A => Seq[String]) = {
val rowsStrings = rows.map(f)
val cells = header +: rowsStrings
val widths = cells.transpose.map(_.map(_.length).max)
val format = widths.map("%-" + _ + "s").mkString(" | ")
val headerStr = format.format(header *)
val separator = widths.map("-" * _).mkString("-+-", "-+-", "-")
val rowsStr = rowsStrings.map(format.format(_ *)).mkString("\n")
println(s"$headerStr\n$separator\n$rowsStr")
}
package kupasyomtov.igudbneitorah.scripts
import bleep.commands.Compile
import bleep.internal.{TransitiveProjects, jvmRunCommand}
import bleep.model.{CrossProjectName, ProjectName}
import bleep.{BleepFileWatching, BleepScript, Commands, FileWatching, PathOps, Started}
import java.io.File
import java.nio.file.Files
import scala.jdk.CollectionConverters.*
@nafg
nafg / SlickMigration.scala
Created October 2, 2022 16:07
SlickMigration
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext}
import slick.dbio.DBIO
import org.flywaydb.core.api.migration.{BaseJavaMigration, Context}
abstract class SlickMigration extends BaseJavaMigration {
implicit val executionContext: ExecutionContext = ExecutionContext.global
import java.beans.Introspector
import java.nio.file.{Files, Paths}
object GenerateBeanCaseClass extends App {
val typeRenderings = Map[Class[_], String](
classOf[String] -> "String",
classOf[Boolean] -> "Boolean",
classOf[Int] -> "Int"
)
package chesednow.requests.sjs
import japgolly.scalajs.react.Callback
import japgolly.scalajs.react.vdom.VdomNode
import cats.implicits._
import monocle.{Iso, Prism}
trait PartialStateBase[V] {
type State
@nafg
nafg / SlickMetaGenPlugin.scala
Last active August 17, 2020 21:59
Simple code generator for Slick using Scalameta rather than strings, as an SBT plugin
import java.sql.Types
import scala.annotation.tailrec
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.meta._
import slick.dbio.DBIO
import slick.jdbc.meta.{MColumn, MQName, MTable}
[1/547] mill.scalalib.ZincWorkerModule.compilerInterfaceClasspath
[2/547] mill.scalalib.ZincWorkerModule.classpath
[4/547] shared_common.js.upstreamCompileOutput
[5/547] shared_common.js.sources
[6/547] shared_common.js.generatedSources
@nafg
nafg / paramCodec.scala
Last active March 10, 2020 10:55
Deriving circe with Magnolia but only as a fallback
import scala.language.experimental.macros
import scala.reflect.ClassTag
import io.circe.{Decoder, Encoder, Json}
import magnolia._
class ParamEncoder[T](val underlying: Encoder[T]) extends AnyVal
trait ParamEncoderLowPriority {
type Typeclass[T] = ParamEncoder[T]
@nafg
nafg / gen-instructions.sbt
Last active April 1, 2020 15:19
Auto-generate your library's install instructions in README
val generateInstallInstructions = taskKey[Unit]("Generate install instructions in README.md")
generateInstallInstructions := {
val info = Def.task((projectID.value, description.value, (publish / skip).value)).all(ScopeFilter(inAnyProject)).value
val lines =
for ((moduleId, descr, noPublish) <- info.sortBy(_._1.name) if !noPublish) yield {
"// " + descr + "\n" +
s"""libraryDependencies += "${moduleId.organization}" %%% "${moduleId.name}" % "${moduleId.revision}""""
}