Skip to content

Commit

Permalink
Formatted with nph (#2246)
Browse files Browse the repository at this point in the history
  • Loading branch information
fox0430 authored Feb 16, 2025
1 parent d16e8a2 commit 553e082
Show file tree
Hide file tree
Showing 208 changed files with 26,248 additions and 29,013 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. _#2246: https://github.com/fox0430/moe/pull/2246

Changed
.......

- `#2246`_ Formatted with nph

3 changes: 2 additions & 1 deletion src/moe.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ proc main() =

status.get.exitEditor

when isMainModule: main()
when isMainModule:
main()
8 changes: 2 additions & 6 deletions src/moepkg/algo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ proc fuzzyScore*(s1, s2: Runes): int =
matrix[i][j] = 0
else:
let
score =
if s1[i - 1] == s2[j - 1]: MatchScore
else: MismatchPenalty
score = if s1[i - 1] == s2[j - 1]: MatchScore else: MismatchPenalty
match = matrix[i - 1][j - 1] + score
delete = matrix[i - 1][j] + GapPenalty
insert = matrix[i][j - 1] + GapPenalty
Expand Down Expand Up @@ -69,9 +67,7 @@ proc fuzzyScore*(s1, s2: Runes): int =
diagonalScore = matrix[maxI - 1][maxJ - 1]
leftScore = matrix[maxI][maxJ - 1]

let score =
if s1[maxI - 1] == s2[maxJ - 1]: MatchScore
else: MismatchPenalty
let score = if s1[maxI - 1] == s2[maxJ - 1]: MatchScore else: MismatchPenalty
if currentScore == diagonalScore + score:
alignedS1.add s1[maxI - 1]
alignedS2.add s2[maxJ - 1]
Expand Down
24 changes: 13 additions & 11 deletions src/moepkg/appinfo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import std/[strformat, strutils, os, pegs]
import pkg/results

type
VersionInfo* = object
major*, minor*, patch*: int

type VersionInfo* = object
major*, minor*, patch*: int

proc toSemVerString*(v: VersionInfo): string {.inline.} =
fmt"{v.major}.{v.minor}.{v.patch}"
Expand All @@ -34,11 +32,13 @@ proc parseVersionInfo*(s: string): Result[VersionInfo, string] =
return Result[VersionInfo, string].ok VersionInfo(
major: strSplit[0].parseInt,
minor: strSplit[1].parseInt,
patch: strSplit[2].parseInt)
patch: strSplit[2].parseInt,
)
except:
return Result[VersionInfo, string].err fmt"Invalid value: {getCurrentExceptionMsg()}"
return
Result[VersionInfo, string].err fmt"Invalid value: {getCurrentExceptionMsg()}"

proc staticReadVersionFromNimble: string {.compileTime.} =
proc staticReadVersionFromNimble(): string {.compileTime.} =
## Get the moe version from moe.nimble.

let
Expand All @@ -58,7 +58,7 @@ proc moeVersion*(): VersionInfo {.compileTime.} =
proc moeSemVersionStr*(): string {.compileTime.} =
moeVersion().toSemVerString

proc staticGetGitHash: string {.compileTime.} =
proc staticGetGitHash(): string {.compileTime.} =
## Get the current git hash.

const CmdResult = gorgeEx("git rev-parse HEAD")
Expand All @@ -70,8 +70,10 @@ proc staticGetGitHash: string {.compileTime.} =
proc gitHash*(): string {.compileTime.} =
staticGetGitHash()

proc buildType*: string {.compileTime.} =
proc buildType*(): string {.compileTime.} =
## Return the build type. "Release" or "Debug".

if defined(release): return "Release"
else: return "Debug"
if defined(release):
return "Release"
else:
return "Debug"
46 changes: 23 additions & 23 deletions src/moepkg/backgroundprocess.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,38 @@ type

StartProcessResult* = Result[BackgroundProcess, string]

proc isRunning*(bp: BackgroundProcess): bool {.inline.} = bp.process.running
proc isRunning*(bp: BackgroundProcess): bool {.inline.} =
bp.process.running

proc isFinish*(bp: BackgroundProcess): bool {.inline.} = not bp.process.running
proc isFinish*(bp: BackgroundProcess): bool {.inline.} =
not bp.process.running

proc cancel*(bp: BackgroundProcess) = bp.process.terminate
proc cancel*(bp: BackgroundProcess) =
bp.process.terminate

