Module: Liste

Defined in:
lib/liste.rb,
lib/liste/version.rb

Overview

Functions used in liste Daniel Ethridge

Constant Summary collapse

VERSION =
"2.0.0"

Instance Method Summary collapse

Instance Method Details

#add(listname, content, path = "#{Dir.home}/.liste/main.list") ⇒ Object

Make a list item and add to the json list file



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/liste.rb', line 41

def add(listname, content, path="#{Dir.home}/.liste/main.list")
  require 'json'
  file = File.open("#{path}", 'r')
  listfile = file.read
  file.close
  listhash = JSON.parse(listfile)
  if ! listhash.keys.include? listname; newlist(listname) end
  listcontent = listhash[listname]
  i = 0
  while listcontent.keys.include? "l#{i}"
    i += 1
  end
  newcontent = { "l#{i}" => content }
  listcontent.merge!(newcontent)
  newjson = JSON.pretty_generate(listhash)
  file = File.open("#{path}", 'w')
  file.puts newjson
  file.close
end

#disp(listname, style) ⇒ Object

Pretty-prints a list’s contents



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/liste.rb', line 77

def disp(listname, style)
  bullet = "\u2022"
  check = "\u2714"
  linesout = putlines(listname)
  case style
    when "bullet"
    linesout.each do |line|
      puts " #{bullet} #{line}"
    end
    when "check"
      linesout.each do |line|
        puts " #{check} #{line}"
      end
    when "number"
      linesout.each.with_index do |line, i|
        puts "#{i+1}. #{line}"
      end
  end
  print "\n"
end

#edit_listObject

Shortcut the manually edit a list



99
100
101
# File 'lib/liste.rb', line 99

def edit_list()
  system "editor #{Dir.home}/.liste/main.list" rescue edit_list()
end

#initObject

Creates a new list



7
8
9
10
11
12
13
14
# File 'lib/liste.rb', line 7

def init()
  unless File.directory?("#{Dir.home}/.liste")
    require 'fileutils'
    FileUtils.mkdir_p("#{Dir.home}/.liste")
  end
  File.open("#{Dir.home}/.liste/main.list", "w")
  puts "New list created in '#{Dir.home}/.liste/main.list'\n\n"
end

#newlist(listname, content = "This is a placeholder", path = "#{Dir.home}/.liste/main.list") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/liste.rb', line 25

def newlist(listname, content="This is a placeholder", path="#{Dir.home}/.liste/main.list")
  require 'json'
  file = File.open("#{path}", 'r')
  listfile = file.read
  file.close
  if listfile.nil? || listfile.empty?; newlistfile(listname, content, path); exit end
  listhash = { :"#{listname}" => {:l0 => content } }
  hash = JSON.parse(listfile)
  newhash = hash.merge!(listhash)
  json = JSON.pretty_generate(newhash)
  file = File.open("#{path}", 'w')
  file.puts json
  file.close
end

#newlistfile(listname, content, path) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/liste.rb', line 16

def newlistfile(listname, content, path)
  hash = { :"#{listname}" => {:l0 => content} }
  json = JSON.pretty_generate(hash)
  file = File.open("#{path}", 'w')
  file.puts json
  file.close
  puts "new list '#{listname}' created"
end

#putlines(listname) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/liste.rb', line 61

def putlines(listname)
  require 'json'
  file = File.open("#{Dir.home}/.liste/main.list") rescue init()
  listfile = file.read
  listhash = JSON.parse(listfile)
  linesout = []
  i = 0
  while i < listhash[listname].count
    line = listhash[listname]["l#{i}"]
    linesout << "#{line}"
    i += 1
  end
  linesout
end