Skip to content

Instantly share code, notes, and snippets.

@heinthanth
Created August 30, 2021 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heinthanth/f46fc8a634c17402965483700b25e26b to your computer and use it in GitHub Desktop.
Save heinthanth/f46fc8a634c17402965483700b25e26b to your computer and use it in GitHub Desktop.
Just mini app ideas
# HIIIiN's todoapp
# line 27 ကို စကြည့်ပါ။
import os, strformat, json, strutils
proc printHelp() =
# ဒါကထွေထွေထူးထူးမရှိပါဘူး။ multiline string ကိုသုံးပြီး output ထုတ်ရုံပဲ။
const helpString = """
todo v1.0.0. Just a simple todo app.
(c) 2021 HIIIiN. Licensed under MIT License.
todo list will view items from todo list.
todo add "learn to code" will add item to list.
todo mark [id] will mark item as done
todo remove [id] will remove item from list
"""
stdout.writeLine(helpString)
type
TodoItem = object
description: string # ဒါက item description
isComplete: bool # ဒါက ပြီးမပြီး
var todoLists: seq[TodoItem] # ဒါက data သိမ်းဖို့ global variable ပေါ့။ lists of todo item object.
when isMainModule: # ဒါက main procedure <- program စတာနဲ့ စ run မှာ
# program စ run တာနဲ့ todoLists variable ကို သိမ်းထားတဲ့ file ကနေ ဖတ်မယ်။ မရှိရင် မဖတ်ဘူးပေါ။
if fileExists("lists.json"):
# parse json to object, json unmarshalling လို့ရှာကြည့်
let jsonStringFromFile = readFile("lists.json")
let jsonNodes = parseJson(jsonStringFromFile)
for item in jsonNodes:
todoLists.add(to(item, TodoItem)) # ဒါက cast လိုက်တဲ့သဘောပဲ​။ python3 တို့မှာ ပိုလွယ်လိမ့်မယ်။
# nim မှာ တခြား language တွေနဲ့မတူတဲ့အချက်က arguments len ကို 1 လျှော့ထားတယ်။
# တခြား language မှာ todo add "hello" ကို argc 3 items ပေးတယ်။
# nim မှာက program name ကိုဖျောက်ပြီး argc 2 items ပဲပေးတယ်။
let argumentsLength: int = paramCount()
# အရင်ဆုံး ငါတို့ program ကို အရင်စဉ်းစားရအောင်။
# -> todo add "learn to code" ဆိုရင် todo lists ထဲထည့်မယ်။
# -> todo list ဆိုရင် todo lists ထဲမှာရှိတဲ့ဟာတွေကို ပြပေးမယ်။
# -> todo remove [id] ဆိုရင် item ကို id နဲ့ဖျက်မယ်။
# -> todo done [id] ဆိုရင် item ကို ပြီးသွားပြီးလို့မှတ်မယ်။
# အဲ့တော့ ငါတို့ program အလုပ်လုပ်ဖို့ဆိုရင် argument က 1ခု (todo list) ကနေ 2ခု (todo create "something") ထိလိုတယ်။ အဲ့တာကို အရင် စစ်ရအောင်။
if argumentsLength < 1 or argumentsLength > 2:
printHelp(); quit(1) # argument လိုအပ်သလောက် မရရင် help message ပြမယ်။ line 6 ကိုကြည့်။
# ဒီမှာဆို argument က ၁ ခုကနေ ၂ ခုပဲရှိတယ်။ မရှိရင် code က ဒီကို ဆက်မရောက်တော့ဘူး if ထဲက quit() ကြောင့် ....
# ဒီတော့ argv[1] ဟာကြမ်းသေ ရှိတာပေါ့။ အဲ့တော့ သူ့ကို case နဲ့ စစ်ရအောင်။
case paramStr(1) # ဒါက တခြား language မှာ argv[1] နဲ့တူတယ်။
of "add":
# add မယ်ဆိုတော့ add ရဲ့ usage က todo add description
# argv[1] -> "add", argv[2] -> must be a description
if argumentsLength != 2 or paramStr(2) == "":
# argument ၂ ခု ( တခြားမှာ ၃ ခု ) မရှိတာဖြစ်ဖြစ်၊ empty ပေးခဲ့တာဖြစ်ဖြစ် error ပြမယ်။
echo "\nOops, description is required!\n"
quit(1)
# အားလုံး fine ပြီးဆိုတော့ item ထည့်ပေါ့။
todoLists.add(TodoItem(description: paramStr(2), isComplete: false))
of "list":
# list မယ်ဆိုတော့ todo lists global variable ကို loop ပြီးပြရုံပဲပေါ့။
if todoLists.len() == 0:
echo "\nOops, no item here!\n"
quit(0) # ဒါကတော့ success မို့ 0, ကျန်တာက error မို့ exit code 1
stdout.writeLine("")
for index, item in todoLists:
if item.isComplete:
# complete ဖြစ်ရင် ​[x] ဆိုပြီးပြမယ်။
echo fmt"{index + 1}. [x] {item.description}"
else:
echo fmt"{index + 1}. [ ] {item.description}"
stdout.writeLine("")
of "remove":
# remove တာဆိုလည်း id ကို ယူ .. ပြီးရင် index နဲ့ဖျက်လိုက်ရုံပဲလေ။
if argumentsLength != 2 or paramStr(2) == "":
# argument ၂ ခု ( တခြားမှာ ၃ ခု ) မရှိတာဖြစ်ဖြစ်၊ empty ပေးခဲ့တာဖြစ်ဖြစ် error ပြမယ်။
echo "\nOops, item id is required!\n"
quit(1)
try:
let id = parseInt(paramStr(2)) - 1
todoLists.delete(id)
except ValueError:
echo "\nOops, id is not a number!\n"
quit(1)
of "done":
# mark as done ဆိုလည်း id ယူ။ index နဲ့ပြင်ရုံပဲ။
if argumentsLength != 2 or paramStr(2) == "":
# argument ၂ ခု ( တခြားမှာ ၃ ခု ) မရှိတာဖြစ်ဖြစ်၊ empty ပေးခဲ့တာဖြစ်ဖြစ် error ပြမယ်။
echo "\nOops, item id is required!\n"
quit(1)
try:
let id = parseInt(paramStr(2)) - 1
todoLists[id].isComplete = true
except ValueError:
echo "\nOops, id is not a number!\n"
quit(1)
# ကဲ ... ဟို global object ကို file ထဲသိမ်းရအောင်။ object to json, json marshalling လို့ရှာကြည့်။
let jsonStringToSave: string = $(%*todoLists) # ဒါက object to json ပြောင်းတဲ့ function, python3 မှာလည်း ရှိလိမ့်မယ်။
writeFile("lists.json", jsonStringToSave) # ဒါက file ထဲ save တာ ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment