WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit ba1b277

Browse files
committed
refactor: Format by nph
1 parent b420eaa commit ba1b277

File tree

11 files changed

+59
-104
lines changed

11 files changed

+59
-104
lines changed

src/gigi.nim

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import
2-
gigipkg/gitignore
3-
1+
import gigipkg/gitignore
42

53
export gitignore
64

7-
85
when isMainModule:
9-
import
10-
gigipkg/command
11-
6+
import gigipkg/command
127

138
quit(main())

src/gigipkg/cache.nim

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
## Cache controller for API response
2-
import
3-
std/[os, options, times],
4-
./utils
5-
2+
import std/[os, options, times], ./utils
63

74
const DEFAULT_EXPIRE_DAYS = 7
85
const DEFAULT_CACHE_FILE = "list.json"
96

10-
117
proc getAppCacheDir*(): string =
128
## Get directory fullpath of application cache
139
return getCacheDir() & DirSep & getAppName()
1410

15-
1611
proc saveCache(path, content: string) =
1712
path.parentDir().createDir()
1813
path.writeFile(content)
1914

20-
2115
proc saveCache*(content: string) =
2216
let cachePath = getAppCacheDir() & DirSep & DEFAULT_CACHE_FILE
2317
cachePath.saveCache(content)
2418

25-
26-
proc loadCache*(src: string, ts: Time, expireDays: int = DEFAULT_EXPIRE_DAYS): Option[string] =
19+
proc loadCache*(
20+
src: string, ts: Time, expireDays: int = DEFAULT_EXPIRE_DAYS
21+
): Option[string] =
2722
## Read cache content if it is exists and is before expired.
2823
if not src.fileExists():
2924
return none(string)
@@ -36,7 +31,6 @@ proc loadCache*(src: string, ts: Time, expireDays: int = DEFAULT_EXPIRE_DAYS): O
3631

3732
return some(src.readFile())
3833

39-
4034
proc loadCache*(expireDays: int = DEFAULT_EXPIRE_DAYS): Option[string] =
4135
## Simply using of ``loadCache``.
4236
## - find default cache path

src/gigipkg/command.nim

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
import
22
std/[json, options, os, parseopt, strutils, terminal],
33
pkg/puppy,
4-
./info, ./parser, ./utils, ./webapi,
4+
./info,
5+
./parser,
6+
./utils,
7+
./webapi,
58
./subcommands/create
69

7-
8-
type
9-
Options = ref object of RootObj
10-
command: string
11-
targets: seq[string]
12-
## Command targets
13-
dest: Option[string]
14-
## If value is set, out into file fo dest instead of STDOUT
15-
10+
type Options = ref object of RootObj
11+
command: string
12+
targets: seq[string] ## Command targets
13+
dest: Option[string] ## If value is set, out into file fo dest instead of STDOUT
1614

1715
proc parseArgs(params: seq[string]): Options =
1816
result = Options(command: "create", targets: @[], dest: none(string))
1917
var p = initOptParser(params)
2018
while true:
2119
p.next()
22-
case p.kind:
23-
of cmdEnd: break
24-
of cmdLongOption, cmdShortOption:
25-
if p.key == "h" or p.key == "help":
26-
result.command = "help"
27-
break
28-
if p.key == "o" or p.key == "out":
29-
result.dest = some(p.val)
30-
of cmdArgument:
31-
result.targets.add(p.key)
32-
20+
case p.kind
21+
of cmdEnd:
22+
break
23+
of cmdLongOption, cmdShortOption:
24+
if p.key == "h" or p.key == "help":
25+
result.command = "help"
26+
break
27+
if p.key == "o" or p.key == "out":
28+
result.dest = some(p.val)
29+
of cmdArgument:
30+
result.targets.add(p.key)
3331

3432
proc main*(): int =
3533
result = 1
36-
let
37-
options = parseArgs(commandLineParams())
38-
var
39-
targets = deepCopy(options.targets)
34+
let options = parseArgs(commandLineParams())
35+
var targets = deepCopy(options.targets)
4036

4137
if options.command == "help":
4238
result = 0
4339
stdout.writeLine(PKG_NAME & " version " & PKG_VERSION)
44-
stdout.writeLine("Usage: " & getAppName() & " [-h/--help] [-o/--out output] (TARGETS)")
40+
stdout.writeLine(
41+
"Usage: " & getAppName() & " [-h/--help] [-o/--out output] (TARGETS)"
42+
)
4543
stdout.writeLine("")
4644
stdout.writeLine(" -h/--help Display help text")
4745
stdout.writeLine(" -o/--out Write output instead of STDOUT")
@@ -56,23 +54,23 @@ proc main*(): int =
5654

5755
if targets.len == 0:
5856
stderr.writeLine("No targets is specified.")
59-
6057

6158
try:
6259
let
6360
content = fetchContentOrCache()
6461
templates = parseGitignoreTable(content.parseJson())
65-
var
66-
strm =
67-
if isNone(options.dest):
68-
stdout
69-
else:
70-
open(options.dest.get(), fmWrite, -1)
62+
var strm =
63+
if isNone(options.dest):
64+
stdout
65+
else:
66+
open(options.dest.get(), fmWrite, -1)
7167
result = strm.outputGitignore(templates, targets)
7268
except PuppyError:
7369
let ex = getCurrentException()
7470
stderr.writeLine("Failured to fetch ignore templates.")
75-
stderr.writeLine("Please see error messages and check your environment if you need.")
71+
stderr.writeLine(
72+
"Please see error messages and check your environment if you need."
73+
)
7674
stderr.writeLine("Message: " & ex.msg)
7775
return
7876