proc kill*(bp: BackgroundProcess) = bp.process.kill
proc kill*(bp: BackgroundProcess) =
bp.process.kill

proc close*(bp: BackgroundProcess) = bp.process.close
proc close*(bp: BackgroundProcess) =
bp.process.close

proc outputStream*(bp: BackgroundProcess): Stream = bp.process.outputStream
proc outputStream*(bp: BackgroundProcess): Stream =
bp.process.outputStream

proc startBackgroundProcess*(
command: BackgroundProcessCommand): StartProcessResult =
## Start the passed command in a new process and return BackgroundProcess.
proc startBackgroundProcess*(command: BackgroundProcessCommand): StartProcessResult =
## Start the passed command in a new process and return BackgroundProcess.

const
Env = nil
Options = {poUsePath, poDaemon, poStdErrToStdOut}
const
Env = nil
Options = {poUsePath, poDaemon, poStdErrToStdOut}

var process: Process
try:
process = startProcess(
command.cmd,
command.workingDir,
command.args,
Env,
Options)
except OSError as e:
return StartProcessResult.err fmt"Failed to create a background process: {e.msg}"
var process: Process
try:
process = startProcess(command.cmd, command.workingDir, command.args, Env, Options)
except OSError as e:
return StartProcessResult.err fmt"Failed to create a background process: {e.msg}"

return StartProcessResult.ok BackgroundProcess(process: process)
return StartProcessResult.ok BackgroundProcess(process: process)

proc result*(bp: var BackgroundProcess): Result[seq[string], string] =
## Return results (Stdout) the BackgroundProcess and close the process.
Expand Down
132 changes: 63 additions & 69 deletions src/moepkg/backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

import std/[os, times, oids, json]
import pkg/results
import settings, unicodeext, fileutils, bufferstatus, gapbuffer, messages,
commandline
import settings, unicodeext, fileutils, bufferstatus, gapbuffer, messages, commandline

type
AutoBackupStatus* = object
lastBackupTime*: DateTime
type AutoBackupStatus* = object
lastBackupTime*: DateTime

proc initAutoBackupStatus*(): AutoBackupStatus {.inline.} =
result.lastBackupTime = now()
Expand All @@ -37,27 +35,30 @@ proc createDir(dir: Runes): bool =
if dirExists($dir):
return true
else:
try: createDir($dir)
except CatchableError: return false
try:
createDir($dir)
except CatchableError:
return false

return true

# Return true if valid json.
proc validateBackupInfoJson*(jsonNode: JsonNode): bool =
jsonNode.contains("path") and
jsonNode["path"].kind == JsonNodeKind.JString and
jsonNode["path"].getStr.len > 0
jsonNode.contains("path") and jsonNode["path"].kind == JsonNodeKind.JString and
jsonNode["path"].getStr.len > 0

# Return path of backupDir.
# Return empty Runes if error or isn't exist.
proc getBackupDir*(baseBackupDir, sourceFilePath: Runes): Runes =
if not dirExists($baseBackupDir):
return "".toRunes

for file in walkPattern($baseBackupDir / "*/backup.json" ):
for file in walkPattern($baseBackupDir / "*/backup.json"):
let backupJson =
try: json.parseFile(file)
except CatchableError: return "".toRunes
try:
json.parseFile(file)
except CatchableError:
return "".toRunes

if validateBackupInfoJson(backupJson):
if backupJson["path"].getStr == $sourceFilePath:
Expand All @@ -76,10 +77,12 @@ proc validateBackupFileName*(filename: string): bool =
# Return the backup dir for `sourceFilePath`.
# `sourceFilePath` is need to absolute path.
proc backupDir*(baseBackupDir, sourceFilePath: string): string =
for jsonFilePath in walkPattern($baseBackupDir / "*/backup.json" ):
for jsonFilePath in walkPattern($baseBackupDir / "*/backup.json"):
let backupJson =
try: json.parseFile(jsonFilePath)
except CatchableError: return ""
try:
json.parseFile(jsonFilePath)
except CatchableError:
return ""

if validateBackupInfoJson(backupJson):
if backupJson["path"].getStr == sourceFilePath:
Expand Down Expand Up @@ -116,7 +119,8 @@ proc initBackupDir(baseBackupDir, sourceFilePath: Runes): Runes =
return backupDir

# Return the filename for the backup.
template genFilename(): Runes = now().toRunes
template genFilename(): Runes =
now().toRunes

