1#!/usr/bin/env python3

2

3 import os

4 import sys

5 from gettext import gettext as _

6 import gettext

7

8

9 def cli_prompt():

10 import termios

11

12 limit = 50

13 separator = ord("\n")

14 fileno = sys.stdin.fileno()

15 saved_attributes = termios.tcgetattr(fileno)

16 attributes = termios.tcgetattr(fileno)

17 attributes[3] = attributes[3] & ~(termios.ICANON)

18 attributes[6][termios.VMIN] = 1

19 attributes[6][termios.VTIME] = 0

20 termios.tcsetattr(fileno, termios.TCSANOW, attributes)

21

22 sys.stdout.write(_("Enter text:\n"))

23

24 input = ""

25 try:

26 while len(input) < limit:

27 ch = str(sys.stdin.read(1))

28 if ord(ch) == separator:

29 break

30 input += ch

31 finally:

32 termios.tcsetattr(fileno, termios.TCSANOW, saved_attributes)

33

34

35 def gtk_prompt():

36 import gi

37

38 gi.require_version("Gdk", "3.0")

39 gi.require_version("Gtk", "3.0")

40 from gi.repository import Gtk, Gdk

41

42 # create a new window

43 window = Gtk.Window()

44 window.set_type_hint(Gdk.WindowType.TOPLEVEL)

45 window.set_size_request(200, 100)

46 window.set_resizable(False)

47 window.set_title(_("Type Text"))

48 window.connect("delete_event", lambda w, e: Gtk.main_quit())

49

50 vbox = Gtk.VBox()

51 vbox.set_homogeneous(False)

52 vbox.set_spacing(0)

53 window.add(vbox)

54 vbox.show()

55

56 entry = Gtk.Entry()

57 entry.set_max_length(50)

58 vbox.pack_start(entry, True, True, 0)

59 entry.show()

60

61 hbox = Gtk.HBox()

62 hbox.set_homogeneous(False)

63 hbox.set_spacing(0)

64 vbox.add(hbox)

65 hbox.show()

66

67 button = Gtk.Button(stock=Gtk.STOCK_CLOSE)

68 button.connect("clicked", lambda w: Gtk.main_quit())

69 vbox.pack_start(button, False, False, 0)

70 button.set_can_default(True)

71 button.grab_default()

72 button.show()

73 window.show()

74

75 Gtk.main()

76

77

78 def main(args):

79

80 gettext.textdomain("checkbox")

81

82 if "DISPLAY" in os.environ:

83 gtk_prompt()

84 else:

85 cli_prompt()

86

87 return 0

88

89

90 if __name__ == "__main__":

91 sys.exit(main(sys.argv[1:]))