src/gigipkg/gitignore.nim

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
## Definition for gitignore template
2-
import
3-
std/[strutils, tables]
4-
2+
import std/[strutils, tables]
53

64
type
7-
Gitignore* = ref object of RootObj
8-
## Gigitnore entity of gigi
5+
Gitignore* = ref object of RootObj ## Gigitnore entity of gigi
96
name: string
107
contents: seq[string]
118

12-
GitignoreTable* = TableRef[string, Gitignore]
13-
## It is alias
14-
9+
GitignoreTable* = TableRef[string, Gitignore] ## It is alias
1510

1611
proc name*(self: Gitignore): string =
1712
return self.name
1813

19-
2014
proc newGitignore*(name: string, contents: seq[string]): Gitignore =
2115
result = Gitignore()
2216
result.name = name
2317
result.contents = contents
2418

25-
2619
proc output*(self: Gitignore): string =
2720
## Return gitignore body for output.
2821
##
2922
## It is used to write into ``.gitignore``.
3023
return self.contents.join("\n")
3124

32-
3325
proc newGitignoreTable*(): GitignoreTable =
3426
## Create empty table for Gitignore entities.
3527
result = newTable[string, Gitignore]()

src/gigipkg/parser.nim

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import
2-
std/[json, strutils, tables],
3-
./gitignore
4-
1+
import std/[json, strutils, tables], ./gitignore
52

63
proc parseGitignore(src: JsonNode): Gitignore =
74
let name = src["name"].getStr()
85
let contents = src["contents"].getStr("").strip().splitLines()
96
result = newGitignore(name, contents)
107

11-
128
proc parseGitignoreTable*(src: JsonNode): GitignoreTable =
139
result = newGitignoreTable()
1410
for key, tmpl in src.pairs:

src/gigipkg/subcommands/create.nim

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
# =====================
33
#
44
# Output gitignore text.
5-
import
6-
std/[strutils, tables],
7-
../gitignore, ../info
5+
import std/[strutils, tables], ../gitignore, ../info
86

9-
10-
proc outputGitignore*(strm: var File, templates: GitignoreTable, targets: seq[string]): int =
7+
proc outputGitignore*(
8+
strm: var File, templates: GitignoreTable, targets: seq[string]
9+
): int =
1110
## Create gitignore text into STDOUT.
1211
result = 1
1312
strm.writeLine("### " & PKG_NAME & " version " & PKG_VERSION)

src/gigipkg/utils.nim

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import
2-
std/[os, strutils]
3-
1+
import std/[os, strutils]
42

53
proc getAppName*(): string =
64
## Get application stem (filename without ext).

src/gigipkg/webapi.nim

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
## Management access to Web API
2-
import
3-
std/options,
4-
pkg/puppy,
5-
./cache
6-
2+
import std/options, pkg/puppy, ./cache
73

84
const
9-
DEFAULT_WEBAPI_URL* = "https://www.toptal.com/developers/gitignore/api/list?format=json"
10-
DEFAULT_USER_AGENT = "gigi web-client"
11-
5+
DEFAULT_WEBAPI_URL* =
6+
"https://www.toptal.com/developers/gitignore/api/list?format=json"
7+
DEFAULT_USER_AGENT = "gigi web-client"
128

139
proc fetchContent*(url: string = DEFAULT_WEBAPI_URL): string =
1410
## Fetch raw content from Web API.
15-
let headers = @[
16-
Header(key: "User-Agent", value: DEFAULT_USER_AGENT),
17-
]
11+
let headers = @[Header(key: "User-Agent", value: DEFAULT_USER_AGENT)]
1812
return fetch(url, headers = headers)
1913

20-
2114
proc fetchContentOrCache*(url: string = DEFAULT_WEBAPI_URL): string =
2215
## Fetch raw content from Web API. If cache is available, return it.
2316
let cacheContent = loadCache()

tests/test_cache.nim

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import
2-
std/[options, os, tempfiles, times, unittest],
3-
gigipkg/cache {.all.}
4-
1+
import std/[options, os, tempfiles, times, unittest], gigipkg/cache {.all.}
52

63
suite "loadCache":
74
let
@@ -21,7 +18,6 @@ suite "loadCache":
2118
check isSome(cached)
2219
check cached.get() == "content"
2320

24-
2521
suite "saveCache":
2622
let
2723
tmpDir = createTempDir("test_utils--loadCache", "")

tests/test_parser.nim

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
import
2-
std/[json, strutils, tables, unittest],
3-
gigipkg/gitignore, gigipkg/parser {.all.}
1+
import std/[json, strutils, tables, unittest], gigipkg/gitignore, gigipkg/parser {.all.}
42

5-
6-
let data = parseJson("""{
3+
let data = parseJson(
4+
"""{
75
"nim": {
86
"fileName": "Nim.gitignore",
97
"key": "nim",
108
"contents": "\n### Nim ###\nnimcache/\nnimblecache/\nhtmldocs/\n",
119
"name": "Nim"
1210
}
1311
}
14-
""")
15-
12+
"""
13+
)
1614

1715
test "Parse content":
1816
let gi = parseGitignore(data["nim"])
1917
check gi.name == "Nim"
2018
check gi.output.splitLines.len == 4
2119

22-
2320
test "Parse contents":
2421
let table = parseGitignoreTable(data)
2522
check table.len == 1

0 commit comments

Comments
 (0)