Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 142 additions & 140 deletions src/main/scala/eu/eyan/lister/FileLister3.scala
Original file line number Diff line number Diff line change
@@ -1,141 +1,143 @@
package eu.eyan.lister

import eu.eyan.util.swing.JPanelWithFrameLayout
import eu.eyan.log.Log
import eu.eyan.log.LogWindow
import eu.eyan.util.awt.clipboard.ClipboardPlus
import eu.eyan.util.io.FilePlus.FilePlusImplicit
import eu.eyan.util.java.net.URLPlus.URLImplicit
import eu.eyan.util.registry.RegistryPlus
import eu.eyan.util.scala.collection.TraversableOncePlus.TraversableOnceImplicit
import eu.eyan.util.string.StringPlus.StringPlusImplicit
import eu.eyan.util.swing.JButtonPlus.JButtonImplicit
import eu.eyan.util.swing.JCheckBoxPlus.JCheckBoxImplicit
import eu.eyan.util.swing.JFramePlus.JFramePlusImplicit
import eu.eyan.util.swing.JLabelPlus.JLabelImplicit
import eu.eyan.util.swing.JPanelWithFrameLayout
import eu.eyan.util.swing.JTextAreaPlus.JTextAreaImplicit
import eu.eyan.util.swing.JTextFieldPlus.JTextFieldPlusImplicit
import eu.eyan.util.swing.SwingPlus
import javax.swing.JFrame
import javax.swing.JOptionPane
import java.awt.Color
import java.awt.Desktop
import java.net.URI
import java.net.URLEncoder
import javax.swing.filechooser.FileSystemView
import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import eu.eyan.util.scala.TryCatchFinally
import java.time.Instant
import eu.eyan.util.java.time.InstantPlus.InstantImplicit
import eu.eyan.util.compress.CompressPlus
import eu.eyan.util.compress.ZipPlus
import eu.eyan.util.compress.SevenZipPlus
import eu.eyan.util.scala.TryCatchFinallyClose
import eu.eyan.util.system.SystemPlus.KeepAlive
import eu.eyan.util.system.SystemPlus
import rx.lang.scala.subjects.BehaviorSubject
import eu.eyan.util.rx.lang.scala.ObservablePlus.ObservableImplicitBoolean
import javax.swing.SwingUtilities
import rx.lang.scala.Observable
import eu.eyan.util.rx.lang.scala.ObservablePlus
import eu.eyan.util.swing.panelbuilder.JPanelBuilder