# Return true if the buffer changed after the previous backup.
proc diff(baseBackupDir, sourceFilePath: Runes, buffer: string): bool =
Expand All @@ -140,16 +144,15 @@ proc diff(baseBackupDir, sourceFilePath: Runes, buffer: string): bool =

# Return true if successful.
proc writeBackupFile(
path, buffer: Runes,
encoding: CharacterEncoding): bool {.inline.} =

return saveFile(path, buffer, encoding).isOk
path, buffer: Runes, encoding: CharacterEncoding
): bool {.inline.} =
return saveFile(path, buffer, encoding).isOk

# Return true if successful.
# Save json file for backup info in the same dir of backup files.
proc writeBackupInfoJson(backupDir, sourceFilePath: Runes): bool =
# `path` is the absolute path of backup source file.
let jsonNode = %* { "path": $sourceFilePath }
let jsonNode = %*{"path": $sourceFilePath}

try:
writeFile($backupInfoJsonPath(backupDir), $jsonNode)
Expand All @@ -161,57 +164,48 @@ proc writeBackupInfoJson(backupDir, sourceFilePath: Runes): bool =
# Backup buffer to {autoBackupStatus.backupDir}/{id}/.
# Ignore if there is no change from the previous backup.
proc backupBuffer*(
bufStatus: BufferStatus,
autoBackupSettings: AutoBackupSettings,
notificationSettings: NotificationSettings,
commandLine: var CommandLine) =

if bufStatus.path.len == 0: return
bufStatus: BufferStatus,
autoBackupSettings: AutoBackupSettings,
notificationSettings: NotificationSettings,
commandLine: var CommandLine,
) =
if bufStatus.path.len == 0:
return

let
sourceFilePath = absolutePath($bufStatus.path)
sourceFileDir = (sourceFilePath.splitPath).head
dirToExclude = autoBackupSettings.dirToExclude

if dirToExclude.contains(ru sourceFileDir):
return

let
baseBackupDir = autoBackupSettings.backupDir
backupFilename = genFilename()

let backupDir = initBackupDir(baseBackupDir, sourceFilePath.toRunes)
if backupDir.len == 0:
commandLine.writeAutoBackupFailedMessage(backupFilename, notificationSettings)
return

let isSame = diff(baseBackupDir, sourceFilePath.toRunes, bufStatus.buffer.toString)
if not isSame:
commandLine.writeStartAutoBackupMessage(notificationSettings)

let
sourceFilePath = absolutePath($bufStatus.path)
sourceFileDir = (sourceFilePath.splitPath).head
dirToExclude = autoBackupSettings.dirToExclude
backupFilePath = backupDir / backupFilename
buffer = bufStatus.buffer.toRunes
encoding = bufStatus.characterEncoding

if dirToExclude.contains(ru sourceFileDir): return

let
baseBackupDir = autoBackupSettings.backupDir
backupFilename = genFilename()

let backupDir = initBackupDir(baseBackupDir, sourceFilePath.toRunes)
if backupDir.len == 0:
commandLine.writeAutoBackupFailedMessage(
backupFilename,
notificationSettings)
if not writeBackupFile(backupFilePath, buffer, encoding):
commandLine.writeAutoBackupFailedMessage(backupFilename, notificationSettings)
return

let isSame = diff(
baseBackupDir,
sourceFilePath.toRunes,
bufStatus.buffer.toString)
if not isSame:
commandLine.writeStartAutoBackupMessage(notificationSettings)

let
backupFilePath = backupDir / backupFilename
buffer = bufStatus.buffer.toRunes
encoding = bufStatus.characterEncoding

if not writeBackupFile(backupFilePath, buffer, encoding):
commandLine.writeAutoBackupFailedMessage(
backupFilename,
notificationSettings)
if not fileExists($backupInfoJsonPath(backupDir)):
if not writeBackupInfoJson(backupDir, sourceFilePath.toRunes):
commandLine.writeAutoBackupFailedMessage(backupFilename, notificationSettings)
return

if not fileExists($backupInfoJsonPath(backupDir)):
if not writeBackupInfoJson(backupDir, sourceFilePath.toRunes):
commandLine.writeAutoBackupFailedMessage(
backupFilename,
notificationSettings)
return

let message = "Automatic backup successful: " & $backupFilePath
commandLine.writeAutoBackupSuccessMessage(
message,
notificationSettings)
let message = "Automatic backup successful: " & $backupFilePath
commandLine.writeAutoBackupSuccessMessage(message, notificationSettings)
Loading

0 comments on commit 553e082

Please sign in to comment.