Skip to content

Commit

Permalink
??
Browse files Browse the repository at this point in the history
  • Loading branch information
noituri committed Nov 2, 2018
2 parents 575ad0c + 34cd6c0 commit d930c31
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 29 deletions.
47 changes: 34 additions & 13 deletions src/LoveShell.cr
Expand Up @@ -23,13 +23,17 @@ module LoveShell
execute_block = ""
pause = false

aliases = { "ls" => "ls --color=auto", "grep" => "grep --color"} of String => String
aliases = { "ls" => "ls --color=auto", "lsa" => "ls --color=auto -a", "grep" => "grep --color"} of String => String

prompt = Prompt.new
fancy = Fancyline.new
historian = Historian.new
commands = Commands.new


#ARGUMENT PARSING


OptionParser.parse! do |parser|
parser.banner = "Shell made with <3"
parser.on("-x BLOCK", "--execute BLOCK", "Executes the specified code block.") {|block| execute = true; execute_block = block}
Expand All @@ -51,6 +55,9 @@ module LoveShell
end


#COLORING INPUT


fancy.display.add do |ctx, line, yielder|
line = line.gsub(/^\w+/, &.colorize(:light_red).mode(:bold))
line = line.gsub(/(\|\s*)(\w+)/) do
Expand All @@ -64,6 +71,9 @@ module LoveShell
end


#HELP LINE


help_line_enabled = true

fancy.sub_info.add do |ctx, yielder|
Expand All @@ -86,6 +96,10 @@ module LoveShell
lines # Return the lines so far
end


#AUTOCOMPLETION


fancy.autocomplete.add do |ctx, range, word, yielder|
completions = yielder.call(ctx, range, word)
prev_char = ctx.editor.line[ctx.editor.cursor - 1]?
Expand Down Expand Up @@ -120,6 +134,10 @@ module LoveShell
completions
end


#MISC KEYBINDS


#fancy.actions.set Fancyline::Key::Control::AltH do |ctx|
# if command = get_command(ctx)
# system("man #{command}")
Expand All @@ -142,26 +160,29 @@ module LoveShell
#Do Nothing
end

fancy.actions.set Fancyline::Key::Control::CtrlR do |ctx|
#No default history search for you
end


#HISTORY CONTROL


fancy.actions.set Fancyline::Key::Control::Up do |ctx|
while true
break if historian.getEntryUp[0..3] != "#<3#"
if historian.getLength == historian.getPosition + 1
historian.getEntryDown
break
end
end
ctx.editor.line = historian.getCurrentEntry
historian.saveLine(ctx.editor.line) if historian.getPosition == -1
ctx.editor.line = historian.getEntryUp
ctx.editor.move_cursor(ctx.editor.line.size)
end

fancy.actions.set Fancyline::Key::Control::Down do |ctx|
while true
break if historian.getEntryDown[0..3] != "#<3#"
end
ctx.editor.line = historian.getCurrentEntry
ctx.editor.line = historian.getEntryDown
ctx.editor.move_cursor(ctx.editor.line.size)
end


#THE MAIN LOOP


historian.log(%(#<3# Opened LoveShell instance with PID ) + "#{Process.pid}" + " on " + "#{Time.now}")

begin
Expand Down
86 changes: 70 additions & 16 deletions src/historian.cr
Expand Up @@ -2,6 +2,7 @@ class Historian

HISTORY_PATH = "/home/#{Process.user}/.hist.love"
@@position = -1
@@savedLine = ""

def log(message : String)
if message != ""
Expand All @@ -11,30 +12,71 @@ class Historian
end
end

#def getEntryUp : String
# histLength = File.read_lines(HISTORY_PATH).size - 1
# histLog = File.read_lines(HISTORY_PATH).reverse
# @@position += 1
# if @@position > histLength
# @@position = histLength
# end
# out = histLog[@@position].to_s
# out
#end

def getEntryUp : String
histLength = File.read_lines(HISTORY_PATH).size - 1
histLog = File.read_lines(HISTORY_PATH).reverse
@@position += 1
if @@position > histLength
@@position = histLength
unless @@savedLine == ""
while true
@@position += 1
out = histLog[@@position].to_s
break if out[0..@@savedLine.size - 1] == @@savedLine
if getLength == getPosition + 1
@@position -= 1
break
end
end
else
while true
@@position += 1
out = histLog[@@position].to_s
break if out[0..3] != "#<3#"
if getLength == getPosition + 1
@@position -= 1
break
end
end
end
out = histLog[@@position].to_s
out
end

def getEntryDown : String
@@position -= 1
if @@position < 0
@@position = -1
end

if @@position == -1
out = ""
else
histLog = File.read_lines(HISTORY_PATH).reverse
out = histLog[@@position].to_s
histLog = File.read_lines(HISTORY_PATH).reverse
unless @@savedLine == "" #Jeśli mam coś zapisane
while true
@@position -= 1 #Pozycja się zmniejsza
if @@position < 0 #Jeśli jest < 0
@@position = -1 #Upewniam się że zostanie na -1
out = @@savedLine #Upewniam się że w prompcie będę miał to co zapisałem
break #I wychodzę z pętli
end
out = histLog[@@position].to_s #Inaczej biorę to co mam na danej pozycji
break if out[0..@@savedLine.size - 1] == @@savedLine #Sprawdzam czy pasuje do tego co szukam
end
out #Returnuję wpis
else #Inaczej
while true
@@position -= 1 #Pozycja się zmniejsza
if @@position < 0 #Jeśli jest < 0
@@position = -1 #Upewniam się że zostanie na -1
out = "" #Upewniam się że prompt będzie pusty
break #Wychodzę z pętli
end
out = histLog[@@position].to_s
break if out[0..3] != "#<3#"
end
out
end
out
end

def getCurrentEntry : String
Expand All @@ -44,7 +86,7 @@ class Historian
histLog = File.read_lines(HISTORY_PATH).reverse
out = histLog[@@position].to_s
end
out
out
end

def getLength : Int
Expand All @@ -58,4 +100,16 @@ class Historian
def resetPosition
@@position = -1
end

def saveLine(line : String)
@@savedLine = line
end

def loadLine : String
@@savedLine
end

def clearSavedLine
@@savedLine = ""
end
end

0 comments on commit d930c31

Please sign in to comment.