/**
* To list all the files on all drives. Also search inside the compressed files.
*/
object FileLister3 extends App {

Log.activateDebugLevel
// TODO reimplement LogWindow.redirectOutAndErrToFiles("""C:\temp""")
LogWindow.redirectSystemOutAndErrToLogWindow
"1".println
"2".printlnErr

val TITLE = "File Lister"


val targetText = BehaviorSubject[String]
val daysText = BehaviorSubject[String]
val ignoreText = BehaviorSubject[String]
val startButton = BehaviorSubject[String]
val isWorkInProgress = BehaviorSubject(false)
val counter = BehaviorSubject[Int]

val panel = JPanelBuilder().withBorders.withSeparators.newColumn.newColumnFPG
.newRow.addLabel.text("Target file:")
.nextColumn.addTextField.text("C:\\temp\\AllDrivesFileList.txt").onTextChanged(targetText).remember("targetPath")

.newRow.addLabel.text("Look into compressed files:")
.nextColumn.addCheckBox.text("").selected(false).onSelectionChanged(selected => {}).remember("lookIntoCompressedFiles")

.newRow.addLabel.text("Execute after every x day:")
.nextColumn.addTextField.text("7").onTextChanged(daysText).remember("days")

.newRow.addLabel.text("Ignore if contains:")
.nextColumn.addTextField.text("I:,N:,P:,T:,Y:").onTextChanged(ignoreText).remember("ignore")

.newRow.addLabel.text("Start to list:")
.nextColumn.addButton.text("Start to list.").onActionEvent(startButton).enabled(isWorkInProgress.negate)

.newRow.addLabel.text("Count").text(counter.map(_.toString))
.getPanel

val frame = new JFrame().title(TITLE).onCloseHide.iconFromChar('L', Color.CYAN).addToSystemTray().withComponent(panel)
.menuItem("File", "Exit", System.exit(0))
.menuItem("Debug", "Open log window", LogWindow.show(panel))
.menuItem("Debug", "Copy logs to clipboard", ClipboardPlus.copyToClipboard(LogWindow.getAllLogs))
.menuItem("Debug", "Clear registry values", RegistryPlus.clear(TITLE))
.menuItem("Help", "Write email", writeEmail)
.menuItem("Help", "About", alert("This is not an official tool, no responsibilities are taken. Use it at your own risk."))
.packAndSetVisible

ObservablePlus.toList(startButton, targetText.map(_.asFile)).map(_(1).asInstanceOf[File]).subscribe(startToList(_))
def writeEmail = Desktop.getDesktop.mail(new URI("mailto:PVTools@eyan.eu?subject=FileLister&body=" + URLEncoder.encode(LogWindow.getAllLogs, "utf-8").replace("+", "%20")))
def alert(msg: String) = JOptionPane.showMessageDialog(null, msg)

var ct = 0

def startToList(target: File) = {
println("startToList " + target)
SwingPlus.runInWorker({ isWorkInProgress.onNext(true); listFilesOfAllDrives(target) }, isWorkInProgress.onNext(false))
}

def listFilesOfAllDrives(target: File) = SystemPlus.keepAlive {
ct = 0
val fsv = FileSystemView.getFileSystemView
// val roots = List("""C:\DEV\projects\AndrisTools\src\test\resources\list""".asFile).map(_.fileTreeWithItself)
val roots = File.listRoots /*.filter(_.getAbsolutePath.containsAnyIgnoreCase(ignoreTextField.getText.split(",")))*/ .map(_.fileTreeWithItself)
if (target.exists) target.renameTo(target.extendFileNameWith("_old").generateNewNameIfExists())

TryCatchFinallyClose(
new BufferedWriter(new FileWriter(target)),
(bw: BufferedWriter) => roots.foreach(_.foreach(file => bw.write(logFile(file) + "\r\n"))),
t => Log.error(s"Error opening $target to write", t))

}

val DT = "yyyyMMdd HHmmss"
def logFile(file: File) = {
ct += 1
counter.onNext(ct)
val path = file.getAbsolutePath
val size = file.length
val lastModified = file.lastModified
val date = Instant.ofEpochMilli(lastModified)

val zipContent =
if (file.isFile && file.endsWith("zip")) {
ZipPlus.listFiles(file).map(zip => formatFileLog(path + "\\" + zip.getName, zip.getLastModifiedTime.toInstant, zip.getSize)).mkString("\r\n", "\r\n", "")
} else if (file.isFile && file.endsWith("7z", "7zip")) {
SevenZipPlus.listFiles(file).map(zip => formatFileLog(path + "\\" + zip.getName, zip.getLastModifiedDate.toInstant, zip.getSize)).mkString("\r\n", "\r\n", "")
} else ""

formatFileLog(path, date, size) + zipContent
}

def formatFileLog(path: String, instant: Instant, size: Long) = f"${instant.toString(DT)} $size%10s $path"

package eu.eyan.lister

import eu.eyan.util.swing.JPanelWithFrameLayout
import eu.eyan.log.Log
import eu.eyan.log.LogWindow
import eu.eyan.util.awt.clipboard.ClipboardPlus
import eu.eyan.util.io.FilePlus.FilePlusImplicit
import eu.eyan.util.java.net.URLPlus.URLImplicit
import eu.eyan.util.registry.RegistryPlus
import eu.eyan.util.scala.collection.TraversableOncePlus.TraversableOnceImplicit
import eu.eyan.util.string.StringPlus.StringPlusImplicit
import eu.eyan.util.swing.JButtonPlus.JButtonImplicit
import eu.eyan.util.swing.JCheckBoxPlus.JCheckBoxImplicit
import eu.eyan.util.swing.JFramePlus.JFramePlusImplicit
import eu.eyan.util.swing.JLabelPlus.JLabelImplicit
import eu.eyan.util.swing.JPanelWithFrameLayout
import eu.eyan.util.swing.JTextAreaPlus.JTextAreaImplicit
import eu.eyan.util.swing.JTextFieldPlus.JTextFieldPlusImplicit
import eu.eyan.util.swing.SwingPlus
import javax.swing.JFrame
import javax.swing.JOptionPane
import java.awt.Color
import java.awt.Desktop
import java.net.URI
import java.net.URLEncoder
import javax.swing.filechooser.FileSystemView
import java.io.File
import java.io.BufferedWriter
import java.io.FileWriter
import eu.eyan.util.scala.TryCatchFinally
import java.time.Instant
import eu.eyan.util.java.time.InstantPlus.InstantImplicit
import eu.eyan.util.compress.CompressPlus
import eu.eyan.util.compress.ZipPlus
import eu.eyan.util.compress.SevenZipPlus
import eu.eyan.util.scala.TryCatchFinallyClose
import eu.eyan.util.system.SystemPlus.KeepAlive
import eu.eyan.util.system.SystemPlus
import rx.lang.scala.subjects.BehaviorSubject
import eu.eyan.util.rx.lang.scala.ObservablePlus.ObservableImplicitBoolean
import javax.swing.SwingUtilities
import rx.lang.scala.Observable
import eu.eyan.util.rx.lang.scala.ObservablePlus
import eu.eyan.util.swing.panelbuilder.JPanelBuilder
import eu.eyan.util.swing.panelbuilder.Click
import eu.eyan.util.rx.lang.scala.ObservablePlus.ObservableImplicitT
import eu.eyan.util.rx.lang.scala.subjects.BehaviorSubjectPlus.BehaviorSubjectImplicitT

/**
* To list all the files on all drives. Also search inside the compressed files.
*/
object FileLister3 extends App {

Log.activateDebugLevel
// TODO reimplement LogWindow.redirectOutAndErrToFiles("""C:\temp""")
LogWindow.redirectSystemOutAndErrToLogWindow
"1".println
"2".printlnErr

val TITLE = "File Lister"


val targetText = BehaviorSubject[String]
val daysText = BehaviorSubject[String]
val ignoreText = BehaviorSubject[String]
val startButton = BehaviorSubject[Click]
val isWorkInProgress = BehaviorSubject(false)
val counter = BehaviorSubject[Int]

val panel = JPanelBuilder().withBorders.withSeparators.newColumn.newColumnFPG
.newRow.addLabel.text("Target file:")
.nextColumn.addTextField.text("C:\\temp\\AllDrivesFileList.txt").onTextChanged(targetText).remember("targetPath")

.newRow.addLabel.text("Look into compressed files:")
.nextColumn.addCheckBox.text("").selected(false).onSelectionChanged(selected => {}).remember("lookIntoCompressedFiles")

.newRow.addLabel.text("Execute after every x day:")
.nextColumn.addTextField.text("7").onTextChanged(daysText).remember("days")

.newRow.addLabel.text("Ignore if contains:")
.nextColumn.addTextField.text("I:,N:,P:,T:,Y:").onTextChanged(ignoreText).remember("ignore")

.newRow.addLabel.text("Start to list:")
.nextColumn.addButton.text("Start to list.").onAction(startButton).enabled(isWorkInProgress.negate)

.newRow.addLabel.text("Count").text(counter.map(_.toString))
.getPanel

val frame = new JFrame().title(TITLE).onCloseHide.iconFromChar('L', Color.CYAN).addToSystemTray().withComponent(panel)
.menuItem("File", "Exit", System.exit(0))
.menuItem("Debug", "Open log window", LogWindow.show(panel))
.menuItem("Debug", "Copy logs to clipboard", ClipboardPlus.copyToClipboard(LogWindow.getAllLogs))
.menuItem("Debug", "Clear registry values", RegistryPlus.clear(TITLE))
.menuItem("Help", "Write email", writeEmail)
.menuItem("Help", "About", alert("This is not an official tool, no responsibilities are taken. Use it at your own risk."))
.packAndSetVisible

startButton.takeLatestOf(targetText.map(_.asFile)).map(_.asInstanceOf[File]).subscribe(startToList(_))
def writeEmail = Desktop.getDesktop.mail(new URI("mailto:PVTools@eyan.eu?subject=FileLister&body=" + URLEncoder.encode(LogWindow.getAllLogs, "utf-8").replace("+", "%20")))
def alert(msg: String) = JOptionPane.showMessageDialog(null, msg)

var ct = 0

def startToList(target: File) = {
println("startToList " + target)
SwingPlus.runInWorker({ isWorkInProgress.onNext(true); listFilesOfAllDrives(target) }, isWorkInProgress.onNext(false))
}

def listFilesOfAllDrives(target: File) = SystemPlus.keepAlive {
ct = 0
val fsv = FileSystemView.getFileSystemView
// val roots = List("""C:\DEV\projects\AndrisTools\src\test\resources\list""".asFile).map(_.fileTreeWithItself)
val roots = File.listRoots /*.filter(_.getAbsolutePath.containsAnyIgnoreCase(ignoreTextField.getText.split(",")))*/ .map(_.fileTreeWithItself)
if (target.exists) target.renameTo(target.extendFileNameWith("_old").generateNewNameIfExists())

TryCatchFinallyClose(
new BufferedWriter(new FileWriter(target)),
(bw: BufferedWriter) => roots.foreach(_.foreach(file => bw.write(logFile(file) + "\r\n"))),
t => Log.error(s"Error opening $target to write", t))

}

val DT = "yyyyMMdd HHmmss"
def logFile(file: File) = {
ct += 1
counter.onNext(ct)
val path = file.getAbsolutePath
val size = file.length
val lastModified = file.lastModified
val date = Instant.ofEpochMilli(lastModified)

val zipContent =
if (file.isFile && file.endsWith("zip")) {
ZipPlus.listFiles(file).map(zip => formatFileLog(path + "\\" + zip.getName, zip.getLastModifiedTime.toInstant, zip.getSize)).mkString("\r\n", "\r\n", "")
} else if (file.isFile && file.endsWith("7z", "7zip")) {
SevenZipPlus.listFiles(file).map(zip => formatFileLog(path + "\\" + zip.getName, zip.getLastModifiedDate.toInstant, zip.getSize)).mkString("\r\n", "\r\n", "")
} else ""

formatFileLog(path, date, size) + zipContent
}

def formatFileLog(path: String, instant: Instant, size: Long) = f"${instant.toString(DT)} $size%10s $path"
}
67 changes: 67 additions & 0 deletions src/main/scala/eu/eyan/sudoku/OrigSudoku.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package eu.eyan.sudoku

object OrigSudoku extends App {
case class Col(col: Int) {
def block = ((col - 1) / 3) * 3
}
case class Row(row: Int) {
def block = ((row - 1) / 3) * 3
}

val cols = (1 to 9).map(Col(_)).toList
val rows = (1 to 9).map(Row(_)).toList

case class Grid(private val grid: Array[Array[Int]]) {
override def toString = grid.map(_.mkString(" ")).mkString("\r\n") + "\r\n"
def get(row: Row, col: Col) = grid(row.row - 1)(col.col - 1)
def set(row: Row, col: Col, n: Int) = grid(row.row - 1)(col.col - 1) = n
}

val grid = Grid(Array(
Array(5, 3, 0, 0, 7, 0, 0, 0, 0),
Array(6, 0, 0, 1, 9, 5, 0, 0, 0),
Array(0, 9, 8, 0, 0, 0, 0, 6, 0),

Array(8, 0, 0, 0, 6, 0, 0, 0, 3),
Array(4, 0, 0, 8, 0, 3, 0, 0, 1),
Array(7, 0, 0, 0, 2, 0, 0, 0, 6),

Array(0, 6, 0, 0, 0, 0, 2, 8, 0),
Array(0, 0, 0, 4, 1, 9, 0, 0, 5),
Array(0, 0, 0, 0, 8, 0, 0, 7, 9)))

val grid2 = Grid(Array(
Array(5, 3, 0, 0, 7, 0, 0, 0, 0),
Array(6, 0, 0, 1, 9, 5, 0, 0, 0),
Array(0, 9, 8, 0, 0, 0, 0, 6, 0),

Array(8, 0, 0, 0, 6, 0, 0, 0, 3),
Array(4, 0, 0, 8, 0, 3, 0, 0, 1),
Array(7, 0, 0, 0, 2, 0, 0, 0, 6),

Array(0, 6, 0, 0, 0, 0, 2, 8, 0),
Array(0, 0, 0, 4, 1, 9, 0, 0, 5),
Array(0, 0, 0, 0, 8, 0, 0, 0, 0)))

def possible(grid: Grid, row: Row, col: Col, n: Int): Boolean = {
val cb = cols.map(grid.get(row, _) == n)
val rb = rows.map(grid.get(_, col) == n)
val bb = (for (i <- 1 to 3; j <- 1 to 3) yield grid.get(Row(row.block + i), Col(col.block + j)) == n).toList
!(cb ++ rb ++ bb).contains(true)
}

def solve(grid: Grid): Unit = {
for (row <- rows; col <- cols; if (grid.get(row, col) == 0)) {
for (n <- 1 to 9; if (possible(grid, row, col, n))) {
grid.set(row, col, n)
solve(grid)
grid.set(row, col, 0)
}
return
}
println(grid)
}

solve(grid)
solve(grid2)
}
Loading