Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. >> cat talist.nim
  2. from os import fileExists
  3. import db_sqlite, prompt, strutils
  4.  
  5. let db_check = fileExists("tmp/test.db")
  6.  
  7. # create the file if it doesn't exist, and initialize it.
  8. if not db_check:
  9. let db = open("tmp/test.db", "", "", "")
  10.  
  11. db.exec(sql"CREATE TABLE items ( id INTEGER PRIMARY KEY, name VARCHAR(500) NOT NULL , label VARCHAR(100) NOT NULL )")
  12. db.exec(sql"CREATE TABLE lists ( name VARCHAR(100) NOT NULL )")
  13. db.exec(sql"CREATE TABLE due_dates ( date real NOT NULL )")
  14.  
  15. db.exec(sql"INSERT INTO lists (name) VALUES (?)", "To-Do")
  16. db.exec(sql"INSERT INTO lists (name) VALUES (?)", "To-Do Today")
  17. db.exec(sql"INSERT INTO lists (name) VALUES (?)", "Done")
  18.  
  19. db.exec(sql"INSERT INTO items (name, label) VALUES (?, ?)", "Finish Note Thing", "To-Do")
  20.  
  21. db.close()
  22.  
  23. let db = open("tmp/test.db", "", "", "")
  24.  
  25. #var output = db.getAllRows(sql"SELECT name FROM sqlite_master WHERE type='table'")
  26. #var output = db.getAllRows(sql"SELECT * FROM items")
  27. #
  28. #for val in output:
  29. # echo val
  30.  
  31. var lists = db.getAllRows(sql"SELECT name FROM lists")
  32. var index = 0
  33.  
  34. proc isInt(value: string): bool =
  35. var val = true
  36.  
  37. try:
  38. discard parseInt(value)
  39. except ValueError:
  40. val = false
  41.  
  42. return val
  43.  
  44. proc printBox(name: string) =
  45. var ind = 0
  46. var line = "---------------"
  47. echo line
  48. echo name
  49. echo line
  50.  
  51. var items = db.getAllRows(sql"SELECT name FROM items WHERE label=(?)", lists[index][0])
  52.  
  53. for i in items:
  54. echo line
  55. echo $ind & ". " & i[0]
  56. ind = ind + 1
  57.  
  58. echo line
  59.  
  60. proc readEntry(entry: string): int =
  61. if entry == "h":
  62. if index == 0:
  63. return 0
  64. else:
  65. index = index - 1
  66. return index
  67. elif entry == "l":
  68. if index == len(lists) - 1:
  69. return index
  70. else:
  71. index = index + 1
  72. return index
  73. elif entry == "q":
  74. quit(1)
  75. elif isInt(entry):
  76. return parseInt(entry)
  77. else:
  78. return 0
  79.  
  80. proc main() =
  81. var input = ""
  82. var prompt = Prompt.init(promptIndicator = ">> ")
  83.  
  84. prompt.showPrompt()
  85.  
  86. while true:
  87. prompt.clear()
  88. echo "\nhelp: \n"
  89.  
  90. printBox(lists[index][0])
  91.  
  92. let input = prompt.readLine()
  93. let entry = input
  94.  
  95. discard readEntry(entry)
  96.  
  97. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement