Add localize on/off command

This commit is contained in:
2023-06-10 13:21:51 +09:00
parent 3a15a054c5
commit f37b797e6e
5 changed files with 47 additions and 11 deletions

View File

@@ -156,7 +156,7 @@ func valueToString(_ any: Any) -> String {
extension Array where Element: PropertyIterable {
func writeCsv(toFile file: URL, appendable: Bool = false, localized: Bool = false) throws {
func writeCsv(toFile file: URL, appendable: Bool = false, localized: Bool) throws {
if appendable, file.isFileExists == true {
try appendAtEnd(ofCsv: file)
}

View File

@@ -84,7 +84,7 @@ extension KissConsole {
}
let fileUrl = KissConsole.candleFileUrl(productNo: productNo, period: period.filePeriod, day: maxTime)
try candles.writeCsv(toFile: fileUrl)
try candles.writeCsv(toFile: fileUrl, localized: localized)
print("wrote \(fileUrl.lastPathComponent) with \(candles.count)")
return true
@@ -150,7 +150,7 @@ extension KissConsole {
}
let fileUrl = KissConsole.candleFileUrl(productNo: productNo, period: .minute, day: maxTime)
try candles.writeCsv(toFile: fileUrl)
try candles.writeCsv(toFile: fileUrl, localized: localized)
print("wrote \(fileUrl.lastPathComponent) with \(candles.count)")
return true
} catch {

View File

@@ -15,10 +15,18 @@ class KissConsole {
private var shop: KissShop? = nil
private var productsLock = NSLock()
///
private var products = [String: [DomesticShop.Product]]()
/// productNo
private var currentShortCode: String?
/// candle productNo
var currentCandleShortCode: String?
private var localNames = [String: LocalName]()
/// CSV , field name (true) (false)
var localized: Bool = false
private enum KissCommand: String {
case quit = "quit"
@@ -62,6 +70,7 @@ class KissConsole {
//
case localizeNames = "localize names"
case localizeOnOff = "localize"
var needLogin: Bool {
@@ -80,7 +89,7 @@ class KissConsole {
return false
case .showcase:
return false
case .loves, .love, .hate, .localizeNames:
case .loves, .love, .hate, .localizeNames, .localizeOnOff:
return false
}
}
@@ -204,6 +213,7 @@ class KissConsole {
case .love: await onLove(args)
case .hate: await onHate(args)
case .localizeNames: await onLocalizeNames()
case .localizeOnOff: await onLocalizeOnOff(args)
default:
print("Unknown command: \(line)")
@@ -362,7 +372,7 @@ extension KissConsole {
print("\(item.dataRank) \(item.shortProductNo) \(item.htsProductName.maxSpace(20)) \(item.currentStockPrice.maxSpace(10, digitBy: 3)) \(item.averageVolume.maxSpace(15, digitBy: 3)) \(item.accumulatedTradingAmount.maxSpace(25, digitBy: 3))")
}
let fileUrl = KissConsole.topProductsUrl(belongClass)
try output.writeCsv(toFile: fileUrl)
try output.writeCsv(toFile: fileUrl, localized: localized)
print("wrote \(fileUrl.lastPathComponent) with \(output.count)")
} catch {
print("\(error)")
@@ -526,7 +536,7 @@ extension KissConsole {
}
let fileUrl = KissConsole.accountStocksUrl
try stocks.writeCsv(toFile: fileUrl)
try stocks.writeCsv(toFile: fileUrl, localized: localized)
print("wrote \(fileUrl.lastPathComponent) with \(stocks.count)")
}
@@ -535,7 +545,7 @@ extension KissConsole {
print("\(item)")
}
let fileUrl = KissConsole.accountAmountUrl
try amounts.writeCsv(toFile: fileUrl)
try amounts.writeCsv(toFile: fileUrl, localized: localized)
print("wrote \(fileUrl.lastPathComponent) with \(amounts.count)")
}
} catch {
@@ -583,7 +593,7 @@ extension KissConsole {
let fileUrl = KissConsole.productPriceUrl(productNo: productNo)
try [output].writeCsv(toFile: fileUrl, appendable: true, localized: true)
try [output].writeCsv(toFile: fileUrl, appendable: true, localized: localized)
} catch {
print("\(error)")
@@ -814,7 +824,7 @@ extension KissConsole {
do {
let fileUrl = KissConsole.shopProductsUrl
try shopItems.writeCsv(toFile: fileUrl)
try shopItems.writeCsv(toFile: fileUrl, localized: localized)
print("wrote \(fileUrl.lastPathComponent) with \(shopItems.count)")
} catch {
print(error)
@@ -982,13 +992,32 @@ extension KissConsole {
}
if addedNameCount > 0 {
do {
try curNames.writeCsv(toFile: nameUrl)
try curNames.writeCsv(toFile: nameUrl, localized: localized)
} catch {
print(error)
}
}
print("Success \(nameUrl.lastPathComponent) total: \(curNames.count), new: \(addedNameCount)")
}
private func onLocalizeOnOff(_ args: [String]) async {
guard args.count == 1 else {
print("Missing on/off option")
return
}
guard let option = OnOff(rawValue: args[0]) else {
print("Invalid on/off option")
return
}
switch option {
case .on:
localized = true
case .off:
localized = false
}
print("Localization \(option.rawValue)")
}
}

View File

@@ -47,3 +47,9 @@ struct LocalContext {
func localizeString(_ str: String) -> String {
return LocalContext.shared.localNamesDic[str]?.localizedName ?? str
}
enum OnOff: String {
case on
case off
}

View File

@@ -40,6 +40,7 @@ WIP `showcase` | 추천 상품을 제안함.
`love (탭).(번호) (PNO)` | 관심 종목에 추가함. (번호) 를 지정하지 않으면 (탭) 마지막에 추가함.
`hate (탭) (PNO)` | 관심 종목에서 삭제함.
`localize names` | csv field name 에 대해서 한글명을 제공하는 **data/localized-names.csv** 를 저장.
`localize (on|off)`| 앞으로 저장하는 모든 csv file 의 field 에 (on) 이면 한글명으로, (off) 이면 영문으로 저장.
* PNO 는 `Product NO` 의 약자이고, 상품의 `단축코드` (shortCode) 와 동일합니다.
* ONO 는 `Order NO` 의 약자이고, 고유한 주문번호 입니다.