Spakman / urchin

A Unix shell for Ruby programmers.
http://spakman.github.com/urchin/
GNU General Public License v3.0
36 stars 1 forks source link

Make Shell.prompt specifiable per-directory #7

Open Spakman opened 13 years ago

Spakman commented 13 years ago

I'm not sure how best to implement this - POSIX extended attributes? Or a simple array of directories with corresponding prompts in .urchin.rb.

brandondrew commented 11 years ago

I'm picturing wanting to make simple modifications on a per-directory basis, rather than having to define the entire prompt separately for each directory (or group of directories). Would it be too awkward to sub-class per directory, so that you could just modify the bits you want different for that directory? Maybe something like this... the to_s method would be the main thing that defined the prompt, and then you could add as many other methods as you wanted the various items you wanted to appear in the prompt. The main class could ship with Urchin and you could either use open classes to change parts, or just subclass it for your own prompts, or both.

class Urchin::Prompt
  def ending
    ">: "
  end
  def current_directory
    `pwd`
  end
  def to_s
    "#{current_directory} #{ending}"
  end
end

class HomePrompt < Urchin::Prompt
  def current_directory
    `pwd`.gsub(`$HOME`, '~')
  end
end

Sorry the example is contrived and not very useful.

Spakman commented 11 years ago

Thanks a lot for the example. The more I think about it, the more I agree that having some way to share and extend prompt behaviours would be very useful.

As it stands, a prompt definition is simply a Proc that is run as we display the prompt. Although I don't plan to work on the per-directory things until after version 0.2 is out, we will need some way to encapsulate things so perhaps breaking out a Prompt class sooner is a good idea.